【问题标题】:Junit 5 @SpringBootTest executable jarJunit 5 @SpringBootTest 可执行jar
【发布时间】:2020-03-21 16:31:50
【问题描述】:

我正在尝试使用我的测试创建一个可执行 jar。我不能使用 maven 来运行测试,所以我正在尝试创建一个执行它们的 jer。

我有一个包含所有测试和所有依赖项的 jar。但是,当我尝试运行时:

java -jar target/tests-0.0.1-SNAPSHOT-spring-boot.jar

我得到-

Exception in thread "main" java.lang.ClassNotFoundException: com.xxx.xx.tests.framework.SDAutomation
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:93)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

pom.xml 包含以下内容以创建 jar:

  <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <classifier>spring-boot</classifier>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals> <goal>single</goal> </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.xxx.xx.tests.framework.SDAutomation</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

任何帮助都会很棒。

编辑 1: 通过使用 LauncherFactory 创建一个 main 找到了一个选择 例如:

        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                                                   .selectors(
                                                           selectClass(SDAutomation.class)
                                                   )
                                                   .build();

        Launcher launcher = LauncherFactory.create();
        // Register a listener of your choice
        SummaryGeneratingListener listener = new SummaryGeneratingListener();
        launcher.registerTestExecutionListeners(listener);

        launcher.execute(request);
        TestExecutionSummary summary = listener.getSummary();

取自juint doc

【问题讨论】:

  • 为什么不能使用 maven 运行测试?
  • 需要运行代码的机器没有 maven,也无法访问 repos。
  • 啊...你必须在不同的机器上运行你的单元测试/集成测试?这听起来你做错了什么......问题是:构建/运行单元测试和运行应用程序生产之间的区别?你好像弄错了什么?或者我不明白这里的东西......
  • 是的。我知道这听起来很奇怪。但我使用 junit 进行 E2E 测试,因为我重用集成测试框架来发送、监控和自动构建测试场景。测试在多个系统上运行,smoe 确实有连接和完整的 maven,有些是隔离设置的,机器上只有 java。我们首先在具有 maven 的系统中运行测试,如果所有测试都通过了,则打包完整的 jar,这样我们就可以使用我们提供的 PIPELINE 来运行测试 JAR。
  • 我遇到了类似的问题。 @aviramshterebaum 你找到解决方案了吗?

标签: java maven junit5 spring-boot-test


【解决方案1】:

Spring boot 可以与真正不是 Jar 的 Jar 一起使用。当你使用spring-boot-maven- plugin 时——你指示maven 准备这样一个spring boot jar。我建议用某种 WinRar/WinZip 应用程序打开这个 jar,看看里面到底是什么。

因此,如果您有一个 spring boot jar 并尝试将其作为 spring boot 应用程序运行,它将加载主类并运行它。

这个主类应该以编程方式运行所有自动化测试,如果我理解正确,这正是 SDAutomation 应该做的。

但是你使用 maven 程序集插件并尝试编辑它的清单并指定主插件 - 不要这样做。而是在 spring boot 插件中指定主类。所以这显然是一个错误

第三步更让人困惑——你创建一个测试jar,这很酷,但是你在哪里使用它呢?

那么如何真正解决这个问题:

  1. 您应该决定是否要使用 Spring Boot 来运行测试。坦率地说,我没有看到这样做的意义——你可以创建一个包含所有依赖项的胖 jar 并从那里运行测试。除非这些测试没有设计为 @SpringBootTest,这对于自动化测试也没有意义,原因如下:

Spring Boot Test 是一个集成框架,而不是通常涵盖端到端流程的自动化测试。自动化测试作为应用程序在不同的 JVM 中运行,这与 Spring Boot 测试相反,Spring Boot 测试提供了许多便利功能,用于在测试内部(或应用程序上下文的一部分)加载真实应用程序的应用程序上下文。自动化测试根本不应该加载任何生产代码...如果您能详细说明自动化测试套件的目的,我可能会提供更多信息。

  1. 即使你运行 spring boot 应用程序,也不应该是运行生产代码的同一个 spring boot 应用程序。可能您应该创建另一个 maven 模块,使用测试分类器提供对工件的依赖,并在 main 方法中使用控制台应用程序(同样,在这种情况下不需要 Web 服务器)来运行测试套件。

  2. 最后但并非最不重要。 maven 的 Surefire/failsafe 插件除了实际运行测试外,还生成可以与 CI 工具集成的报告。由于您那里没有任何 maven / surefire(来自您的评论),您将无法从这些报告中受益。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-17
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 2022-12-05
    相关资源
    最近更新 更多