肥皂集成测试仅加载测试的端点,而不是所有端点

时间:2020-01-28 17:01:23

标签: java spring-boot soap spring-test spring-ws

我有一个疑问: 什么是正确的配置以仅加载我要在该集成测试中测试的Enpoint类,而不加载整个应用程序上下文(不是所有Enpoint类)? 现在我有:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebServiceConfigTest.class}, properties = {"application.address=http://hostname:port/context"})
public class MyEndpointTest {

@Autowired
private ApplicationContext applicationContext;

private MockWebServiceClient mockClient;


@Before
public void init() {
    mockClient = MockWebServiceClient.createClient(applicationContext);
    MockitoAnnotations.initMocks(this);
}
@Test
public void test1(){}
....
}
WebServiceConfigTest中的

是:

@ComponentScan("mypackages.soap")
@ContextConfiguration(classes = SoapApplication.class)
@MockBean(classes = {MyService.class})
public class WebServiceConfigTest {
}

SoapApplication是:

@ComponentScan({"mypackages"})
@SpringBootApplication
public class SoapApplication extends SpringBootServletInitializer implements WebApplicationInitializer {

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

原因是在Soap模块中,我具有Service模块的依赖关系,而Service模块也具有其他依赖关系,依此类推。 如果我加载整个ApplicaitonContext,则:

  • 我要么需要模拟在Soap模块中使用的服务的完整列表,要么
  • 或模拟Service模块的底层依赖关系,例如DataSource,Queues等。

如果我第二次这样做,将会使Soap模块意识到它不应该知道的事情。 如果我第一次这样做,则必须模拟并在config测试文件中维护使用的服务的完整列表,该列表可能很长。

这里有什么建议吗?

2 个答案:

答案 0 :(得分:1)

什么是正确的配置,使其仅加载要在该集成测试中测试的Endpoint类,而不加载整个Application上下文

您可以要求spring仅实例化特定的Controller类,而不能通过使用@WebMvcTest(MyEndpoint.class)加载完整的应用程序上下文

@RunWith(SpringRunner.class)
@WebMvcTest(MyEndpoint.class)
public class MyEndpointTest {

 @MockBean //mock all service beans that are injected into controller
 private Service service;

 @Autowired
 private MockMvc mockMvc;

   @Test
   public void test1(){}
      ....
   }
}

如果您使用嵌入式数据库(例如H2)或嵌入式队列进行测试,我还建议使用@SpringBootTest进行端到端集成测试

答案 1 :(得分:0)

经过长时间的搜索和试验,我找到了解决方案。 正如您可以要求REST上的Spring在

中仅将1个Controller放到上下文中一样
@WebMvcTest(MyController.class) 

您可以用同样的香气

@SpringBootTest(classes = {MyEndpoint.class}) 

它将仅加载您想要的端点,您可以模拟在其中使用的服务,也可以一直进行到存储库,无论您的应用程序业务逻辑在其中做什么。

相关问题