簡體   English   中英

無法使用 restclienttest 模擬 resttemplate 調用

[英]unable to mock the resttemplate call using restclienttest

我想模擬使用RestTemplateRestTemplateBuilder 因此,我正在使用restclienttest

不幸的是,我無法模擬resttemplate調用。 Sup supExpected = myService.getDetails("1234")被調用時,它正在執行完整的后端調用,而不是導致我要求做的事情。

它不是生成自定義 json 字符串“SD”,而是執行實際的 GET 調用並從后端給出響應。

這是我的 junit 代碼:

@RunWith(SpringRunner.class)
@RestClientTest(MyService.class)
public class TestMyServiceApplication {


    @Autowired
    private MyService myService; 

     @Autowired
        private RestTemplate restTemplate;

    @Autowired 
    private MockRestServiceServer server;       

    @Before public void setUp() { 
     server= MockRestServiceServer.createServer(restTemplate); }

    @Test
    public void ReturnSupplierSuccessfully() {

        String SD= "{\"SUP\": {\"LNR\": \"1234\",\"NAME1\": \"RestClient\"}]}}";
        this.server
        .expect(requestTo("abc.net/service/v1?lnr=1234"))
        .andExpect(method(HttpMethod.GET))
        .andRespond(withSuccess(SD, MediaType.APPLICATION_JSON));

        Sup supExpected = myService.getDetails("1234");

        server.verify();
        assertNotNull(supExpected);
    }

誰能幫我我的代碼有什么問題?

嘗試將@Mock注釋放在 RestTemplate 和其他相應的服務類之上。

@Mock

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM