首页 文章

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

提问于
浏览
1

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

如果他们不包括启动器,我如何测试jar项目?

Example JUnit test case header:

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

3 回答

  • 8

    我认为每个模块都应该提供上下文测试,因此您可以在早期找到有关线路和配置的问题,而不是依靠完整的应用程序测试来查找它们 .

    我在同一个模块中使用测试应用程序类解决了这个问题 . 确保此主要类位于 test 目录中 .

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

    你的上下文现在应该工作 .

    @RunWith(SpringRunner.class)
    //@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
    //@TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
    //@ContextConfiguration()
    @ActiveProfiles(profiles = {Profiles.WEB_REST})
    //@TestPropertySource("/config/rest.yml")
    @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());
        }
    }
    
  • 3

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

    • 带有域和实用程序类的"lib"项目,

    • a "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”项目的测试源中,我有一个充当我的测试配置的类

    package my.pack.utils;
    
        import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
        import org.springframework.boot.orm.jpa.EntityScan;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    
        @Configuration
        @EnableJpaRepositories(basePackages = {"my.pack.engine.storage", "my.pack.storage"})
        @EntityScan(basePackages = {"my.pack.storage", "my.pack.entity"})
        @EnableAutoConfiguration
        public class TestConfiguration
        {
    
        }
    

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

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

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

    问题是

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

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

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

    如果您阅读SpringApplicationConfiguration's javadoc

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

    它被认为是集成测试 .

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

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

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

相关问题