如何在集成测试中对Spring存储库进行存根抛出异常?

时间:2018-09-06 12:40:17

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

我有一个服务,其中有一个存储库作为构造函数的参数。

@Autowired
NodeServiceListInstallService( final BykiListRepository aBykiListRepository )

BykiListRepository是没有实现的默认Spring存储库

interface BykiListRepository extends JpaRepository<BykiList, Long> {
    //some magic methods
}

我的配置类标记为@EnableJpaRepositories,因此,我没有指导bean的声明。服务声明:

@SpringBootApplication
@EnableConfigurationProperties( ApplicationProperties )
@EnableJpaRepositories
@EnableTransactionManagement
@ImportResource( 'classpath:META-INF/spring/application-context.xml' )
class Application extends SpringBootServletInitializer {
    @Bean
    NodeServiceListInstallService nodeServiceListInstallService( final BykiListRepository bykiListRepository ) {
        new NodeServiceListInstallService( bykiListRepository )
    }
}

我正在尝试在存储库的方法save的调用范围内编写测试,将引发异常PersistenceException。 我试图对存储库进行存根/间谍,并使用@TestConfiguration@Primary中将其声明为Bean,甚至实现接口。 但是我没有结果。

@TestConfiguration
class TestConfig {
    @Bean
    BykiListRepository bykiListRepository() {
        //return Spock's Spy/Stub or new BykiRepositoryBadImpl()
    }

测试:

@ContextConfiguration( classes = TestConfig )
class GlobalErrorHandlerIntegrationTest extends BaseFlowIntegrationTest {
    //test()
}

我在Groovy-2.4.12上写,并在Spock-1.1上写测试。 Spring Boot 1.5.4。 保留的变体是要使用方面,但是并没有我所希望的。 非常感谢您的帮助。

更新:DetachedMockFactory的用法:

配置:

@TestConfiguration

class DummyConfiguration {
    private final detachedFactory = new DetachedMockFactory()
    @Bean
    @Primary
    BykiListRepository bykiListRepository() {
        detachedFactory.Mock( BykiListRepository )
    }
}

测试的骨架:

@SpringBootTest( classes = DummyConfiguration )
@Import( [DummyConfiguration] )
@ContextConfiguration( classes = DummyConfiguration )
class GlobalErrorHandlerIntegrationTest extends BaseFlowIntegrationTest {
    @Autowired
    BykiListRepository bykiListRepositoryMock
    def 'exercise error handling'() {
        given: 'the failing repository'
        bykiListRepositoryMock.save( _ ) >> {
            throw new CannotCreateTransactionException()
        }
        when: 'the message is send to rabbit'
        rabbitOperations.send( configuration.rabbitExchangeName, '', msg )
    }
}

其中:

@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.NONE )
@ContextConfiguration( classes = Application )
class BaseFlowIntegrationTest extends AbstractIntegrationTest {...}

@Category( InboundIntegrationTest )
abstract class AbstractIntegrationTest extends Specification {...}

2 个答案:

答案 0 :(得分:0)

您可以如下创建test configuration,并在调用某些函数时使用Spock记录,然后将抛出ex。当然,请在测试课程中使用@Inject or @Autowire ...,然后执行@Import([IntegrationTestMockingConfig])

@TestConfiguration
class IntegrationTestMockingConfig {

     private DetachedMockFactory factory = new DetachedMockFactory()

     @Bean
     ExternalRepository externalRepository() {
         factory.Mock(ExternalRepository)
     }
}

答案 1 :(得分:0)

问题是因为我有错误的bean名称。我将其更改为bykiListRepositoryMock(而不是bykiListRepository),它已经解决了问题。