如何使用 Spring Boot 测试 Maven 模块项目

How to test Maven module project with Spring Boot

我已经将一个基于Spring Boot 的项目拆分成几个Maven 模块。现在只有 war-project 包含一个 starter class(有一个 main 方法,starting Spring),其他模块都是 jar.

类型

如果 jar 项目不包含启动程序,我该如何测试它们?

示例 JUnit 测试用例 header:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(StarterClassInDifferentProject.class)
...

问题是

How do I test the jar projects, if they don't include a starter?

我认为正确的答案是,您的 jar 子模块不应与 spring-boot 上下文联合测试。

事实上,即使不是全部,您的 jar 项目中的大多数测试甚至不应该使用 RunWith(Spring...) 它们应该是普通的或使用模拟库,例如 @RunWith(MockitoJUnitRunner.class).

如果你阅读 SpringApplicationConfiguration's javadoc:

Class-level annotation that is used to determine how to load and configure an ApplicationContext for integration tests.

它被认为是集成测试。

除此之外,您还可以使用 spring 上下文(而不是 spring-boot)在您的 jar 子模块中使用 'test spring configuration' 来启动您的测试。定义你的 beans/resources 并在你的测试中使用它。

@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(TestConfigInJarModule.class)

例如,我这样做是为了测试 Spring 数据存储库,使用测试 spring 配置(不依赖于 spring-boot)。

我解决了类似的情况。 我有一个包含两个模块的项目:

  1. 一个 "lib" 具有域和实用程序的项目 classes,
  2. 一个 "web" 项目,带有 spring 引导应用程序、模板、控制器等...

我想以 spring-boot-test 方式测试 "lib" 项目。

首先,在 pom.xml 中包含范围为 "test" 的所需依赖项(在我的例子中还有 H2 数据库):

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>1.3.3.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <!-- add also add this here, even if in my project it is already present as a regular dependency -->
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>1.3.3.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.191</version>
        <scope>test</scope>
    </dependency>

出于测试目的,在 "lib" 项目的测试源中,我有一个 class 作为我的测试配置

    package my.pack.utils;

    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.domain.EntityScan;
    import org.springframework.boot.test.context.TestConfiguration;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

    @TestConfiguration
    @EnableJpaRepositories(basePackages = {"my.pack.engine.storage", "my.pack.storage"})
    @EntityScan(basePackages = {"my.pack.storage", "my.pack.entity"})
    @EnableAutoConfiguration
    public class MyTestConfiguration
    {

    }

这将设置 H2 数据库以测试应用程序的数据访问功能

最后,只有在我发现它有用的测试 classes 中,我将执行配置为使用测试配置(我并不总是需要这样做,但有时它很方便):

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = MyTestConfiguration.class)
    public class TestAClassThatNeedsSpringRepositories
    {
        // tests...
    }

我认为每个模块都应该进行上下文测试,这样您就可以及早发现线路和配置问题,而不是依赖于完整的应用程序测试来找到它们。

我在同一模块中使用测试应用程序 class 解决了这个问题。 确保这个主要 class 在你的 test 目录中。

@SpringBootApplication
public class TestApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

您的上下文现在应该可以使用了。

@RunWith(SpringRunner.class)
@ActiveProfiles(profiles = {Profiles.WEB_REST})
@WebMvcTest(EntityController.class)
@DirtiesContext
public class ServicesControllerTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private Controller controller;

    @Test
    public void testAll() throws Exception {
        given(controller.process(null)).willReturn(null);

        mvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk());
    }
}