How to test a RestClientException with MockRestServiceServer

后端 未结 4 2285
不思量自难忘°
不思量自难忘° 2021-02-19 04:13

While testing a RestClient-Implementation I want to simulate a RestClientException that may be thrown by some RestTemplate-methods in that implementation f.e. the delete-method:

4条回答
  •  庸人自扰
    2021-02-19 05:03

    How about this :

    @Spy
    @InjectMocks
    ClasstoMock objToMock;
    
    @Test
    public void testRestClientException() throws  Exception {       
            try {
            Mockito.when(this.objToMock.perform()).thenThrow(new RestClientException("Rest Client Exception"));
            this.objToMock.perform();
            }
            catch(Exception e){
                Assert.assertEquals(RestClientException.class, e.getClass());
            }  
    

提交回复
热议问题