<legend id='TBGvC'><style id='TBGvC'><dir id='TBGvC'><q id='TBGvC'></q></dir></style></legend>

      <tfoot id='TBGvC'></tfoot>
      <i id='TBGvC'><tr id='TBGvC'><dt id='TBGvC'><q id='TBGvC'><span id='TBGvC'><b id='TBGvC'><form id='TBGvC'><ins id='TBGvC'></ins><ul id='TBGvC'></ul><sub id='TBGvC'></sub></form><legend id='TBGvC'></legend><bdo id='TBGvC'><pre id='TBGvC'><center id='TBGvC'></center></pre></bdo></b><th id='TBGvC'></th></span></q></dt></tr></i><div id='TBGvC'><tfoot id='TBGvC'></tfoot><dl id='TBGvC'><fieldset id='TBGvC'></fieldset></dl></div>

      <small id='TBGvC'></small><noframes id='TBGvC'>

      • <bdo id='TBGvC'></bdo><ul id='TBGvC'></ul>

    1. 如何更改 Mockito 中字符串的默认返回值?

      时间:2023-09-25

    2. <tfoot id='gOmYn'></tfoot>
    3. <small id='gOmYn'></small><noframes id='gOmYn'>

          <bdo id='gOmYn'></bdo><ul id='gOmYn'></ul>
            <legend id='gOmYn'><style id='gOmYn'><dir id='gOmYn'><q id='gOmYn'></q></dir></style></legend>
              <tbody id='gOmYn'></tbody>
          • <i id='gOmYn'><tr id='gOmYn'><dt id='gOmYn'><q id='gOmYn'><span id='gOmYn'><b id='gOmYn'><form id='gOmYn'><ins id='gOmYn'></ins><ul id='gOmYn'></ul><sub id='gOmYn'></sub></form><legend id='gOmYn'></legend><bdo id='gOmYn'><pre id='gOmYn'><center id='gOmYn'></center></pre></bdo></b><th id='gOmYn'></th></span></q></dt></tr></i><div id='gOmYn'><tfoot id='gOmYn'></tfoot><dl id='gOmYn'><fieldset id='gOmYn'></fieldset></dl></div>
                本文介绍了如何更改 Mockito 中字符串的默认返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                这个 2010 年的问题暗示了我正在尝试做的事情.

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

                I'm working on a unit test which exercises code that requires many mocked objects to do what it needs to do (testing HTML + PDF rendering). For this test to be successful I need many mocked objects to be generated and each of these objects ultimately return some String data to the code being tested.

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

                I think I can do this by implementing either my own Answer class or IMockitoConfiguration, but I am not sure how to implement those so they only affect methods which return Strings.

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

                I feel that the following code is close to what I want. It throws a cast exception, java.lang.ClassCastException: java.lang.String cannot be cast to com.mypackage.ISOCountry. I think this means I need to somehow default or limit the Answer to only affect the defaults for 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?我在 SO 上找到了这部分问题的解决方案

                How can I set up Mockito to return a generated String by default for methods on a mocked class which return String? I found a solution to this part of the question on SO

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

                For extra points, how can I make that generated value be a String which is in the form Class.methodName? For example "Address.getStreet1()" instead of just a random String?

                推荐答案

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

                I was able to completely answer my own question.

                在此示例中,生成的地址为路易斯维尔地区,而其他字段类似于address.getStreet1();".

                In this example an address with the Locality of Louisville is generated while the other fields look like "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);
                           }
                       }
                }
                

                这篇关于如何更改 Mockito 中字符串的默认返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Mockito - 预期 0 个匹配器,记录 1 个(InvalidUseOfMa 下一篇:Mockito/Powermockito 模拟私有 void 方法

                相关文章

                最新文章

                1. <tfoot id='RFFIp'></tfoot>

                  <small id='RFFIp'></small><noframes id='RFFIp'>

                  <legend id='RFFIp'><style id='RFFIp'><dir id='RFFIp'><q id='RFFIp'></q></dir></style></legend>

                  1. <i id='RFFIp'><tr id='RFFIp'><dt id='RFFIp'><q id='RFFIp'><span id='RFFIp'><b id='RFFIp'><form id='RFFIp'><ins id='RFFIp'></ins><ul id='RFFIp'></ul><sub id='RFFIp'></sub></form><legend id='RFFIp'></legend><bdo id='RFFIp'><pre id='RFFIp'><center id='RFFIp'></center></pre></bdo></b><th id='RFFIp'></th></span></q></dt></tr></i><div id='RFFIp'><tfoot id='RFFIp'></tfoot><dl id='RFFIp'><fieldset id='RFFIp'></fieldset></dl></div>
                      <bdo id='RFFIp'></bdo><ul id='RFFIp'></ul>