【问题标题】:How do I change the default return value for Strings in Mockito?如何更改 Mockito 中字符串的默认返回值?
【发布时间】:2013-04-02 14:59:11
【问题描述】:

This issue from 2010 hints at what I'm trying to do.

我正在做一个单元测试,它练习需要许多模拟对象来完成它需要做的事情的代码(测试 HTML + PDF 渲染)。为了使这个测试成功,我需要生成许多模拟对象,并且这些对象中的每一个最终都会将一些字符串数据返回给正在测试的代码。

认为我可以通过实现我自己的Answer 类或IMockitoConfiguration 来做到这一点,但我不确定如何实现这些,因此它们只会影响返回字符串的方法。

感觉下面的代码和我想要的很接近。它抛出一个强制转换异常,java.lang.ClassCastException: java.lang.String cannot be cast to com.mypackage.ISOCountry。我认为这意味着我需要以某种方式默认或限制 Answer 以仅影响 String 的默认值。

private Address createAddress(){
    Address address = mock(Address.class, new StringAnswer() );

    /* I want to replace repetitive calls like this, with a default string. 
    I just need these getters to return a String, not a specific string.

    when(address.getLocality()).thenReturn("Louisville");
    when(address.getStreet1()).thenReturn("1234 Fake Street Ln.");
    when(address.getStreet2()).thenReturn("Suite 1337");
    when(address.getRegion()).thenReturn("AK");
    when(address.getPostal()).thenReturn("45069");   
    */

    ISOCountry isoCountry = mock(ISOCountry.class);
    when(isoCountry.getIsocode()).thenReturn("US");
    when(address.getCountry()).thenReturn(isoCountry);

    return address;
}

//EDIT: This method returns an arbitrary string
private class StringAnswer implements Answer<Object> {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
        String generatedString = "Generated String!";
           if( invocation.getMethod().getReturnType().isInstance( generatedString )){
               return generatedString;
           }
           else{
               return Mockito.RETURNS_DEFAULTS.answer(invocation);
           }
       }
}

如何设置 Mockito 以默认为返回 String 的模拟类上的方法返回生成的 String? I found a solution to this part of the question on SO

对于额外的点,我怎样才能使生成的值成为Class.methodName 形式的字符串?例如"Address.getStreet1()" 而不仅仅是一个随机字符串?

【问题讨论】:

    标签: java unit-testing mocking mockito


    【解决方案1】:

    我能够完全回答我自己的问题。

    在此示例中,生成了一个带有路易斯维尔地区的地址,而其他字段看起来像“address.getStreet1();”。

    private Address createAddress(){
        Address address = mock(Address.class, new StringAnswer() );
    
        when(address.getLocality()).thenReturn("Louisville");
    
        ISOCountry isoCountry = mock(ISOCountry.class);
        when(isoCountry.getIsocode()).thenReturn("US");
        when(address.getCountry()).thenReturn(isoCountry);
    
        return address;
    }
    
    private class StringAnswer implements Answer<Object> {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
               if( invocation.getMethod().getReturnType().equals(String.class)){
                   return invocation.toString();
               }
               else{
                   return Mockito.RETURNS_DEFAULTS.answer(invocation);
               }
           }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      相关资源
      最近更新 更多