Spring启动测试多模块maven应用程序

时间:2016-10-01 12:28:15

标签: spring-boot integration-testing maven-module

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

- spring boot parent
    - myproject parent (both parent and module pom)
        - module1
        - module2
        - module-it (integration tests)

在我的模块中,我在依赖项中添加了其他模块,并按如下方式配置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>

当我使用maven构建项目时,我获得了“Build Success”:

mvn clean install

到目前为止一切顺利 然而,我希望我的每个模块在构建结束时都是可执行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>

使用此设置,我的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

我的测试具有以下形式(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());
    }
}

控制器有这种形式(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";
    }
}

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

我知道:

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

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:4)

要解决此问题,我们可以按照文档custom repackage classifier

中的说明添加分类器

然后该插件变为:

{{1}}

答案 1 :(得分:0)

您还可以将 repackage 目标参数 attach 设置为false:

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <attach>false</attach>
                    </configuration>
                </execution>
            </executions>
        </plugin>