Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Mockito在客户端测试POST请求_Java_Spring_Unit Testing_Post_Mockito - Fatal编程技术网

Java 使用Mockito在客户端测试POST请求

Java 使用Mockito在客户端测试POST请求,java,spring,unit-testing,post,mockito,Java,Spring,Unit Testing,Post,Mockito,我想测试应该向“服务器”发送post请求的post方法(因此我想模拟来自服务器的响应并检查响应)。另外,我想测试响应是否在主体中包含http status OK。问:我应该如何处理mockito 我在客户端的Post方法(客户端): 测试: 现在我得到了这个错误: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ClientTest': Unsatis

我想测试应该向“服务器”发送post请求的post方法(因此我想模拟来自服务器的响应并检查响应)。另外,我想测试响应是否在主体中包含http status OK。问:我应该如何处理mockito

我在客户端的Post方法(客户端):

测试:

现在我得到了这个错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ClientTest': Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.web.client.RestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
首先是完整代码(解释如下):

您需要使用
@AutoConfigureMockMvc
注释。背后的目的是根本不启动服务器,而是只测试服务器下面的层,在该层中,Spring处理传入的HTTP请求并将其交给您的控制器。这样,几乎使用了整个堆栈,调用代码的方式与处理真实HTTP请求的方式完全相同,但不需要启动服务器。为此,我们将使用Spring的MockMvc,我们可以通过在测试类上使用
@AutoConfigureMockMvc
注释来请求为我们注入它

private MockRestServiceServer mockServer;
MockRestServiceServer是客户端REST测试的主要入口点。用于涉及直接或间接使用RestTemplate的测试。提供了一种设置预期请求的方法,这些请求将通过RestTemplate执行,并提供模拟响应以发送回,从而消除了对实际服务器的需要

mockServer.expect(once(), requestTo("www.example.com/endpoint1"))
    .andRespond(withSuccess());
这是设置模拟外部调用的地方。并设定期望值

this.mockMvc.perform(post("www.example2.com/example2endpoint")..

这是您实际对自己的端点(在控制器中定义的端点)进行rest/api调用的地方。Spring将访问您的端点,执行控制器/服务层中的所有逻辑,当涉及到实际在外部进行调用时,将使用您刚才定义的mockServer。这样,它就完全离线了。你从来没有打过真正的外线服务。此外,您将在同一mockMvc.perform方法上附加断言

根据您的客户机类别,建议进行以下更改,以使其更好地进行测试:

    //  class
    public class Client {

        /*** restTemplate unique instance for every unique HTTP server. ***/
        @Autowired
        RestTemplate restTemplate;

        public ResponseEntity<String> sendUser() {

        String url = "http://localhost:8080/user/add";

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

        User test = new User();
        test.setName("test");
        test.setEmail("a@hotmail.com");
        test.setScore(205);

        HttpEntity<User> request = new HttpEntity<>(test);

        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);

        if(response.getStatusCode() == HttpStatus.OK){
            System.out.println("user response: OK");
        }
        return response;
      }
  }
因此,在相应的测试中,我们也应该只测试函数正在做什么 由于连接由
restemplate
负责,并且您没有覆盖
restemplate
的任何工作,因此我们不应该为相同的问题执行任何操作。。。 而只是测试我们的代码/逻辑

最后,请确保导入看起来像:

诚然,导入将类似于:

import org.springframework.http.HttpEntity; 
import org.springframework.http.HttpHeaders; 
import org.springframework.http.HttpMethod; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.MediaType; 
import org.springframework.http.ResponseEntity; 
import org.springframework.web.client.RestTemplate;

希望这能有所帮助。

