繁体   English   中英

Spring Boot Maven 集成测试给出 ClassNotFoundException

[英]Spring Boot Maven Integration Test Giving ClassNotFoundException

我有一个带有两个应用程序的 Spring Boot 项目,一个在 src/test 中,一个在 src/main 中。 我有两个应用程序,一个用于连接 SOAP 端点的中介程序,另一个充当用于测试的模拟服务器。

我有一个集成测试,检查它是否会命中(需要测试应用程序(端口 9119)和主应用程序(端口 28433)启动)

我使用 maven spring boot 插件来配置在我的 POM 中运行这两个应用程序,并在运行 mvn verify 时跳过单元测试。

<plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
             <includePom>true</includePom>          
        </configuration>  
            <executions>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
               <mainClass>com.nulogix.billing.App</mainClass>   
                    </configuration> 

                </execution>
                <execution>
                    <id>pre-integration-test2</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
               <mainClass>com.nulogix.billing.mockserver.MockServerApp</mainClass>   
                    </configuration> 

                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals>

                         <goal>stop</goal>                  
                    </goals>
                     <configuration>
               <mainClass>com.nulogix.billing.App</mainClass>   
                    </configuration>

                </execution>
                <execution>
                    <id>post-integration-test2</id>
                    <goals>

                         <goal>stop</goal>                  
                    </goals>
                     <configuration>
               <mainClass>com.nulogix.billing.mockserver.MockServerApp</mainClass>   
                    </configuration>

                </execution>
            </executions>

        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.18.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>

当我运行 mvn verify 它在预集成阶段加载 28433 但当它尝试在预集成阶段 2 运行 9119 时,它会出现此错误:

java.lang.ClassNotFoundException: com.nulogix.billing.mockserver.MockServerApp
    at java.net.URLClassLoader.findClass (URLClassLoader.java:436)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:588)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:521)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:553)
    at java.lang.Thread.run (Thread.java:835)

类路径目录是对的。 我尝试在我的 pom 中添加一个类范围,但它似乎没有解决问题并给了我同样的错误。

在后集成阶段,当它关闭时,它给出了这个错误,我认为这只是意味着它无法加载应用程序上下文,因此没有找到可以关闭它的 bean

Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.0.RELEASE:stop (post-integration-test2) on project billing_mediator: Spring application lifecycle JMX bean not found (fork is null). Could not stop application gracefully: org.springframework.boot:type=Admin,name=SpringApplication -> [Help 1]

当我运行 mvn -x 进行堆栈跟踪时,它会自动给我一个构建失败并给我这个

org.apache.maven.lifecycle.NoGoalSpecifiedException: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy.

这是否意味着我的 POM 设置错误?

由于 maven 不扫描 src/test 文件夹,因此我通过在我的主应用程序中仅添加一个集成阶段来实现此目的。 然后我创建了我的模拟服务器的应用程序上下文并让它在测试类之前运行。

<plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
            <mainClass>com.nulogix.billing.App</mainClass> 
        </configuration>
            <executions>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>

        </plugin>
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {EndPointTestConfiguration.class
})


public class SoapIT {
private static ApplicationContext context;
    @BeforeClass
    static public void  setup(){
        SpringApplication springApplication = new SpringApplicationBuilder()           
                .sources(MockServerApp.class)
                .build();
        context = springApplication.run();
    }


    @Autowired
    private String studyDetailDemo;
    @Test
    public void soapTest() throws ClientProtocolException, IOException {
        String result = Request.Post("https://127.0.0.1:28433/nulogix/ws/billingtool")
                .connectTimeout(2000)
                .socketTimeout(2000)
                .bodyString("testing", ContentType.TEXT_PLAIN)
                .execute().returnContent().asString();

    }
}

这将使我的主应用程序运行预集成测试,然后在测试运行之前启动我的模拟服务器应用程序。 因此,这会在集成测试之前启动两个应用程序并触发我的肥皂调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM