【问题标题】:How do I setup my project to run integration tests with Spring 3.2.4 version?如何设置我的项目以使用 Spring 3.2.4 版本运行集成测试?
【发布时间】:2020-04-22 04:48:27
【问题描述】:

我正在处理一个没有测试的遗留项目。我们想为我们的服务添加集成测试。 这些服务与其他服务以及数据库进行通信。

我可以处理运行 junit 测试,因此我可以模拟服务调用和数据库调用以返回我想要的任何内容,但我想知道是否可以运行实际的集成测试,它与实际的开发数据库和其他数据库进行通信项目中的服务也是如此。

bean 是在带有一些占位符的 xml 文件中定义的。 我正在寻找一个关于寻找什么的方向,以及这个春季版本是否可能。

谢谢!

【问题讨论】:

    标签: java spring spring-mvc junit


    【解决方案1】:

    要配置您的测试,您可以制作一流的 SwitchCase 并在测试类中扩展它。

    
    
    import java.util.Random;
    
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = Application.class)
    public class SwitchCase{
    
     *** here you can have tour variables and do @Autowired in yours service
    
    }
    
    
    public class YoursTestClass  extends SwitchCase{
    
        @Test
        public void save() throws Exception {
    
        }
    }
    
    

    要进行模拟,您可以使用 Mockito。

    @WebMvcTest
    @AutoConfigureMockMvc
    public class YouClass {
    
     @Autowired
     MockMvc mock
    
     MockHttpServelet request = MockMvcRequestBuilders
     .post("http/....")
     .contentTupe(**pass yout content type **);
    
     mock.perform(request)
     .andExpect(MockMvcResultMatchers.status().isCreated())
     .andExpect(MockMvcResultMatchers.jsonPath("id").value(7)
    }
    

    这只是一个指导示例,但最好看一下 mockito 文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 2020-02-08
      • 2017-06-23
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多