Spock: Spring Boot Test MockBean and SpyBean support

Created on 7 Aug 2016  路  3Comments  路  Source: spockframework/spock

I am using @MockBean and @SpyBean features from new spring boot test framework

It provide Mockito mock insteadof raw bean. After that, i must write code like this:

  @SpyBean
  AuthenticationService authenticationService 
  def 'should try to authenticate'() {
    when:
     // smth
    then:
    { Mockito.verify(authenticationService).authenticate(Matchers.any(Message)) }
  }

It is really hard to read :(
I hope, that in future a can write verify code for spring mocks and spies like this: 1 * authenticationService.authenticate(_) >> null instead of code above
It is possible? Thanks for read!

Most helpful comment

No Mockito support is planned since Spock has its own built in mocks, but in the upcoming 1.1 release you can create Spock mocks outside of a Specification see https://github.com/spockframework/spock/blob/master/spock-spring/src/test/resources/MockExamples-context.xml for XML and https://github.com/spockframework/spock/blob/master/spock-spring/src/test/groovy/org/spockframework/spring/MockInjectionWithEmbeddedConfig.groovy for the Java Config version.

You can try it in the already released 1.1-rc1

All 3 comments

No Mockito support is planned since Spock has its own built in mocks, but in the upcoming 1.1 release you can create Spock mocks outside of a Specification see https://github.com/spockframework/spock/blob/master/spock-spring/src/test/resources/MockExamples-context.xml for XML and https://github.com/spockframework/spock/blob/master/spock-spring/src/test/groovy/org/spockframework/spring/MockInjectionWithEmbeddedConfig.groovy for the Java Config version.

You can try it in the already released 1.1-rc1

@leonard84 thanks. It`s good news! I hope, in one day, i use only @SpyBean (Or Spock analog) and get a native Spock mock/spy bean instead of Mockito stub.
Did you discuss it with spring-boot team?

Closed in favor of #624

Was this page helpful?
0 / 5 - 0 ratings