Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, make sure you distinguish between <em>unit tests</em> and <em>integration tests</em>. JUnit is just a framework that helps you organize and run the tests, but you have to determine the scope of your tests.</p> <p>I assume you're interested in defining a <strong>unit</strong> test of <code>CommentService.findAll()</code>. What does that mean? That means I'll verify that calling the <code>findAll()</code> method results in CommentService invoking the named query named by the <code>FIND_ALL</code> string constant.</p> <p>Thanks to dependency injection and stubbing, you can easily achieve that using e.g. <a href="http://mockito.org/" rel="noreferrer">Mockito</a> to stub out the <code>EntityManager</code>. For the unit test, we're only focusing on the business logic in <code>findAll()</code>, so I won't bother testing lookup of the Comment service either--testing that the Comment service can be looked up and is wired to a proper entity manager instance is in the scope of an integration test, not a unit test.</p> <pre><code>public class MyCommentServiceUnitTest { CommentService commentService; EntityManager entityManager; @Before public void setUp() { commentService = new CommentService(); entityManager = mock(EntityManager.class); commentService.setEm(entityManager); // inject our stubbed entity manager } @Test public void testFindAll() { // stub the entity manager to return a meaningful result when somebody asks // for the FIND_ALL named query Query query = mock(Query.class); when(entityManager.createNamedQuery(Comment.FIND_ALL, Comment.class)).thenReturn(query); // stub the query returned above to return a meaningful result when somebody // asks for the result list List&lt;Comment&gt; dummyResult = new LinkedList&lt;Comment&gt;(); when(query.getResultList()).thenReturn(dummyResult); // let's call findAll() and see what it does List&lt;Comment&gt; result = commentService.findAll(); // did it request the named query? verify(entityManager).createNamedQuery(Comment.FIND_ALL, Comment.class); // did it ask for the result list of the named query? verify(query).getResultList(); // did it return the result list of the named query? assertSame(dummyResult, result); // success, it did all of the above! } } </code></pre> <p>With the unit test above, I tested the <strong>behavior</strong> of the <code>findAll()</code> implementation. The unit test verified that the correct named query is obtained and that the result returned by the named query was returned to the callee.</p> <p>What's more, the unit test above verifies that the implementation of <code>findAll()</code> is correct independently of the underlying JPA provider and the underlying data. I don't want to test JPA and the JPA provider unless I suspect there are bugs in the 3rd party code, so stubbing out these dependencies lets me focus the test entirely on the business logic of the Comment service.</p> <p>It can take a little while to adjust to the mindset of testing behavior using stubs, but it is a very powerful technique for testing the business logic of your EJB 3.1 beans because it lets you isolate and narrow the scope of each test to exclude external dependencies.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload