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 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.
Aucun commentaire:
Enregistrer un commentaire