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

我想测试应该向“服务器”发送post请求的post方法(因此我想模拟来自服务器的响应并检查响应)。另外,我想测试响应是否在正文中包含http状态OK。问题:我应该如何使用 mockito?


客户端中的“我的帖子”方法(客户端):


public class Client{

        public static void 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);


        RestTemplate restTemplate = new RestTemplate();

        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");

        }


      }

  }

另一个模块(服务器端)中的我的控制器:


@RestController

@RequestMapping("/user")

public class UserController

{

    @Autowired

    private UserRepository userRepository;


    @PostMapping("/add")

    public ResponseEntity addUserToDb(@RequestBody User user) throws Exception

    {

        userRepository.save(user);

        return ResponseEntity.ok(HttpStatus.OK);

    }

测试:


    @RunWith(SpringRunner.class)

@ActiveProfiles("test")

@SpringBootTest(classes = Client.class)

@AutoConfigureMockMvc

public class ClientTest

{


    private MockRestServiceServer mockServer;


    @Autowired

    private RestTemplate restTemplate; 


    @Autowired

    private MockMvc mockMvc;


    @Before

    public void configureRestMVC()

    {

        mockServer =

                MockRestServiceServer.createServer(restTemplate);

    }


    @Test

    public void testRquestUserAddObject() throws Exception

