首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring中以测试模式运行时,如何禁用方法?

在Spring中以测试模式运行时,如何禁用方法?
EN

Stack Overflow用户
提问于 2018-12-11 09:32:06
回答 1查看 99关注 0票数 0

我有这样一种方法:

代码语言:javascript
复制
@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,这样这个方法就不会被调用?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-11 10:16:51

在测试模式中,可以在模拟中替换amqpService:

代码语言:javascript
复制
@Configuration
public class AmqpConfig {

   @Profile("test")
   @Bean 
   public AmqpService amqpService(){
     return mock(AmqpService.class);
   }   
}

或者,您可以在测试类中立即使用@MockBean来替换测试中模拟中的bean。

因此,您可以运行一个模拟方法,而不是一个真正的对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53721115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档