【问题标题】:spring boot for unit test onlyspring boot 仅用于单元测试
【发布时间】:2018-07-19 03:57:57
【问题描述】:

我有一个使用带有 xml 的经典 spring 配置的应用程序,是否可以仅将 spring boot 用于单元测试?

像这样:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration(locations = { "classpath:security-context.xml", 
"classpath:persistence-context-test.xml", "classpath:core-context.xml", 
"classpath:web-context.xml" })
@EnableAutoConfiguration
public class SampleTomcatApplicationTests {

@Test
public void testHome() {

}
}

【问题讨论】:

    标签: unit-testing spring-boot


    【解决方案1】:

    好的,只需使用 Spring Boot 的 main 方法创建一个类,如下所示:

            @SpringBootApplication
            @ImportResource(locations = { "classpath:security-context.xml", "classpath:persistence-context-test.xml", "classpath:core-context.xml", "classpath:web-context.xml" })
            public class SimpleBootCxfSystemTestApplication {
    
                /**
                 * The main method.
                 *
                 * @param args
                 *            the arguments
                 */
                public static void main(String[] args) {
                    SpringApplication.run(SimpleBootCxfSystemTestApplication.class, args);
                }
            }
    

    并像这样更改类测试:

        @RunWith(SpringJUnit4ClassRunner.class)
        @SpringApplicationConfiguration(classes = SimpleBootCxfSystemTestApplication.class)
        @WebIntegrationTest("server.port:8080")
        @ActiveProfiles("test")
        @WithUserDetails(value = "testUser", userDetailsServiceBeanName = "utilisateurService")
        public class SampleTomcatApplicationTests {
    
        //autowired
        ...
        //test
        }
    

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 2016-05-28
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 2016-03-15
      • 2017-02-08
      • 2020-08-08
      相关资源
      最近更新 更多