运行 spring 为每个模块在其自己的模块上启动单元测试

Running spring boot unit tests for every module on their own module

我们正在开发 Spring-boot 应用程序,我们在其中使用 Maven 进行依赖项管理。我们把项目分成几个 modules 作为不同的服务。

有一些疑惑,希望能在这里得到一些见解和解答。

<modules>
    <module>app</module>
    <module>core</module>
    <module>firstlibrary</module>
    <module>firstlibraryservice</module>
    <module>secondlibrary</module>
    <module>secondlibraryservice</module>
</modules>

应用 --> pom.xml

<dependencies>
    <dependency>
        <groupId>com.microservice</groupId>
        <artifactId>core</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.microservice</groupId>
        <artifactId>firstlibraryservice</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.microservice</groupId>
        <artifactId>firstlibrary</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.microservice</groupId>
        <artifactId>secondlibraryservice</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.microservice</groupId>
        <artifactId>secondlibrary</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0.1</version>
    </dependency>


</dependencies>

将项目拆分为独立模块的主要目的是提高整个系统的弹性和可扩展性。 我们将系统划分为不同的服务,对于每个服务,我们都有一个 base module 和一个 service module

Each base module can access other services based on its needs with maven dependency but no base module is allowed to directly access other base modules.

我们现在遇到的问题是,当我们在单个模块上进行错误修复或添加功能时,我们被迫在 app 模块上编写测试,该模块可以访问在中定义为依赖项的所有其他模块它的 pom maven 文件。

我认为我们在这里遇到了一个矛盾,因为使用微服务架构的主要原因是让我们开发和测试每个模块尽可能独立于其他模块,而不是在整个应用程序级别编写我们的测试。

现在因为每个模块只能访问其 pom.xml 依赖项中的模块,我们无法清理编译每个模块或在模块级别编写测试。

我的问题是 如何 运行 对每个模块在其自己的模块上进行单元测试 而不是在 app 模块上编写所有测试? (可以访问所有其他模块)

希望能在这里得到一些解答和建议

您的 POM 看起来很完美而且可以正常工作。看看这个博客post:

https://info.michael-simons.eu/2018/06/18/maven-use-junit-5-with-spring-boot-for-unit-and-integration-tests/

你也一样,你的项目一定运行没有问题,也许问题出在你项目的另一部分。

你可以用这个项目,和你的项目对比一下,找出问题所在:

https://github.com/GLinBoy/feader/tree/develop