Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Maven没有';运行集成测试时,不要初始化spring上下文属性_Java_Spring_Maven_Spring Boot - Fatal编程技术网

Java Maven没有';运行集成测试时,不要初始化spring上下文属性

Java Maven没有';运行集成测试时,不要初始化spring上下文属性,java,spring,maven,spring-boot,Java,Spring,Maven,Spring Boot,问题:从maven(mvn验证)运行集成测试时,spring应用程序上下文没有正确初始化,它没有考虑我的自定义ApplicationContextInitializer类 测试类: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {MainApplication.class}, initializers = CustomContextInitializer.class) @WebI

问题:从maven(mvn验证)运行集成测试时,spring应用程序上下文没有正确初始化,它没有考虑我的自定义ApplicationContextInitializer类

测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MainApplication.class}, initializers = CustomContextInitializer.class)
@WebIntegrationTest
public class ApplicationIT {

    // Running a SOAPUI suite as a JUnit Test
    @Test
    public void TestGateway() throws Exception {
        SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
        runner.setProjectFile("../gateway/src/test/resources/soapui/gateway-soapui.xml");
    runner.run();
}
@Configuration
@ComponentScan(basePackages = {
    // different packages here (not relevant)
})
@EnableAutoConfiguration
public class MainApplication {

    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder(MainApplication.class)
                .initializers(new CustomContextInitializer())
                .run(args);
    }
}
}

main应用程序类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MainApplication.class}, initializers = CustomContextInitializer.class)
@WebIntegrationTest
public class ApplicationIT {

    // Running a SOAPUI suite as a JUnit Test
    @Test
    public void TestGateway() throws Exception {
        SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
        runner.setProjectFile("../gateway/src/test/resources/soapui/gateway-soapui.xml");
    runner.run();
}
@Configuration
@ComponentScan(basePackages = {
    // different packages here (not relevant)
})
@EnableAutoConfiguration
public class MainApplication {

    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder(MainApplication.class)
                .initializers(new CustomContextInitializer())
                .run(args);
    }
}
CustomContextinitilizer类(用于将自定义.properties文件添加到spring环境应用程序上下文):

公共类CustomContextInitializer实现ApplicationContextInitializer{
@凌驾
public void初始化(ConfigurableApplicationContext applicationContext){
ConfigurableEnvironment=applicationContext.getEnvironment();
试一试{
Resource[]res=new-PathMatchingResourcePatternResolver().getResources(“classpath*:/*.properties”);
(资源:res){
env.getPropertySources().addFirst(新的ResourcePropertySource(re));
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
结果:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MainApplication.class}, initializers = CustomContextInitializer.class)
@WebIntegrationTest
public class ApplicationIT {

    // Running a SOAPUI suite as a JUnit Test
    @Test
    public void TestGateway() throws Exception {
        SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
        runner.setProjectFile("../gateway/src/test/resources/soapui/gateway-soapui.xml");
    runner.run();
}
@Configuration
@ComponentScan(basePackages = {
    // different packages here (not relevant)
})
@EnableAutoConfiguration
public class MainApplication {

    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder(MainApplication.class)
                .initializers(new CustomContextInitializer())
                .run(args);
    }
}
1) 当我启动并运行应用程序时(从IDE或通过调用mvn exec),一切正常

2) 从IDE启动时,集成测试运行正常


3) 集成测试在通过maven verify调用时抛出错误,因为自定义属性文件未加载到spring上下文环境中。结果与我不会在测试类中编写initializers=CustomContextInitializer.class并尝试从IDE运行测试的结果相同

我认为您的代码是正确的,但是您的
.properties
文件可能位于错误的位置。确保它们位于
/src/main/resources
下,或者您已经在maven中配置了自定义资源文件夹。如果它们位于
/src/main/java
下,就maven而言,它们将不属于类路径的一部分。

我认为您的代码是正确的,但是您的
.properties
文件可能位于错误的位置。确保它们位于
/src/main/resources
下,或者您已经在maven中配置了自定义资源文件夹。如果它们位于
/src/main/java
下,就maven而言,它们将不属于类路径的一部分。

您可以共享您的文件结构吗?我对您的属性文件的位置很感兴趣。是否要针对生产属性文件进行测试?或者您应该创建在集成测试期间使用的测试属性吗?您可以共享您的文件结构吗?我对您的属性文件的位置很感兴趣。是否要针对生产属性文件进行测试?或者您应该创建在集成测试期间使用的测试属性吗?属性文件位于src/main/resources中的每个子模块中。maven构建之后,它们被移动到target/classes/中。主要问题似乎是在使用maven verify执行时类路径不同,它不再指向每个模块,而是指向一个临时创建的jar:/C:/ws/project/starter/target/surefire/surefirebooter680787691996319825.jar,它很可能不包含my.properties文件。您是否正确声明了模块之间的依赖关系?是的,依赖关系正常,starter模块(其中包含集成测试)与所有其他模块都有依赖关系。能否尝试将属性文件临时复制到starter模块的src/main/resources,然后查看它是否正常工作?还可以尝试从测试中打印类路径,以查看是否可以在那里发现奇怪的东西…我将尝试一下。同时,我已设法找到一个加载的解决方法我的属性文件来自所有其他模块。删除CustomContextInitializer类,并在每个子模块中创建一个带有“@PropertySources”的“@Configuration”类指向该模块属性。该类为空,仅与两个注释一起使用。这很好,因为每个模块仅声明其自己的PropertySources文件,并且模块之间没有硬编码关系。属性文件位于src/main/resources中的每个子模块中。maven构建后,它们在target/classes/.M中移动问题似乎在于,在使用maven verify执行时,类路径不同,它不再指向每个模块,而是指向一个临时创建的jar:/C:/ws/project/starter/target/surefire/surefirebooter680787691996319825.jar,它很可能不包含my.properties文件。您是否声明了模块之间的依赖关系es正确?是的,依赖关系正常,启动机模块(包含集成测试)与所有其他模块都有依赖关系。能否尝试将属性文件临时复制到starter模块的src/main/resources,然后查看它是否正常工作?还可以尝试从测试中打印类路径,以查看是否可以在那里发现奇怪的东西…我将尝试一下。同时,我已设法找到一个加载的解决方法我的属性文件来自所有其他模块。删除CustomContextInitializer类,并在每个子模块中创建一个带有“@PropertySources”的“@Configuration”类指向该模块属性。该类为空,仅与两个注释一起使用。这很好,因为每个模块只声明自己的PropertySources文件,并且模块之间没有硬编码关系。