0

Is there any way to write unit tests for save methods in DAO layer when return type is void? I'm using Log4j, Junit in spring boot project.

I've tried many ways to assert them. But since they aren't returning any value i wasn't able to assert them.

Vimukthi_R
  • 1,440
  • 3
  • 18
Teguwih
  • 43
  • 1
  • 6

4 Answers4

6

If the method is void, then it has side-effects, otherwise it would be a no-op.

So you call the method, then check if the desired side-effect happened.

E.g. setFoo(7) should mean that getFoo() returns 7, though unit testing simple getter/setter methods is a waste of time.

It is common to use mock objects to detect the side-effects, but it all depends on what the expected side-effect is. See: What is the purpose of mock objects?

Andreas
  • 138,167
  • 8
  • 112
  • 195
  • Let's say I'm trying to save some values to database. But inside that save method i won't return anything. In that kind of a scenario how can i make sure that data has successfully saved into database or not by using unit testing? – Teguwih Jun 12 '19 at 18:03
  • 1
    By mocking the database access objects and verifying the correct calls where made on them – David Zimmerman Jun 12 '19 at 18:08
  • @Teguwih What are you testing? The database? Don't. Hibernate? Don't. So what is it that you are testing in the code you are calling? – Andreas Jun 12 '19 at 18:42
  • @Andreas i'm new to spring. I've used mysql as database and jdbc connection to connect to the database. I've wrote a save method to save an entity into databse table. but that method won't return anything. I tried vimukthi_r's answer. It seems to be working as i expected. – Teguwih Jun 12 '19 at 18:57
2

According to your comments you need to write an unit test for a save method. Try this example code,

@Autowired
private EmployeeDAO employeeDAO;

@Test
public void whenValidEmployee_thenShouldSave()
{
    EmployeeEntity employee = new EmployeeEntity("1", "Department Name", "Role"); //id, department name and role are passing as constructor parameters
    employeeDAO.save(employee);

    List<EmployeeEntity> employees = employeeDAO.findAll();

    //Assert
    Assert.assertEquals(employee.getId(), employees.get(0).getId());
}
Vimukthi_R
  • 1,440
  • 3
  • 18
2

Writing a testable code is important as a developer in modern days. You should understand that a method with void, is bad for a single reason.it is not testable by any means. i would suggest you below actions to take

  1. Improve your code with a relevant return type.
  2. It's worth applying DbUnit, rather than applying just Junit to test your DAO layer.

@Teguwih

1

There are several ways to unit test a method that returns void.

  1. Change the method so that it returns a value even though you don't use that value. This is NOT the best way, but I note it here for completeness.

  2. The method likely changes the state of the object in some way. A file was saved, a value was stored somewhere, parameters have been changed, etc. So check the values that should have been changed. You can read back a saved file, a changed variable, data in a test database, etc.

  3. Mock objects can be used to determine if a method was called and what the behavior was. There are a number of mock object frameworks for Java, including Easy Mock, JMockit and Mockito. How to use a mock framework is beyond the scope of this answer, but I did include links to the various sites for your reference.

  4. If bad inputs are given to the method it may throw an exception. It is a good idea to do this to test the error handling of your methods.

Bill W
  • 1,287
  • 1
  • 16
  • 28