模拟 JWT Utils 以验证令牌
Mock JWT Utils to validate Token
我想为此端点创建 JUnkt 测试:
@Autowired
private JwtTokenProvider jwtTokenProvider;
@PostMapping("reset_token")
public ResponseEntity<?> resetToken(@Valid @RequestBody ResetPasswordTokenDTO resetPasswordTokenDTO, BindingResult bindResult) {
final String login = jwtTokenProvider.getUsername(resetPasswordTokenDTO.getResetPasswordToken());
}
完整代码:Github
JUnit 测试:
@Test
public void resetTokenTest_NOT_FOUND() throws Exception {
when(usersService.findByResetPasswordToken(anyString())).thenReturn(Optional.empty());
mockMvc.perform(post("/users/reset_token")
.contentType(MediaType.APPLICATION_JSON)
.content(ResetPasswordTokenDTO))
.andExpect(status().isNotFound());
}
当我 运行 代码时,我在这一行得到了 NPE:
final String login = jwtTokenProvider.getUsername(resetPasswordTokenDTO.getResetPasswordToken());
我如何模拟我加载的 jwtTokenProvider properly? As you can see I have a file with test data 但未提取令牌。你知道我该如何解决这个问题吗?
最直接的方法是使用 Mockito 并创建模拟实例,然后使用构造函数注入将其直接传递给您的控制器 class。
但是,如果您不想使用构造函数注入(尽管我建议您使用它,因为它更明确),您需要在单独的测试配置中定义您的 bean class
@Profile("test")
@Configuration
public class TestConfiguration {
@Bean
public JwtTokenProvider mockJwtTokenProvider() {
return Mockito.mock(JwtTokenProvider.class);
}
}
此外,通过 @ActiveProfiles("test")
将正确的配置文件添加到您的测试 class
您可以考虑在测试 class 中直接使用 @MockBean
来模拟您的 JwtTokenProvider
。 @MockBean annotation is Spring-ish and is included in spring-boot-starter-test
. The Spring Boot documentation总结的很好:
Spring Boot includes a @MockBean annotation that can be used to define
a Mockito mock for a bean inside your ApplicationContext. You can use
the annotation to add new beans or replace a single existing bean
definition. The annotation can be used directly on test classes, on
fields within your test, or on @Configuration classes and fields. When
used on a field, the instance of the created mock is also injected.
Mock beans are automatically reset after each test method.
@MockBean
注释将使 Spring 在其应用程序上下文中查找类型为 JwtTokenProvider
的现有单个 bean。如果存在,模拟将替换那个 bean,如果不存在,它将在应用程序上下文中添加新的模拟。
您的测试 class 将如下所示:
import org.springframework.boot.test.mock.mockito.MockBean;
@MockBean
@Qualifier("xxx") //If there is more than one bean of type JwtTokenProvider
private JwtTokenProvider jwtTokenProvider;
@Test
public void resetTokenTest_NOT_FOUND() throws Exception {
when(jwtTokenProvider.getUsername(anyString())).thenReturn(Optional.empty());
mockMvc.perform(post("/users/reset_token")
.contentType(MediaType.APPLICATION_JSON)
.content(ResetPasswordTokenDTO))
.andExpect(status().isNotFound());
}
您可能还想查看 this and 。
我想为此端点创建 JUnkt 测试:
@Autowired
private JwtTokenProvider jwtTokenProvider;
@PostMapping("reset_token")
public ResponseEntity<?> resetToken(@Valid @RequestBody ResetPasswordTokenDTO resetPasswordTokenDTO, BindingResult bindResult) {
final String login = jwtTokenProvider.getUsername(resetPasswordTokenDTO.getResetPasswordToken());
}
完整代码:Github
JUnit 测试:
@Test
public void resetTokenTest_NOT_FOUND() throws Exception {
when(usersService.findByResetPasswordToken(anyString())).thenReturn(Optional.empty());
mockMvc.perform(post("/users/reset_token")
.contentType(MediaType.APPLICATION_JSON)
.content(ResetPasswordTokenDTO))
.andExpect(status().isNotFound());
}
当我 运行 代码时,我在这一行得到了 NPE:
final String login = jwtTokenProvider.getUsername(resetPasswordTokenDTO.getResetPasswordToken());
我如何模拟我加载的 jwtTokenProvider properly? As you can see I have a file with test data 但未提取令牌。你知道我该如何解决这个问题吗?
最直接的方法是使用 Mockito 并创建模拟实例,然后使用构造函数注入将其直接传递给您的控制器 class。
但是,如果您不想使用构造函数注入(尽管我建议您使用它,因为它更明确),您需要在单独的测试配置中定义您的 bean class
@Profile("test")
@Configuration
public class TestConfiguration {
@Bean
public JwtTokenProvider mockJwtTokenProvider() {
return Mockito.mock(JwtTokenProvider.class);
}
}
此外,通过 @ActiveProfiles("test")
您可以考虑在测试 class 中直接使用 @MockBean
来模拟您的 JwtTokenProvider
。 @MockBean annotation is Spring-ish and is included in spring-boot-starter-test
. The Spring Boot documentation总结的很好:
Spring Boot includes a @MockBean annotation that can be used to define a Mockito mock for a bean inside your ApplicationContext. You can use the annotation to add new beans or replace a single existing bean definition. The annotation can be used directly on test classes, on fields within your test, or on @Configuration classes and fields. When used on a field, the instance of the created mock is also injected. Mock beans are automatically reset after each test method.
@MockBean
注释将使 Spring 在其应用程序上下文中查找类型为 JwtTokenProvider
的现有单个 bean。如果存在,模拟将替换那个 bean,如果不存在,它将在应用程序上下文中添加新的模拟。
您的测试 class 将如下所示:
import org.springframework.boot.test.mock.mockito.MockBean;
@MockBean
@Qualifier("xxx") //If there is more than one bean of type JwtTokenProvider
private JwtTokenProvider jwtTokenProvider;
@Test
public void resetTokenTest_NOT_FOUND() throws Exception {
when(jwtTokenProvider.getUsername(anyString())).thenReturn(Optional.empty());
mockMvc.perform(post("/users/reset_token")
.contentType(MediaType.APPLICATION_JSON)
.content(ResetPasswordTokenDTO))
.andExpect(status().isNotFound());
}
您可能还想查看 this and