Reputation: 19
I'm creating a simple web application in JAVA EE using (EJB, JPA and JPQL). I want to know how to test the model using mockito to test the EJB. I want to mock the context (database, and properties)
How do I do that ?
@Mock
private EntityManager mockedEntityManager;
private static TendererBean tendererBean;
private static TendererManagerBean tendererManagerBean;
private static EJBContainer container;
final Properties p = new Properties();
p.put("jdbc/Mydatabase", "new://Resource?type=DataSource");
p.put("jdbc/Mydatabase.JdbcDriver", "apache_derby_net");
p.put("jdbc/Mydatabase.JdbcUrl", "jdbc:derby://localhost:1527/Mydatabase");
container = EJBContainer.createEJBContainer(p);
final Context context = container.getContext();
tendererBean = (TendererBean) context.lookup("java:global/classesTendererManagerBean");
Upvotes: 2
Views: 3240
Reputation: 316
Alex is right, you should not need database connections in a unit test (unless you trying to create an integration test) You can try mocking them like this. The code may be messy, hope it helps
public class ChargeServiceTest {
@InjectMocks
private ChargeService chargeService;
@Mock
private EntityManager entityManager;
@Mock
private Query query;
@Mock
private Biller biller;
@Mock
private Client client;
private String expectedSqlQuery1 = "Select count(c) from ClientContractCharge c where c.clientContract.biller =?1 and c.recurringInvoice = true and c.clientContract.client = ?2 and (c.dateCreated >= ?3 or c.dateLastUpdated >= ?3) order by c.dateCreated ASC";
@Before
public void before() {
chargeService = new ChargeService();
MockitoAnnotations.initMocks(this);
}
@Test
public void listCountTest1() {
when(entityManager.createQuery(expectedSqlQuery1)).thenReturn(query);
when(query.getSingleResult()).thenReturn((long) 10);
Long count = chargeService.listCount(biller, client);
verify(query).setParameter(1, biller);
assertEquals(count, Long.valueOf(10));
}
}
Service class
@Stateless
@LocalBean public class ChargeService {
public long listCount(Biller biller, Client client) {
StringBuilder s = new StringBuilder();
s.append("Select count(c) from ClientContractCharge c where c.clientContract.biller =?1 and c.recurringInvoice = true ");
if (client != null) {
s.append("and c.clientContract.client = ?2 ");
}
s.append(" order by c.dateCreated ASC");
Query q = em.createQuery(s.toString());
q.setParameter(1, biller);
if (client != null) {
q.setParameter(2, client);
}
return (long) (Long) q.getSingleResult();
}
Upvotes: 2
Reputation: 6939
It should not be necessary to hassle around with db properties in your test code. Another thing to keep in mind: A unit test should not need a database connection, because then it's more than a unit test, it should mock away the persistence.
EntityManager
can easily be mocked with for example Mockito. Additionally, you can mock a Query
to get the needed result for the test.
Check out Adam Bien's post on Mocking JPA EntityManager and Query for how this can be done.
Upvotes: 1