简体   繁体   English

使用MockMVC进行集成测试时模拟非Spring管理的对象

[英]mock a non spring managed object while integration testing using mockMVC

I am writing integration test cases using MockMvc to test my REST API. 我正在使用MockMvc编写集成测试用例来测试我的REST API。

Within my implementation of the RESTAPI I am internally using RestTemplate(not directly from the controller but from within a util class which the controller calls) to call a 3rd party REST API. 在我实现RESTAPI的过程中,我在内部使用RestTemplate(不是直接从控制器而是从控制器调用的util类中)调用第三方REST API。 The RestTemplate which I use (to make the 3rd party rest API) is not a spring managed bean instead I am instantiating it as RestTemplate restTemplate = new RestTemplate(); 我使用的RestTemplate(用于使第三方rest API)不是Spring托管的bean,而是将其实例化为RestTemplate restTemplate = new RestTemplate();。

I want to mock the restTemplate call(postForEntity). 我想模拟restTemplate调用(postForEntity)。

I am trying the below approach: 我正在尝试以下方法:

My test class- 我的测试课-

@ContextConfiguration(locations = {
    "classpath:test-applicationContext.xml"
})
@WebAppConfiguration

public class MockMVCTest { 公共类MockMVCTest {

  private MockMvc mockMvc;
  private RestTemplate restTemplate

  @Autowired
  private WebApplicationContext webApplicationContext;

  @Before
  public void setUp() {
    if (!initalized) {
     mockMvc =   MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  restTemplate = (RestTemplate)webApplicationContext.getBean("restTemplate");

} }

@Test
public void demo() throws Exception {
 when(
  restTemplate.postForEntity(
    eq("thirdpartyuri"),
    any(HttpEntity.class),
    eq(MyClass.class))).thenReturn(myresponse);

mockMvc.perform(
  post("uriExposedbyme")
    .contentType(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON)
    .content(MY_PAYLOAD)).andExpect(status().isOk());
}

In my application-context I have the following mock defined: 在我的应用程序上下文中,我定义了以下模拟:

<bean id="restTemplate" class="org.mockito.Mockito" factory-method="mock"> 
    <constructor-arg value="org.springframework.web.client.RestTemplate" />        </bean>

But when I execute my test case the RestTemplate is getting mocked but when a call to RestTemplate happens during the execution the actual resttemplate is called instead of my mock resttemplate. 但是,当我执行测试用例时,RestTemplate会被模拟,但是在执行过程中发生对RestTemplate的调用时,会调用实际的resttemplate而不是我的模拟resttemplate。

Please suggest on how I can mock RestTemplate for my test case. 请建议我如何为我的测试案例模拟RestTemplate。

The util class instantiates its private RestTemplate, like you said: RestTemplate restTemplate = new RestTemplate();. util类实例化其私有RestTemplate,如您所说:RestTemplate restTemplate = new RestTemplate();。

This means that it will use it, not the one mocked in the test. 这意味着它将使用它,而不是在测试中嘲笑的那个。 You can either make RestTemplate a spring managed bean in your actual code, or have a setter method on the util class and call this setter in the test using the mocked rest template. 您可以在实际代码中使RestTemplate成为spring托管的bean,也可以在util类上使用setter方法,然后使用模拟的rest模板在测试中调用此setter。

Based on the information provided, I could say to try following changes and check whether it's resolved your issue. 根据提供的信息,我可以说尝试进行以下更改并检查是否解决了您的问题。 What I can see is since you autowire the WebApplicationContext as 我看到的是因为您将WebApplicationContext自动连接为

@Autowired private WebApplicationContext webApplicationContext;

It might possible the development profile getting injected, rather test profile. 开发配置文件可能会被注入,而不是测试配置文件。 Hence can you put this annotation to mark the class in Test profile at the top of the test class 因此,您是否可以将此注释放在测试类顶部的测试配置文件中标记该类

@RunWith(SpringJUnit4ClassRunner.class) 

Still if you have issues, auto wire the rest template correctly with your RestTemplate instance as below. 如果仍然有问题,请按如下所示自动将其余模板与RestTemplate实例自动连线。

@Autowired
@Qualifier("restTemplate")
private RestTemplate restTemplate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM