[英]Junit Test cases for Save and Update using Spring Boot REST
我編寫了一個測試用例來保存和更新,如下所示,
@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 如下,
@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!");
}
而ServiceImpl.java如下,
@Override
public void saveUser(User user) {
userMapper.saveUser(user);
}
@Override
public void updateUser(User user) {
userMapper.updateUser(user);
}
運行上述方法后,我收到如下錯誤,org.springframework.beans.factory.UnsatisfiedDependencyException: Error created bean with name 'com.naveen.MybatisApplicationTests': Unsatisfied dependency express through field 'user'; 嵌套異常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有“com.naveen.entity.User”類型的合格 bean 可用:預計至少有 1 個有資格作為自動裝配候選者的 bean。
您的測試邏輯錯誤(:
您將 userService 作為 Mock object - 如果您不自己編寫它,它將不會返回任何內容。 它不會在數據庫中保存任何東西。 您可以驗證調用此 Mock:
then(userService).should(times(1)).saveUser(any(User.class));
而且您不需要這些行:
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);
因為用戶也是模擬 object。
所以你的測試將是這樣的:
@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.