第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 Mockito 在客戶端測(cè)試 POST 請(qǐng)求

使用 Mockito 在客戶端測(cè)試 POST 請(qǐng)求

搖曳的薔薇 2022-08-17 12:15:54
我想測(cè)試應(yīng)該向“服務(wù)器”發(fā)送post請(qǐng)求的post方法(因此我想模擬來自服務(wù)器的響應(yīng)并檢查響應(yīng))。另外,我想測(cè)試響應(yīng)是否在正文中包含http狀態(tài)OK。問題:我應(yīng)該如何使用 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");        }      }  }另一個(gè)模塊(服務(wù)器端)中的我的控制器:@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);    }測(cè)試:    @RunWith(SpringRunner.class)@ActiveProfiles("test")@SpringBootTest(classes = Client.class)@AutoConfigureMockMvcpublic 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());}
查看完整描述

2 回答

?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

根據(jù)您的客戶端類,希望提出以下更改建議,以使其更易于測(cè)試:


    //  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;

      }

  }

然后對(duì)于上面的我們朱尼特作為:


@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());

  }

}

基本上在您的功能中,我們正在做:sendResponse()


// 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 

因此,在相應(yīng)的測(cè)試中,我們還應(yīng)該只測(cè)試函數(shù)正在執(zhí)行的操作,因?yàn)檫B接正在被照顧,并且您沒有覆蓋任何工作,因此我們不應(yīng)該為相同的事情做任何事情......而只是測(cè)試我們的代碼/邏輯。restTemplaterestTemplate


最后,為了確保導(dǎo)入看起來像:


可以肯定的是,進(jìn)口會(huì)像這樣:


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;

希望這有幫助。


查看完整回答
反對(duì) 回復(fù) 2022-08-17
?
largeQ

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊

首先是完整代碼(說明如下):


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

@AutoConfigureMockMvc

public class MyTestClass {


MockRestServiceServer mockServer;


    @Autowired

    private RestTemplate restTemplate;  //create a bean somewhere. It will be injected here. 


    @Autowired

    private MockMvc mockMvc;


    @Before

    public void configureRestMVC(){

        mockServer =

                MockRestServiceServer.createServer(restTemplate);

    }


    @Test

    public void test0() throws Exception {

        //this is where you would mock the call to endpoint and and response

        mockServer.expect(once(), requestTo("www.example.com/endpoint1"))

        .andRespond(withSuccess());

    ... 

    //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.

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

                .content(asJsonString(new YouCustomObjectThatYouWantToPost))

                .contentType(MediaType.APPLICATION_JSON))

        .andDo(print()).andExpect(status().isOk())

        .andExpect(content().json(matchResponseAgainstThisObject()));

   }

您需要使用注釋。背后的目的是根本不啟動(dòng)服務(wù)器,而只測(cè)試該層下面的層,其中Spring處理傳入的HTTP請(qǐng)求并將其傳遞給您的控制器。這樣,幾乎使用完整的堆棧,并且您的代碼將被調(diào)用,就像它處理真正的HTTP請(qǐng)求一樣,但沒有啟動(dòng)服務(wù)器的成本。為此,我們將使用Spring的MockMvc,我們可以通過使用測(cè)試類上的注釋來要求為我們注入它。@AutoConfigureMockMvc@AutoConfigureMockMvc


private MockRestServiceServer mockServer;

MockRestServiceServer是客戶端REST測(cè)試的主要入口點(diǎn)。用于涉及直接或間接使用 RestTemplate 的測(cè)試。提供一種方法來設(shè)置將通過 RestTemplate 執(zhí)行的預(yù)期請(qǐng)求,以及要發(fā)送回的模擬響應(yīng),從而消除了對(duì)實(shí)際服務(wù)器的需求。


mockServer.expect(once(), requestTo("www.example.com/endpoint1"))

    .andRespond(withSuccess());

這是您將設(shè)置模擬外部調(diào)用的地方。以及設(shè)置期望。


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

在這里,您將實(shí)際對(duì)自己的終結(jié)點(diǎn)(在控制器中定義的終結(jié)點(diǎn))進(jìn)行 rest/api 調(diào)用。Spring將命中你的端點(diǎn),執(zhí)行你在控制器/服務(wù)層中的所有邏輯,當(dāng)涉及到實(shí)際在外面進(jìn)行調(diào)用的部分時(shí),將使用你上面剛剛定義的mockServer。這樣,它完全離線。您從未遇到過實(shí)際的外部服務(wù)。此外,您將在同一個(gè) mockMvc.perform 方法上附加斷言。


查看完整回答
反對(duì) 回復(fù) 2022-08-17
  • 2 回答
  • 0 關(guān)注
  • 89 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)