    {


        User user = new User("test", "mail", 2255);


        Gson gson = new Gson();


        String json = gson.toJson(user );


        mockServer.expect(once(), requestTo("http://localhost:8080/user/add")).andRespond(withSuccess());

}


摇曳的蔷薇
浏览 146回答 2
2回答

慕尼黑5688855

根据您的客户端类,希望提出以下更改建议,以使其更易于测试:&nbsp; &nbsp; //&nbsp; class&nbsp; &nbsp; public class Client {&nbsp; &nbsp; &nbsp; &nbsp; /*** restTemplate unique instance for every unique HTTP server. ***/&nbsp; &nbsp; &nbsp; &nbsp; @Autowired&nbsp; &nbsp; &nbsp; &nbsp; RestTemplate restTemplate;&nbsp; &nbsp; &nbsp; &nbsp; public ResponseEntity<String> sendUser() {&nbsp; &nbsp; &nbsp; &nbsp; String url = "http://localhost:8080/user/add";&nbsp; &nbsp; &nbsp; &nbsp; HttpHeaders requestHeaders = new HttpHeaders();&nbsp; &nbsp; &nbsp; &nbsp; requestHeaders.setContentType(MediaType.APPLICATION_JSON);&nbsp; &nbsp; &nbsp; &nbsp; requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));&nbsp; &nbsp; &nbsp; &nbsp; User test = new User();&nbsp; &nbsp; &nbsp; &nbsp; test.setName("test");&nbsp; &nbsp; &nbsp; &nbsp; test.setEmail("a@hotmail.com");&nbsp; &nbsp; &nbsp; &nbsp; test.setScore(205);&nbsp; &nbsp; &nbsp; &nbsp; HttpEntity<User> request = new HttpEntity<>(test);&nbsp; &nbsp; &nbsp; &nbsp; ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);&nbsp; &nbsp; &nbsp; &nbsp; if(response.getStatusCode() == HttpStatus.OK){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("user response: OK");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return response;&nbsp; &nbsp; &nbsp; }&nbsp; }然后对于上面的我们朱尼特作为:@RunWith(MockitoJUnitRunner.class)public class ClientTest {&nbsp; private String RESULT = "Assert result";&nbsp; @Mock&nbsp; private RestTemplate restTemplate;&nbsp; @InjectMocks&nbsp; private Client client;&nbsp; /**&nbsp; &nbsp;* any setting needed before load of test class&nbsp; &nbsp;*/&nbsp; @Before&nbsp; public void setUp() {&nbsp; &nbsp; // not needed as of now&nbsp; }&nbsp; // testing an exception scenario&nbsp; @Test(expected = RestClientException.class)&nbsp; public void testSendUserForExceptionScenario() throws RestClientException {&nbsp; &nbsp; doThrow(RestClientException.class).when(restTemplate)&nbsp; &nbsp; &nbsp; &nbsp; .exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), any(Class.class));&nbsp; &nbsp; // expect RestClientException&nbsp; &nbsp; client.sendUser();&nbsp; }&nbsp; @Test&nbsp; public void testSendUserForValidScenario() throws RestClientException {&nbsp; &nbsp; // creating expected response&nbsp; &nbsp; User user= new User("name", "mail", 6609);&nbsp;&nbsp; &nbsp; Gson gson = new Gson();&nbsp;&nbsp; &nbsp; String json = gson.toJson(user);&nbsp;&nbsp; &nbsp; doReturn(new ResponseEntity<String>(json, HttpStatus.OK)).when(restTemplate)&nbsp; &nbsp; &nbsp; &nbsp; .exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), any(Class.class));&nbsp; &nbsp; // expect proper response&nbsp; &nbsp; ResponseEntity<String> response =&nbsp; &nbsp; &nbsp; &nbsp; (ResponseEntity<String>) client.sendUser();&nbsp; &nbsp; assertEquals(this.RESULT, HttpStatus.OK, response.getStatusCode());&nbsp; }}基本上在您的功能中,我们正在做:sendResponse()// we are getting URL , creating requestHeader// finally creating HttpEntity<User> request&nbsp;// and then passing them restTemplate.exchange&nbsp;// and then restTemplate is doing its job to make a HTPP connection and getresponse...// and then we are prinnting the response... somestuff&nbsp;因此,在相应的测试中,我们还应该只测试函数正在执行的操作,因为连接正在被照顾,并且您没有覆盖任何工作,因此我们不应该为相同的事情做任何事情......而只是测试我们的代码/逻辑。restTemplaterestTemplate最后,为了确保导入看起来像:可以肯定的是,进口会像这样:import org.springframework.http.HttpEntity;&nbsp;import org.springframework.http.HttpHeaders;&nbsp;import org.springframework.http.HttpMethod;&nbsp;import org.springframework.http.HttpStatus;&nbsp;import org.springframework.http.MediaType;&nbsp;import org.springframework.http.ResponseEntity;&nbsp;import org.springframework.web.client.RestTemplate;希望这有帮助。

largeQ

首先是完整代码(说明如下):import static org.springframework.test.web.client.ExpectedCount.manyTimes;import static org.springframework.test.web.client.ExpectedCount.once;import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;@RunWith(SpringRunner.class)@ActiveProfiles("test")@SpringBootTest@AutoConfigureMockMvcpublic class MyTestClass {MockRestServiceServer mockServer;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private RestTemplate restTemplate;&nbsp; //create a bean somewhere. It will be injected here.&nbsp;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private MockMvc mockMvc;&nbsp; &nbsp; @Before&nbsp; &nbsp; public void configureRestMVC(){&nbsp; &nbsp; &nbsp; &nbsp; mockServer =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MockRestServiceServer.createServer(restTemplate);&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; public void test0() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; //this is where you would mock the call to endpoint and and response&nbsp; &nbsp; &nbsp; &nbsp; mockServer.expect(once(), requestTo("www.example.com/endpoint1"))&nbsp; &nbsp; &nbsp; &nbsp; .andRespond(withSuccess());&nbsp; &nbsp; ...&nbsp;&nbsp; &nbsp; //here you will actually make a call to your controller. If the service class is making a post call to another endpoint outside, that you just mocked in above statement.&nbsp; &nbsp; this.mockMvc.perform(post("www.example2.com/example2endpoint")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .content(asJsonString(new YouCustomObjectThatYouWantToPost))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .contentType(MediaType.APPLICATION_JSON))&nbsp; &nbsp; &nbsp; &nbsp; .andDo(print()).andExpect(status().isOk())&nbsp; &nbsp; &nbsp; &nbsp; .andExpect(content().json(matchResponseAgainstThisObject()));&nbsp; &nbsp;}您需要使用注释。背后的目的是根本不启动服务器,而只测试该层下面的层,其中Spring处理传入的HTTP请求并将其传递给您的控制器。这样,几乎使用完整的堆栈,并且您的代码将被调用,就像它处理真正的HTTP请求一样,但没有启动服务器的成本。为此,我们将使用Spring的MockMvc,我们可以通过使用测试类上的注释来要求为我们注入它。@AutoConfigureMockMvc@AutoConfigureMockMvcprivate MockRestServiceServer mockServer;MockRestServiceServer是客户端REST测试的主要入口点。用于涉及直接或间接使用 RestTemplate 的测试。提供一种方法来设置将通过 RestTemplate 执行的预期请求,以及要发送回的模拟响应,从而消除了对实际服务器的需求。mockServer.expect(once(), requestTo("www.example.com/endpoint1"))&nbsp; &nbsp; .andRespond(withSuccess());这是您将设置模拟外部调用的地方。以及设置期望。this.mockMvc.perform(post("www.example2.com/example2endpoint")..在这里,您将实际对自己的终结点(在控制器中定义的终结点)进行 rest/api 调用。Spring将命中你的端点,执行你在控制器/服务层中的所有逻辑,当涉及到实际在外面进行调用的部分时,将使用你上面刚刚定义的mockServer。这样,它完全离线。您从未遇到过实际的外部服务。此外,您将在同一个 mockMvc.perform 方法上附加断言。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java