繁体   English   中英

如何使用 mockito 模拟和编写 try 和 catch 块的单元测试

[英]How to mock and write unit test for try and catch block using mockito

在下面的示例中,我尝试使用 mockito 测试 catch 和 try 块。 而且当引发 CustomException 时,我想用第二个主机调用该方法。 谢谢你。

private static void getDetails()
{
    final String host1 = "http://localhost:8080/springrestexample/employee/id";
   
    final String host2 = "http://localhost:8080/springrestexample/student/id";

     
    RestTemplate restTemplate = new RestTemplate();
    String result = null;
    try {
        String result = restTemplate.getForObject(host1, String.class);
    } catch(CustomException customException) {
       String result = restTemplate.getForObject(host2, String.class);
    }
    return result;
}

是否有可能将restTemplate作为参数传递给该方法。

(如果是,那么)

使用org.mockito.Mockito -> mock()你可以像下面这样模拟,

RestTemplate restTemplateMock = mock(RestTemplate.class);
when(restTemplateMock.getForObject(host1)).thenThrow(CustomException.class);

将此模拟 object 传递给您的方法,它将在 try 中引发异常。

更新:您的参考的示例测试用例。

@Test
public void testGetDetails()
{
  RestTemplate restTemplateMock = mock(RestTemplate.class);
  when(restTemplateMock.getForObject(host1)).thenReturn(SOME_STRING);
  String result = //call your method
  assertThat(result).isEqualTo(SOME_STRING);
}

@Test
public void testGetDetailsUsesOtherHostWhenExceptionIsThrown()
{
  RestTemplate restTemplateMock = mock(RestTemplate.class);
  when(restTemplateMock.getForObject(host1)).thenThrow(CustomException.class);
  when(restTemplateMock.getForObject(host2)).thenReturn(SOME_STRING);
  String result = //call your method
  assertThat(result).isEqualTo(SOME_STRING);
}

当您需要使用相同的模拟 object 时,

RestTemplate myRestTemplateMock = mock(RestTemplate.class);

@Test
public void testGetDetails()
{
  when(myRestTemplateMock.getForObject(host1)).thenReturn(SOME_STRING);
  String result = //call your method
  assertThat(result).isEqualTo(SOME_STRING);
}

@Test
public void testGetDetailsUsesOtherHostWhenExceptionIsThrown()
{ 
  
  when(myRestTemplateMock.getForObject(host1)).
                                  thenThrow(CustomException.class);
  when(myRestTemplateMock.getForObject(host2)).thenReturn(SOME_STRING);
  String result = //call your method
  assertThat(result).isEqualTo(SOME_STRING);
}

好的,首先,您的原始方法需要如下所示。 (因为通常的做法是我们不测试私有方法,如果您需要测试 static 方法,您需要的不仅仅是 Mockito (PowerMock))

public class Example {

    @Autowired
    public RestTemplate restTemplate;

    public  String restTemplateTest()
    {
        final String host1 = "http://localhost:8080/springrestexample/employee/id";

        final String host2 = "http://localhost:8080/springrestexample/student/id";

        String result;
        try {
             result = restTemplate.getForObject(host1, String.class);
        } catch(CustomException customException) {
             result = restTemplate.getForObject(host2, String.class);
        }
        return result;
    }
}

以下是上述方法的示例测试。

    public class ExampleTest {
    
       @Mock
       private RestTemplate restTemplate;
    
       @InjectMocks
       private Example testExample;
    
       @BeforeEach
       public void setUp()
       {
           MockitoAnnotations.initMocks(this);
       }
    
        @Test
        public void restTemplateTest() {
    
            Mockito.when(restTemplate.getForObject(Mockito.eq("http://localhost:8080/springrestexample/employee/id"), Mockito.eq(String.class)))
                    .thenThrow(CustomException.class);
    
            Mockito.when(restTemplate.getForObject(Mockito.eq("http://localhost:8080/springrestexample/student/id"), Mockito.eq(String.class)))
                    .thenReturn("expected");
    
            testExample.restTemplateTest();
    
            // if exception thrown , then rest template mock will invoke 2 times, otherwise
            // 1
            Mockito.verify(restTemplate, Mockito.times(2)).getForObject(Mockito.anyString(), Mockito.eq(String.class));
    
        }

}

以下是在没有发生异常的情况下进行测试的方法。

@Test
public void restTemplateTest_when_no_exception() {

    Mockito.when(restTemplate.getForObject(Mockito.eq("http://localhost:8080/springrestexample/employee/id"), Mockito.eq(String.class)))
            .thenReturn("expectedString");

    String actual = testExample.restTemplateTest();

    // if exception not thrown , then rest template mock will invoke 1 times, otherwise
    // 1
    Mockito.verify(restTemplate, Mockito.times(1)).getForObject(Mockito.anyString(), Mockito.eq(String.class));

    Assert.assertEquals("expectedString",actual);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM