当用于测试时,Spring Boot似乎没有拿起Atomikos

时间:2014-12-17 06:45:37

标签: spring-boot jta atomikos


  我正在研究在我们的项目中使用Spring Boot的原型。我们正在生产JBoss服务器,我正在考虑使用像Atomikos这样的嵌入式事务管理器对Undertow嵌入式服务器运行集成测试,因为存在我必须重用的persistence.xml。我的测试应用程序上下文文件包含以下行:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@EnableAutoConfiguration
@IntegrationTest("server.port:0")
@ActiveProfiles("test")
public abstract class TestApplicationContext  {
...
}

我还添加了自定义测试配置:

@Configuration
public class TestConfiguration {
    @Value("${spring.jpa.hibernate.dialect}")
    private String dialectClassName;

    @Value("${spring.jpa.hibernate.transaction.manager_lookup_class}")
    private String transactionManagerClass;

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        return new UndertowEmbeddedServletContainerFactory(9000);  // Don't know if this can be avoided using some properties
    }

    @Bean
    @ConfigurationProperties(prefix = DataSourceProperties.PREFIX)
    public DataSource dataSource() throws Exception {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
                                                                       DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean =
                builder.dataSource(dataSource).persistenceUnit("main").build();
        Properties additionalProperties = new Properties();
        additionalProperties.put("hibernate.dialect", dialectClassName);
        additionalProperties.put("hibernate.transaction.manager_lookup_class", transactionManagerClass);
        entityManagerFactoryBean.setJpaProperties(additionalProperties);
        return entityManagerFactoryBean;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        // this should not be needed if I have included Atomikos but it seems to pick 
        // JPA Transaction manager still and fails with the famous NullPointerException at
        // CMTTransaction class - because it cannot find a JTA environment
        // return new JtaTransactionManager(userTransaction, transactionManager);
    }
}

我的Graom包括Atomikos:

testCompile('org.springframework.boot:spring-boot-starter-jta-atomikos')

我正在使用Spring Boot 1.2.0-RC2。

有人指出我做错了什么或如何解决这个问题?

谢谢,
稻谷

0 个答案:

没有答案