Search code examples
javajunit5spring-boot-test

Unit testing save methods when return type is void


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.


Solution

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