gpt4 book ai didi

maven - mvn spring boot 插件破坏集成测试

转载 作者:行者123 更新时间:2023-12-04 12:45:20 25 4
gpt4 key购买 nike

我使用 maven-failsafe-pluginJUnit5 的集成测试最初很难设置,但现在可以了:

plugins:
- artifactId: maven-failsafe-plugin
groupId: org.apache.maven.plugins
version: 2.19.1
dependencies:
- artifactId: junit-platform-surefire-provider
groupId: org.junit.platform
version: 1.0.3
- artifactId: junit-jupiter-engine
groupId: org.junit.jupiter
version: 5.1.1
configuration:
includes:
- v3api/*IntegrationTests.java
executions:
- goals: [ integration-test, verify ]

但是我想把 Artifact 变成一个 fat jar 子,看起来好像 spring-boot-maven-pluginmaven-failsafe-plugin 冲突,因为如果我将此配置放入我的 pom ...

  - artifactId: spring-boot-maven-plugin
groupId: org.springframework.boot
version: ${spring.boot.version}
executions:
- goals: [ repackage ]
configuration:
fork: true
executable: true
outputDirectory: ${project.build.directory}/binary

然后我在集成测试中得到了这个错误:

Jun 05, 2018 3:03:15 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
java.lang.NoClassDefFoundError: com/adam/api/DbAccess
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetPublicMethods(Class.java:2902)
at java.lang.Class.getMethods(Class.java:1615)
at org.junit.platform.commons.util.ReflectionUtils.getDefaultMethods(ReflectionUtils.java:1024)
at org.junit.platform.commons.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:997)
at org.junit.platform.commons.util.ReflectionUtils.findAllMethodsInHierarchy(ReflectionUtils.java:939)
at org.junit.platform.commons.util.ReflectionUtils.findMethods(ReflectionUtils.java:923)
at org.junit.platform.commons.util.ReflectionUtils.findMethods(ReflectionUtils.java:909)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveContainedMethods(JavaElementsResolver.java:189)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveChildren(JavaElementsResolver.java:177)
at java.lang.Iterable.forEach(Iterable.java:75)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveClass(JavaElementsResolver.java:61)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.lambda$resolve$3(DiscoverySelectorResolver.java:69)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolve(DiscoverySelectorResolver.java:68)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:50)
at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:61)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:130)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:117)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:82)
at org.junit.platform.surefire.provider.TestPlanScannerFilter.accept(TestPlanScannerFilter.java:50)
at org.apache.maven.surefire.util.DefaultScanResult.applyFilter(DefaultScanResult.java:98)
at org.junit.platform.surefire.provider.JUnitPlatformProvider.scanClasspath(JUnitPlatformProvider.java:121)
at org.junit.platform.surefire.provider.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:111)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
Caused by: java.lang.ClassNotFoundException: com.adam.api.DbAccess
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 28 more

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

maven spring boot docs他们描述了如何配置插件以启动和停止 spring boot 应用程序,以便在 pre-post-integration-testing 生命周期阶段进行集成测试。

我的应用程序的生命周期在 docker 和 kubernetes 方面略有不同。它是这样的:

  • 打包jar
  • 使用 spring boot 插件将 jar 变成一个 fat jar
  • 对接它
  • 将其安装到 docker registry
  • 部署到开发
  • 集成测试
  • 将构建从开发推广到测试
  • 部署测试

我已经尝试将 spring boot 插件绑定(bind)到 package 阶段,但这对错误没有影响。

除了 spring boot 生命周期阶段问题,它也可能与 JUnit5 有很大关系,因为 maven-failsafe-plugin 似乎接近不受 JUnit5 支持:How do I run JUnit 5 integration tests with the Maven Failsafe plugin?

我也在 Spring plus JUnit Jupiter 上看到过这个但它对集成测试没有帮助。

最佳答案

以下对我有用:

      <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
<executions>
<execution>
<!-- repackage will break the integration-test class-path, so schedule after -->
<phase>post-integration-test</phase>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

我可以通过以下方式运行集成测试:mvn verify

SpringBoot 通过以下方式启动:java -jar target/my-jar.jar

关于maven - mvn spring boot 插件破坏集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50705270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com