2 回答

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;
希望這有幫助。

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 方法上附加斷言。
添加回答
舉報(bào)