Unit testing save methods when return type is void

51
June 13, 2019, at 11:00 AM

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.

Answer 1

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());
}
Answer 2

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?

Answer 3

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 good to test the error handling of your methods.

Answer 4

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

READ ALSO
Does this function rightly shuffle the first elements of a list into the whole list? [on hold]

Does this function rightly shuffle the first elements of a list into the whole list? [on hold]

I would like to transform a list partially shuffled (the first elements are not shuffled) into a fully shuffled list, I came up with this method, but I'm not sure that it produces a balanced shuffle, so the question is: is it the right way to do that?

52
Getting the max occurences of each individual number from a HashMap&lt;Integer, List&lt;Integer&gt;&gt;

Getting the max occurences of each individual number from a HashMap<Integer, List<Integer>>

I've been dealing with an exercise, in which I have to find a solution for the following problem: I have a

19
How to Read Specific Range value of an Object Property

How to Read Specific Range value of an Object Property

i'm new dealing with Ontologies and finding problems to get my GraphQL Query working , trying to read value of specific Object property that has multiple Ranges Object Property Screenshot

53
Unable to establish connection to unix server using Jsch and private key

Unable to establish connection to unix server using Jsch and private key

I am trying to establish a connection to a Unix server and put/get files using SFTPI am using JCraft's Jsch 0

32