【发布时间】:2020-01-01 10:26:18
【问题描述】:
我目前正在学习 Spring-boot 的课程,并且我一直在测试一个项目 - 非常感谢任何帮助,因为我是这里的初学者。
我有一个休息控制器测试,使用 Mockito,当使用 Mockito.when() 调用方法时,它似乎忽略了“ThenReturn”。
这是全班:
package com.example.demo.controllers;
import com.example.demo.TestUtils;
import com.example.demo.model.persistence.AppUser;
import com.example.demo.model.persistence.repositories.CartRepository;
import com.example.demo.model.persistence.repositories.UserRepository;
import com.example.demo.model.requests.CreateUserRequest;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.Optional;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class UserControllerTest {
private UserController userController;
private UserRepository userRepository = mock(UserRepository.class);
private CartRepository cartRepository = mock(CartRepository.class);
private BCryptPasswordEncoder bCryptPasswordEncoder = mock(BCryptPasswordEncoder.class);
@Before
public void initTest(){
userController = new UserController();
TestUtils.injectObjects(userController, "userRepository", userRepository);
TestUtils.injectObjects(userController, "cartRepository", cartRepository);
TestUtils.injectObjects(userController, "bCryptPasswordEncoder", bCryptPasswordEncoder);
AppUser appUser = TestUtils.getAppUser();
when(userRepository.findById(0L)).thenReturn(Optional.of(appUser));
when(bCryptPasswordEncoder.encode("testPassword")).thenReturn("hashedPassword");
}
@Test
public void testFindUserById(){
ResponseEntity<AppUser> response = userController.findById(0L);
System.out.println(response);
}
@Test
public void testCreateUser() throws Exception{
CreateUserRequest createUserRequest = new CreateUserRequest();
createUserRequest.setUsername("testUser");
createUserRequest.setPassword("testPassword");
createUserRequest.setConfirmPassword("testPassword");
ResponseEntity<AppUser> response = userController.createUser(createUserRequest);
assertNotNull(response);
assertEquals(200, response.getStatusCodeValue());
AppUser createdUser = response.getBody();
assertNotNull(createdUser);
assertEquals(0, createdUser.getId());
assertEquals("testUser", createdUser.getUsername());
assertEquals("hashedPassword", createdUser.getPassword());
}
}
名为“testCreateUser”的测试顺利通过。给我带来问题的是名为“testFindUserById”的测试。
这是我正在尝试测试的控制器方法(在 Postman 中测试时一切正常):
public ResponseEntity<AppUser> findById(@PathVariable Long id) {
try{
log.info("UserIDSearch = " + id);
System.out.println("UserIDSearch = " + id);
Optional<AppUser> optionalAppUser = userRepository.findById(id);
if(optionalAppUser.isPresent()){
log.info("UserIdFound = " + id);
return ResponseEntity.ok(optionalAppUser.get());
}else{
throw new ApiException(ExceptionTypes.SEARCHUSER, id.toString());
}
}catch(ApiException a){
return ResponseEntity.notFound().build();
}
}
在测试类中被模拟的存储库只是一个简单的 JpaRepository:
public interface UserRepository extends JpaRepository<AppUser, Long> {
Optional<AppUser> findByUsername(String username);
public Optional<AppUser> findById(long id);
}
运行 testFindUserById() 测试得到的输出如下:
UserIDSearch = 0
<404 NOT_FOUND Not Found,[]>
我想我在这里想要实现的是测试使用 when().thenReturn() 来模拟来自模拟 userRepository 的 OK 响应,但实际上它执行搜索并返回“未找到” .任何人都可以帮忙吗?非常感谢!
【问题讨论】:
-
抛出的异常信息是什么?
-
感谢您的回复!也不例外 - 它似乎完全忽略了
thenReturn(Optional.of(appUser))部分并实际在存储库上执行搜索 - 我不知道为什么。有点像testCreateUser()方法模拟从bCryptPasswordEncoder返回密码时,我试图让testFindUserById方法模拟从存储库返回用户。 -
从响应中似乎代码进入了 catch 块 - 不是这样吗?
-
据我了解(可能是错误的),实际上不应执行搜索,因为 mockito 应该接听电话
Optional<AppUser> optionalAppUser = userRepository.findById(id);并返回Optional.of(appUser),它可用于构建返回的 ReponseEntity通过控制器方法。但这没有发生。 -
嗯,你是个传奇。您的两个小提示使我能够使我的 4 个测试课程中的 3 个完全正常工作。非常感谢!
标签: java junit mocking mockito