简体   繁体   中英

Junit Test cases for Save and Update using Spring Boot REST

I've written a Test case to save & update which as below,

@InjectMocks
UserController uc;

@Mock
UserService userService;
@Mock
User user;

@Test
public void saveUser() throws Exception {
    SimpleDateFormat simpleformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:s+ss");
    
    Long id= 2L;
    String userName = "John";
    String passWord = "john";
    Date otpSentDate = simpleformat.parse("2019-12-24 01:22:05+00");
    Date createdDate = simpleformat.parse("2019-12-24 01:22:05+00");
    Date updatedDate = simpleformat.parse("2019-12-24 01:22:05+00");
    user.setId(id);user.setUserName(userName);user.setPassWord(passWord);user.setOtpSentDate(otpSentDate);
    user.setCreatedDate(createdDate);user.setUpdatedDate(updatedDate);
    assertEquals(1, uc.saveUser(user));
}

@Test
public void updateUser() throws Exception {
    SimpleDateFormat simpleformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:s+ss");
    
    Long id= 2L;
    String userName = "John";
    String passWord = "john";
    Date otpSentDate = simpleformat.parse("2019-12-24 01:22:05+00");
    Date createdDate = simpleformat.parse("2019-12-24 01:22:05+00");
    Date updatedDate = simpleformat.parse("2019-12-24 01:22:05+00");
    user.setId(id);user.setUserName(userName);user.setPassWord(passWord);user.setOtpSentDate(otpSentDate);
    user.setCreatedDate(createdDate);user.setUpdatedDate(updatedDate);
    assertEquals(1, uc.updateUser(user));
}

Controller for both methods are as below,

 @PostMapping("/saveUser")
public int saveUser(@RequestBody User user) throws Exception {
    if (user.getUserName() == "" || user.getPassWord() == "") {
        throw new Exception("User name or Password should not be empty!");
    } else {
        userService.saveUser(user);
        System.out.println("Inserted data with id: "+ user.getId());
    }
    return 1;
}
@PutMapping("/updateUser")
public void updateUser(@RequestBody User user) {
    userService.updateUser(user);
    System.out.println("User with id "+ user.getId() + " updated successfully!");
}

And ServiceImpl.java are as follows,

@Override
public void saveUser(User user) {
    userMapper.saveUser(user);
}
 @Override
public void updateUser(User user) {
    userMapper.updateUser(user);
}

After running above methods, I'm getting error as below, org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.naveen.MybatisApplicationTests': Unsatisfied dependency expressed through field 'user'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.naveen.entity.User' available: expected at least 1 bean which qualifies as autowire candidate.

You test logic is wrong (:

You have userService as a Mock object - it won't return anything if you don't write it by yourself. And it won't save in the db anything. You can just verify calling this Mock:

then(userService).should(times(1)).saveUser(any(User.class));

And you don't need these rows:

SimpleDateFormat simpleformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:s+ss");
    
    Long id= 2L;
    String userName = "John";
    String passWord = "john";
    Date otpSentDate = simpleformat.parse("2019-12-24 01:22:05+00");
    Date createdDate = simpleformat.parse("2019-12-24 01:22:05+00");
    Date updatedDate = simpleformat.parse("2019-12-24 01:22:05+00");
    user.setId(id);user.setUserName(userName);user.setPassWord(passWord);user.setOtpSentDate(otpSentDate);
    user.setCreatedDate(createdDate);user.setUpdatedDate(updatedDate);

because user is also a Mock object.

So your test will be something like that:

@Test
public void saveUser() throws Exception {
//given
     given(user.getUserName()).willReturn("Username");
     given(user.getPassWord()).willReturn("Password");
     given(user.getId()).willReturn(1);
     given(userService.saveUser(any(User.class))).willReturn(user);
//when
     int result = uc.saveUser(user);
//then
     then(userService).should(times(1)).saveUser(any(User.class));
     assertEquals(1, result);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM