How test a void method into Service class, with Mockito?

Alessandro Moretti :

I need to make pass all my test and to reach a complete code coverage for all classes of my project.

I created a Repository, Service and Controller class.

I need to test all single method into these classes.

I have these classes: 1) ApplicationController with ApplicationControllerTest 2) UserService with UserServiceTest 3) UserRepository with UserRepositoryTest

This is the controller:

 @Controller
    @RequestMapping("/")
    @SessionAttributes("user")
    public class ApplicationWebController {

    @Autowired
    private UserService userService;

    @Autowired
    private FundService fundService;

    @PostMapping("/save-user")
    public String saveUser(User user, Model model) {
        final User presentUsername = 
    userService.getUserByUsername(user.getUsername());
        if (presentUsername == null) {
            userService.insertNewUser(user);
            if((user.getUsername()).equals("admin")) {
                userService.updateRoleToAdmin(user.getId());
            }
    }

Here I insert a new User; if User.username is equal to "admin" then I update the field "role" with userService.updateRoleToAdmin(user.getId())

This is the JUnit test for controller:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = ApplicationWebController.class)
public class ApplicationWebControllerTest {

@Autowired
private WebClient webClient;

@MockBean
private UserService userService;

@Test
public void test_whenInsertedUserIsAdmin_DoUpdateOnRole() throws 
Exception {

User userInserted = new User(1L, "admin", "password", 1);       

mvc.perform(post("/save-user")
        .param("id", "1")
        .param("username", "admin")
        .param("password", "password")
        .param("role", "1"));

if (userInserted.getUsername() == "admin") {
    verify(userService).updateRoleToAdmin(1L);
}
}
}

this test correctly pass.

Now I have the Service class and the relative test class

This is the Service class

@Service
public class UserService {

private UserRepo userRepo;

public void updateRoleToAdmin(Long id) {
    userRepo.updateRoleToAdmin(id);
}
}

This class also must be tested and this is the JUnit class for the Service

@RunWith(MockitoJUnitRunner.class)
public class UserServiceTest {

@Mock
private UserRepo userRepo;

@InjectMocks
private UserService userService;

@Test
public void test_updateRoleToAdmin() {
//I DON'T KNOW HOW WRITE HERE
        //I NEED TO TEST THE RESPECTIVE METHOD IN SERVICE CLASS
        //THE METHOD IN userRepo IS A VOID METHOD 
}
}

These are my Repository classes (class and JUnit test)

public interface UserRepo extends JpaRepository<User, Long> {

@Modifying(flushAutomatically = true, clearAutomatically = true)
@Transactional
@Query("update User u set u.role = 2 where u.id = :userid")
void updateRoleToAdmin(@Param("userid") Long id);

}

The test for the Repo class:

@Test
public void test_UpdateRoleToAdmin() {
User user = new User(null, "admin", "password", 1);
User saved = userRepo.save(user);
userRepo.updateRoleToAdmin(saved.getId());
User found = userRepo.findByUsername("admin");
assertThat(found.getRole()).isEqualTo(2);
}

also, this test correctly pass

Now I must write a test into UserServiceTest JUnit test class.

I don't know what should I do, because it's a void method and I can't use when(method).thenReturn(..) and the following Assertion

I must have a Code Coverage of 100% for all my classes and without test the method "UserService.updateRoleToAdmin(Long id)" I can't reach it.

Please help, because I don't know how to test a void method there.

Thank you. I need to make pass all my test and to reach a complete code coverage for all classes of my project.

I created a Repository, Service and Controller class.

I need to test all single method into these classes.

Maciej Kowalski :

You should test what is happening inside of the SUT method. You already have the repo mocked so just verify behaviour in the test:

@Test
public void shouldUpdateRoleToAdmin() {
    // When
    userService.updateRoleToAdmin(anyLong());

    // Then
    verify(userRepo, times(1)).updateRoleToAdmin(anyLong()); 
}

after you have invoked the method with a certain id.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to test a void method using JUnit and/or Mockito

How to test void method with private method calls using Mockito

How to Test void Method with spring repository using Junit and Mockito

In Java, How to test void method in Junit Mockito with assertion?

How to mock/test method that returns void, possibly in Mockito

How to test a void method within a Presenter using Mockito?

Mockito: How to replace method of class which is invoked by class under test?

Test void method behavior junit and mockito

Mockito test a void method throws an exception

How to test a void class method without arguments with Xcode Unit Test

Mockito - Test if a method of a class is called

How to specify matchers in Mockito unit test for a SpringBoot based service class

How to test a method invocation inside an anonymous class using mockito

How to Unit Test A Method Argument which is a Builder Class in Mockito?

How to test void method that changes class variable in Angular

How to test void method with Jest

How to unit test a void method

Mockito to test void methods

NullPointerException when trying to test a void method in Presenter using Mockito

test void method using mockito and Junit with generic parameter

Mockito with void method in JUnit

Mockito with void method

Mockito testing method void

How to test update method with junit 5 and mockito with exception handling in the service layer

Mockito: How to test my Service with mocking?

Spring test service class mocking utility class- Junit and Mockito

Mockito uses method stubbing from other test method in the same class

How to test a method returns boolean in Mockito

How should I test this method with JUNIT and Mockito