有没有一种方法可以在使用Spock框架时使用@MockBean批注

时间:2019-03-06 10:09:12

标签: spring-boot spock spring-test-mvc

我正在尝试Spock,在编写控制器测试时遇到了一个有趣的问题。

WebMvcTest(value = SomeController.class)
@AutoConfigureMockMvc
@ActiveProfiles(value = "restapi")
@Import(value = SecurityConfiguration)
class AccountBalanceControllerTest extends Specification {

@Autowired
SomeController someController

@MockBean
SomeService someService

def "lets test it" {
  given: 
      someService.findAllByName(_) >> ["Some", "Work"]
  when:
    def response = mockMvc.perform(get("/v1/someName/545465?fast=false").with(user("mvc-test").roles("SOME_ACCOUNTS")))
  then: 
      response.andExpect(status().isOk()) 
}


}

因此问题是SomeService实例上的模拟方法不起作用,因为它使用不同的Mock类来模拟SomeService类的实例。我在设置中使用了Spock中的静态Mock方法,然后使用了setter在控制器中设置SomeService来解决。我的问题是在Spock @MockBean测试中使用Specification的任何优雅方法。

0 个答案:

没有答案
相关问题