coo*_*ego 1 tdd spring-security spring-boot junit5
这是我使用 TDD 和 JUNIT 5 的第一个项目。我正在为我的项目使用最新的 Springboot。
我注意到,当我有来自 springboot 的依赖项时,当使用 @InjectMocks 注释时,它们不会在测试阶段被注入。我收到了 authenticationManager 依赖项的 NullPointerException。但是,当 service 方法仅使用使用 springboot JPA 为实体类创建的存储库依赖项时,测试通过。
下面是服务类和对应的测试类。 UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
AuthenticationManager authenticationManager;
@Autowired
UserRepository userRepository;
@Autowired
PasswordEncoder passwordEncoder;
@Autowired
UserDetailsService userDetailsService;
@Autowired
JWTUtil jwtUtil;
@Override
@Transactional
public AuthenticationResponseDTO login(AuthenticationRequestDTO authenticationRequestDTO) {
try {
authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(authenticationRequestDTO.getUserName(), authenticationRequestDTO.getPassword()));
UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequestDTO.getUserName());
return new AuthenticationResponseDTO(userDetails.getUsername(), jwtUtil.generateToken(userDetails));
}
catch (BadCredentialsException e) {
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
UserServiceImplTest.java
@InjectMocks
private UserServiceImpl userServiceImpl;
@Mock
private UserRepository userRepository;
private User userMock;
private AuthenticationRequestDTO authenticationRequestDTO;
@BeforeEach
void init(){
MockitoAnnotations.initMocks(this);
}
@BeforeEach
void setupUser(){
userMock = new User();
userMock.setUserName("sd");
userMock.setPassword("sd");
authenticationRequestDTO = new AuthenticationRequestDTO();
authenticationRequestDTO.setUserName("sd");
authenticationRequestDTO.setPassword("sd");
}
@Test
void testUserIsPresentOrNot(){
Mockito.when( userRepository.findByUserName("sd") ).thenReturn(userMock);
AuthenticationResponseDTO responseDTO = userServiceImpl.login(authenticationRequestDTO);
assertNotNull(responseDTO);
assertEquals(userMock.getUserName(), responseDTO.getName(), "user id should be same.");
}
Run Code Online (Sandbox Code Playgroud)
请让我知道是否需要我的进一步详细信息。
我不确定我是否 100% 关注,因为我可以看到您缺少一些注释。
如果你想在你需要的单元测试中注入 Spring bean @SpringBootTest/@ExtendWith(SpringExtension.class)(从我的角度来看,使它成为集成测试)。
如果你想使用 Mockito
@InjectMocks并且@Mock需要@ExtendWith(MockitoExtension.class)上面的测试类。
并删除以下内容。
@BeforeEach
void init(){
MockitoAnnotations.initMocks(this);
}
Run Code Online (Sandbox Code Playgroud)
从我的角度来看,将依赖注入与 spring 和 Mockito 混合使用会太复杂。我宁愿只使用 Mockito 进行单元测试。
也许下面的文章可以提供帮助。
| 归档时间: |
|
| 查看次数: |
4253 次 |
| 最近记录: |