Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

Do I Really Need to Test This??

2 min read

Yes! Let's start straight of with an example. Consider I have my service (business) class SourceCodeItemService and an according SourceCodeItemDao class in my nicely layered app and there is the method removeSourceCodeItem(SourceCodeItem) with the following content
public class SourceCodeItemService implements ISourceCodeItemService{
private ISourceCodeItemDao sourceCodeItemDao; //this will be injected at runtime

public void removeSourceCodeItem(SourceCodeItem itemToRemove){
Note, the method really only has one line and all the logic it executes is to delegate the command to the underlying data access object class. Many devs, especially "unit testing newbies" would be tempted to not test this method because "it hasn't really logic inside but just delegates". Indeed, to some degree this is true, but note that the SourceCodeItemService class is just in its beginnings.
Remember, a test should not only verify the correctness of the current behavior but also preserve that behavior in case of future changes.
I want to be sure that if other devs modify my remove method, it will still work the way I expected it to do!

Said that, let's take a look on how the actual unit test for such a method would look like. First of all, what do we want to verify? We want to test the logic of the SourceCodeItemService.removeSourceCodeItem(...) in isolation. In a test-first approach, we know that our service class will use a DAO for persisting the delete operation. So as there will be no other planned behavior for the remove method we want to assure that dao is being called correctly, nothing more, nothing less.
public class SourceCodeItemServiceTest{

//setup stuff etc..

public void testRemoveSourceCodeItem(){
//the dummy item to be deleted
SourceCodeItem item = new SourceCodeItem(...);

That's it. I'm using Mockito here, a mocking library for Java. The verify call in the middle is a call to the Mockito library which assures the method delete of the DAO is correctly called with the object I'm passing. So there is a further check that the correct object is being given to the DAO. However you could easily also achieve the same behavior without a mocking framework, although I highly suggest you to adopt one which suits your needs.

So to conclude, a question that may arise when you look at this is "how I did verify whether the item has actually been deleted". To be honest, I didn't! But this is not the scope of the unit test of this service class here but rather of a possible (integration) test of the SourceCodeItemDao class :)
Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus