Soap 集成测试仅加载被测试的端点而不是所有端点
Soap integration tests load only the tested endpoint not all Endpoints
我有一个问题:
仅加载我想在该集成测试中测试的 Enpoint class 而不是完整的应用程序上下文(不是所有 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 模块中,我有一个服务模块的依赖项,它也有其他依赖项等等。
如果我加载整个 ApplicaitonContext 那么:
- 我是否需要模拟我在 Soap 模块中使用的完整服务列表
- 或者模拟服务模块的底层依赖,比如DataSource、Queues等
如果我这样做,第二个将使 Soap 模块知道它不应该知道的事情。
如果我首先这样做,我将被迫模拟并在配置测试文件中维护可能很长的已用服务的完整列表。
这里有什么建议吗?
what would be the right configuration to load only the Endpoint class that I want to test in that integration test and not the full Application context
您可以要求 spring 只实例化特定的 Controller
class 但不加载完整的应用程序上下文 @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
进行端到端集成测试
经过长时间的搜索和试验,我找到了解决方案。
正如您可以要求 Spring 在 REST 上仅将 1 个控制器加载到
的上下文中
@WebMvcTest(MyController.class)
您可以使用
为 Soap 做同样的事情
@SpringBootTest(classes = {MyEndpoint.class})
它将只加载你想要的端点,你可以模拟你在其中使用的服务,或者你可以一直走到存储库 o 无论你的应用程序业务逻辑在那里做什么。
我有一个问题: 仅加载我想在该集成测试中测试的 Enpoint class 而不是完整的应用程序上下文(不是所有 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 模块中,我有一个服务模块的依赖项,它也有其他依赖项等等。 如果我加载整个 ApplicaitonContext 那么:
- 我是否需要模拟我在 Soap 模块中使用的完整服务列表
- 或者模拟服务模块的底层依赖,比如DataSource、Queues等
如果我这样做,第二个将使 Soap 模块知道它不应该知道的事情。 如果我首先这样做,我将被迫模拟并在配置测试文件中维护可能很长的已用服务的完整列表。
这里有什么建议吗?
what would be the right configuration to load only the Endpoint class that I want to test in that integration test and not the full Application context
您可以要求 spring 只实例化特定的 Controller
class 但不加载完整的应用程序上下文 @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
进行端到端集成测试
经过长时间的搜索和试验,我找到了解决方案。 正如您可以要求 Spring 在 REST 上仅将 1 个控制器加载到
的上下文中@WebMvcTest(MyController.class)
您可以使用
为 Soap 做同样的事情@SpringBootTest(classes = {MyEndpoint.class})
它将只加载你想要的端点,你可以模拟你在其中使用的服务,或者你可以一直走到存储库 o 无论你的应用程序业务逻辑在那里做什么。