如何将Spring存储库存根以在集成测试中抛出异常?

问题描述 投票:1回答:2

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

@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 {...}
java spring spring-boot spock spring-test
2个回答
0
投票

您可以像下面一样创建test configuration,并在调用某个函数时使用Spock记录,然后将抛出ex。当然,在测试类中使用@Inject or @Autowire ...并且做@Import([IntegrationTestMockingConfig])

@TestConfiguration
class IntegrationTestMockingConfig {

     private DetachedMockFactory factory = new DetachedMockFactory()

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.