Spring Boot 测试多模块 maven 应用程序

D3n*_*nsk 5 integration-testing maven-module spring-boot

我有一个使用 Spring Boot 的多模块 maven 应用程序:

- spring boot parent
    - myproject parent (both parent and module pom)
        - module1
        - module2
        - module-it (integration tests)
Run Code Online (Sandbox Code Playgroud)

在我的 module-it 中,我将其他模块添加到我的依赖项中,并按如下方式配置 maven-failsafe-plugin:

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

当我用 maven 构建我的项目时,我得到“构建成功”:

mvn clean install
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好。
但是我希望我的每个模块在构建结束时都是一个可执行的 jar。使用上述设置,清单未定义且 jar 不可执行。为了解决这个问题,我在 module1 和 module2 pom 文件中添加了以下内容:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

有了这个设置,我的 jar 文件是可执行的,但我不能再构建了。我在我的模块中使用的类 - 它没有找到。

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project module-it: Compilation failure: Compilation failure:
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/greeting/GreetingControllerIT.java:[20,17] cannot find symbol
[ERROR] symbol:   class GreetingController
[ERROR] location: class com.mycompany.testing.greeting.GreetingControllerIT
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/hello/HelloControllerIT.java:[20,17] cannot find symbol
[ERROR] symbol:   class HelloController
[ERROR] location: class com.mycompany.testing.hello.HelloControllerIT
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/greeting/GreetingControllerIT.java:[16,27] cannot find symbol
[ERROR] symbol: class GreetingController
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/hello/HelloControllerIT.java:[16,27] cannot find symbol
[ERROR] symbol: class HelloController
Run Code Online (Sandbox Code Playgroud)

我的测试具有以下形式(HelloControllerIT.java 同上):

@RunWith(SpringRunner.class)
@SpringBootTest(classes = GreetingController.class)
public class GreetingControllerIT {

    @Autowired
    private GreetingController greetingController;

    @Test
    public void getIT_OK() throws Exception {

        Assert.assertEquals("My greetings", greetingController.getStr());
    }
}
Run Code Online (Sandbox Code Playgroud)

Controllers 有这种形式(getStr() 方法只是为了测试配置):

@RestController
public class GreetingController {

    public String getStr() {
        return "My greetings";
    }

    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public String get() {
        return "greeting";
    }
}
Run Code Online (Sandbox Code Playgroud)

你能帮我理解为什么 spring-boot-maven-plugin 使我的构建失败以及我如何解决这个问题吗?

我知道:

  • 我的测试现在不是集成测试。这只是一个例子;
  • 根据我在 stackoverflow 上找到的答案,我可以添加一个文件,例如下面的文件,然后使用 @ContextConfiguration 但我尝试过并且它不会更改 Maven 构建的输出。
@Configuration
@ComponentScan(basePackages = "com.mycompany.testing")
public class AppConfig {
}
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助。

D3n*_*nsk 4

为了解决这个问题,我们可以添加一个分类器,如文档自定义重新打包分类器中所述

然后插件就变成了:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)