Spring集成测试中没有使用application.properties

时间:2015-09-16 14:28:13

标签: spring-boot spring-test

我在测试期间没有阅读application.propertiesapplication-test.properties的Spring启动存在问题。

这是我的测试类:

public class SignupRestControllerTest extends AbstractEnd2EndIntegrationTest {
 ...

这是AbstractEnd2EndIntegrationTest类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { TestEnd2EndIntegrationConfiguration.class })
@WebAppConfiguration
@ActiveProfiles(Profiles.TEST)// "test"
@Transactional
public abstract class AbstractEnd2EndIntegrationTest {

}

我可以看到我使用@SpringApplicationConfiguration所以我的测试应该使用Spring启动......

我使用gradle来运行测试:

  sourceSets {

        main {
            output.resourcesDir = output.classesDir
        }

        integrationTest {
            java.srcDirs = ['src/it/java']
            resources.srcDirs = ['src/it/resources', 'src/main/resources']
            compileClasspath = sourceSets.main.output + configurations.testRuntime
            runtimeClasspath = output + compileClasspath
        }
    }

   task integrationTest(type: Test) {
        description "Run the integration tests."
        testClassesDir = sourceSets.integrationTest.output.classesDir
        classpath = sourceSets.integrationTest.runtimeClasspath
        reports.html.destination = file("$reports.html.destination/integration")
        reports.junitXml.destination = file("$reports.junitXml.destination/integration")
    }

来自application.propertiessrc/main/resources):

database.hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

我得到例外:

 Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我刚刚遇到此问题,并通过将ConfigFileApplicationContextInitializer添加到@ContextConfiguration来解决此问题。在这种情况下得到的注释将是:

@SpringApplicationConfiguration(
classes = TestEnd2EndIntegrationConfiguration.class,
initializers = ConfigFileApplicationContextInitializer.class)