Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I refactor and unit test complex legacy Java EE5 EJB methods?
    text
    copied!<p>My colleagues and I are currently introducing unit tests to our legacy Java EE5 codebase. We use mostly JUnit and Mockito. In the process of writing tests, we have noticed that several methods in our EJBs were hard to test because they did a lot of things at once.</p> <p>I'm fairly new to the whole testing business, and so I'm looking for insight in how to better structure the code or the tests. My goal is to write good tests without a headache.</p> <p>This is an example of one of our methods and its logical steps in a service that manages a message queue:</p> <ul> <li><p>consumeMessages</p> <ul> <li><p>acknowledgePreviouslyDownloadedMessages</p></li> <li><p>getNewUnreadMessages</p></li> <li><p>addExtraMessages (depending on somewhat complex conditions)</p></li> <li><p>markMessagesAsDownloaded</p></li> <li><p>serializeMessageObjects</p></li> </ul></li> </ul> <p>The top-level method is currently exposed in the interface, while all sub-methods are private. As far as I understand it, it would be bad practice to just start testing private methods, as only the public interface should matter.</p> <p>My first reaction was to just make all the sub-methods public and test them in isolation, then in the top-level method just make sure that it calls the sub-methods. But then a colleague mentioned that it might not be a good idea to expose all those low-level methods at the same level as the other one, as it might cause confusion and other developers might start using when they should be using the top-level one. I can't fault his argument.</p> <p>So here I am.</p> <p>How do you reconcile exposing easily testable low-level methods versus avoiding to clutter the interfaces? In our case, the EJB interfaces.</p> <p>I've read in other unit test questions that one should use dependency injection or follow the single responsibility principle, but I'm having trouble applying it in practice. Would anyone have pointers on how to apply that kind of pattern to the example method above?</p> <p>Would you recommend other general OO patterns or Java EE patterns?</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