我有这样一种方法:
@PostMapping(path = ["/signup"],
consumes = [(MediaType.APPLICATION_JSON_UTF8_VALUE)])
fun signUp(@RequestBody dto: RegistrationDto)
: ResponseEntity<Void> {
val userId : String = dto.userInfo!!.username!!
val password : String = dto.password!!
val registered = if(!dto.secretPassword.isNullOrBlank() && dto.secretPassword.equals(adminCode)) {
authService.createUser(userId, password, setOf("ADMIN"))
} else {
authService.createUser(userId, password, setOf("USER"))
}
if (!registered) {
return ResponseEntity.status(400).build()
}
val userDetails = userDetailsService.loadUserByUsername(userId)
val token = UsernamePasswordAuthenticationToken(userDetails, password, userDetails.authorities)
authenticationManager.authenticate(token)
if (token.isAuthenticated) {
SecurityContextHolder.getContext().authentication = token
}
/**
* AMQP
*/
amqpService.send(dto.userInfo!!, "USER-REGISTRATION")
return ResponseEntity.status(204).build()
}如果您注意到我有一个方法amqpService.send(dto.userInfo!!, "USER-REGISTRATION,当我在“开发”模式下运行时,如何禁用这个方法?
在测试模式下运行时,我想禁用RabbiqMQ,这样这个方法就不会被调用?
谢谢
发布于 2018-12-11 10:16:51
在测试模式中,可以在模拟中替换amqpService:
@Configuration
public class AmqpConfig {
@Profile("test")
@Bean
public AmqpService amqpService(){
return mock(AmqpService.class);
}
}或者,您可以在测试类中立即使用@MockBean来替换测试中模拟中的bean。
因此,您可以运行一个模拟方法,而不是一个真正的对象。
https://stackoverflow.com/questions/53721115
复制相似问题