测试应该向服务器发送post请求的post方法,因此您想要的是真正的服务器调用而不是模拟?不,我想要模拟响应。这看起来像是不向服务器发送post请求,而是要模拟某个post请求的响应?是吗?是的,我想模拟此特定POST请求的POST响应,这些是一些有用的链接。如果遇到问题,建议尝试一下更新您的问题。
@ActiveProfiles(“test”)
:有此功能的意义是什么,它做了什么<代码>@Autowired private RestTemplate RestTemplate:resttemplate在我的客户机代码中(所以在不同的模块中),所以如何能够自动连接它?通常,您会有一些bean只用于测试。或者您可能有一些bean,它们对于不同的配置文件可能有不同的行为。因此,当您放置
@ActiveProfiles(“test”)
时,只有那些被标记为
@Profile(“test”)
的bean被实例化。此外,此
@ActiviveProfiles(“测试”)
将激活测试配置文件。其次,如果您已经在别处定义了RESTTemplatebean,那么就不必担心了。Spring将在这个类中为您自动连接它。如果它仍然不起作用,那么您可以提供一个具有RESTTemplatebean的类名,如下所示:
@SpringBootTest(classes=classname,其中包含resttemplate.class的定义))
@MoNigma尝试一下,而不是自动连接它,只需创建一个新的resttemplate实例。试试看,我已经更新了问题。请看一下我现在所拥有的,因为我现在有点困惑(我是mockito高级测试的新手)。嘿,VibrantVivek,我现在在执行addUser()方法时出错了,因为restTemplate是自动连接的,我真的不想用SpringBoot运行客户机类。因此,它给出了一个表示restTemplate为NuLL的错误。那么,我怎样才能让它在没有自动连线的情况下运行,并且仍然让测试工作?它不需要是Springboot应用程序就可以使用自动连线。相反,只有Springwebmvc应该这样做。让我们继续:
mockServer.expect(once(), requestTo("www.example.com/endpoint1"))
    .andRespond(withSuccess());
this.mockMvc.perform(post("www.example2.com/example2endpoint")..
    //  class
    public class Client {

        /*** restTemplate unique instance for every unique HTTP server. ***/
        @Autowired
        RestTemplate restTemplate;

        public ResponseEntity<String> sendUser() {

        String url = "http://localhost:8080/user/add";

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

        User test = new User();
        test.setName("test");
        test.setEmail("a@hotmail.com");
        test.setScore(205);

        HttpEntity<User> request = new HttpEntity<>(test);

        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);

        if(response.getStatusCode() == HttpStatus.OK){
            System.out.println("user response: OK");
        }
        return response;
      }
  }
@RunWith(MockitoJUnitRunner.class)
public class ClientTest {

  private String RESULT = "Assert result";

  @Mock
  private RestTemplate restTemplate;

  @InjectMocks
  private Client client;

  /**
   * any setting needed before load of test class
   */
  @Before
  public void setUp() {
    // not needed as of now
  }

  // testing an exception scenario
  @Test(expected = RestClientException.class)
  public void testSendUserForExceptionScenario() throws RestClientException {

    doThrow(RestClientException.class).when(restTemplate)
        .exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), any(Class.class));
    // expect RestClientException
    client.sendUser();
  }

  @Test
  public void testSendUserForValidScenario() throws RestClientException {

    // creating expected response
    User user= new User("name", "mail", 6609); 
    Gson gson = new Gson(); 
    String json = gson.toJson(user); 
    doReturn(new ResponseEntity<String>(json, HttpStatus.OK)).when(restTemplate)
        .exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), any(Class.class));
    // expect proper response
    ResponseEntity<String> response =
        (ResponseEntity<String>) client.sendUser();
    assertEquals(this.RESULT, HttpStatus.OK, response.getStatusCode());
  }
}
// we are getting URL , creating requestHeader
// finally creating HttpEntity<User> request 
// and then passing them restTemplate.exchange 
// and then restTemplate is doing its job to make a HTPP connection and getresponse...
// and then we are prinnting the response... somestuff 
import org.springframework.http.HttpEntity; 
import org.springframework.http.HttpHeaders; 
import org.springframework.http.HttpMethod; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.MediaType; 
import org.springframework.http.ResponseEntity; 
import org.springframework.web.client.RestTemplate;