简体   繁体   English

Junit 使用 Spring 引导进行保存和更新的测试用例 REST

[英]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,两种方法的 Controller 如下,

 @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,而ServiceImpl.java如下,

@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';运行上述方法后,我收到如下错误,org.springframework.beans.factory.UnsatisfiedDependencyException: Error created bean with name 'com.naveen.MybatisApplicationTests': Unsatisfied dependency express 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.嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.naveen.entity.User”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。

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.您将 userService 作为 Mock object - 如果您不自己编写它,它将不会返回任何内容。 And it won't save in the db anything.它不会在数据库中保存任何东西。 You can just verify calling this Mock:您可以验证调用此 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.因为用户也是模拟 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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