【问题标题】:How to test Maven module project with Spring Boot如何使用 Spring Boot 测试 Maven 模块项目
【发布时间】:2016-10-03 14:39:29
【问题描述】:

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

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

示例 JUnit 测试用例标头:

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

【问题讨论】:

    标签: java junit spring-boot maven-module


    【解决方案1】:

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

    我使用同一模块中的测试应用程序类解决了这个问题。 确保这个主类在你的 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());
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      1. 具有域和实用程序类的“lib”项目,
      2. 一个带有 Spring Boot 应用程序、模板、控制器等的“Web”项目...

      我想以 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”项目的测试源中,我有一个类作为我的测试配置

          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 数据库以测试应用程序的数据访问功能

      最后,只有在我觉得有用的测试类中,我才配置执行以使用测试配置(我并不总是需要这样做,但有时它很方便):

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

      【讨论】:

        【解决方案3】:

        问题是

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

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

        事实上,你的 jar 项目中的大多数测试甚至都不应该使用 RunWith(Spring...) 它们应该是 vanilla 或使用模拟库,例如 @RunWith(MockitoJUnitRunner.class)。

        如果你读到SpringApplicationConfiguration's javadoc

        类级注释,用于确定如何为集成测试加载和配置 ApplicationContext。

        它被认为是集成测试。

        除此之外,您还可以在您的 jar 子模块中使用带有“测试弹簧配置”的弹簧上下文(不是弹簧引导)启动您的测试。定义您的 bean/资源并在您的测试中使用它。

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

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

        【讨论】:

        • 顺便说一句,您仍然可以选择在测试范围内的子模块中包含 spring-boot 依赖项,并使用它进行单元测试!
        • 我不同意,我认为模块化应用程序应该逐个模块地测试,即使有应用程序上下文。您可以尽早发现配置问题,而无需等待完整的应用程序测试。
        • 我明白你的意思,我同意测试越多越好! (不管它们是单位,intg ...)。问题是,我们谈论的是“Spring boot”,大部分配置都在 application.properties 中,对子模块不可见。在子模块中使用 @SpringBootTest 将需要特定的测试设置,然后我不确定您是否真的在测试您的生产上下文。当您的生产失败时,您的“测试”上下文可能会很好地工作。请回复您对此的看法。
        • 我几乎总是从 SpringBoot 开始做的一件事是由内存数据库支持的持久层(spring data repo),所有设置都来自 application.properties。它允许我测试保存/查找......并且主要用于验证我的 jpa 模型的目的(任何其他持久性实现,如 mongodb、SOLR ......)。我开始使用 SpringBoot Boot 的主要原因是它的配置非常简单,但它根本不测试生产 spring conext。
        • 我们用配置文件划分我们的应用程序,我们在每个配置文件中包含配置元素,因此核心配置与核心一起使用,jpa 配置与 jpa 一起使用。 Spring 非常适合扫描我们的模块以查找配置类并选择所有必要的配置。在没有应用程序的情况下在模块上下文中进行测试的另一个原因。
        猜你喜欢
        • 2015-10-10
        • 1970-01-01
        • 2020-03-20
        • 1970-01-01
        • 2014-06-02
        • 2016-03-27
        • 1970-01-01
        • 1970-01-01
        • 2020-04-01
        相关资源
        最近更新 更多