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

        <legend id='AoLYd'><style id='AoLYd'><dir id='AoLYd'><q id='AoLYd'></q></dir></style></legend>
        • <bdo id='AoLYd'></bdo><ul id='AoLYd'></ul>
        <tfoot id='AoLYd'></tfoot>

      1. <small id='AoLYd'></small><noframes id='AoLYd'>

        基于参数属性的模拟返回值

        时间:2023-10-01
        <tfoot id='MAaXL'></tfoot>

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

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

              <bdo id='MAaXL'></bdo><ul id='MAaXL'></ul>
                <tbody id='MAaXL'></tbody>

                • <legend id='MAaXL'><style id='MAaXL'><dir id='MAaXL'><q id='MAaXL'></q></dir></style></legend>
                • 本文介绍了基于参数属性的模拟返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  通常在使用 mockito 时,我会做类似的事情

                  Normally when using mockito I will do something like

                  Mockito.when(myObject.myFunction(myParameter)).thenReturn(myResult);
                  

                  有没有可能做一些类似的事情

                  Is it possible to do something along the lines of

                  myParameter.setProperty("value");
                  Mockito.when(myObject.myFunction(myParameter)).thenReturn("myResult");
                  
                  myParameter.setProperty("otherValue");
                  Mockito.when(myObject.myFunction(myParameter)).thenReturn("otherResult");
                  

                  所以而不是仅仅使用参数来确定结果.它使用参数内的属性值来确定结果.

                  So rather than when just using the parameter to determine the result. It is using a value of a property inside the parameter to determine the result.

                  所以当代码执行时,它的行为是这样的

                  so when the code is executed it behaves like so

                  public void myTestMethod(MyParameter myParameter,MyObject myObject){
                      myParameter.setProperty("value");
                      System.out.println(myObject.myFunction(myParameter));// outputs myResult
                  
                      myParameter.setProperty("otherValue");
                      System.out.println(myObject.myFunction(myParameter));// outputs otherResult
                  }
                  

                  <小时>

                  目前的解决方案,希望可以提出更好的建议.


                  current solution, hopefully something better can be suggested.

                  private class MyObjectMatcher extends ArgumentMatcher<MyObject> {
                  
                      private final String compareValue;
                  
                      public ApplicationContextMatcher(String compareValue) {
                          this.compareValue= compareValue;
                      }
                  
                      @Override
                      public boolean matches(Object argument) {
                          MyObject item= (MyObject) argument;
                          if(compareValue!= null){
                              if (item != null) {
                                  return compareValue.equals(item.getMyParameter());
                              }
                          }else {
                              return item == null || item.getMyParameter() == null;
                          }
                          return false;
                      }
                  }
                  
                  public void initMock(MyObject myObject){
                      MyObjectMatcher valueMatcher = new MyObjectMatcher("value");
                      MyObjectMatcher otherValueMatcher = new MyObjectMatcher("otherValue");
                      Mockito.when(myObject.myFunction(Matchers.argThat(valueMatcher))).thenReturn("myResult");
                      Mockito.when(myObject.myFunction(Matchers.argThat(otherValueMatcher))).thenReturn("otherResult");
                  }
                  

                  推荐答案

                  这是一种方法.这使用 Answer 对象来检查属性的值.

                  Here's one way of doing it. This uses an Answer object to check the value of the property.

                  @RunWith(MockitoJUnitRunner.class)
                  public class MyTestClass {
                      private String theProperty;
                      @Mock private MyClass mockObject;
                  
                      @Before
                      public void setUp() {
                          when(mockObject.myMethod(anyString())).thenAnswer(
                              new Answer<String>(){
                              @Override
                              public String answer(InvocationOnMock invocation){
                                  if ("value".equals(theProperty)){
                                      return "result";
                                  }
                                  else if("otherValue".equals(theProperty)) {
                                      return "otherResult";
                                  }
                                  return theProperty;
                              }});
                      }
                  }
                  

                  还有一种我更喜欢的替代语法,它可以实现完全相同的效果.由你选择其中的哪一个.这只是 setUp 方法 - 测试类的其余部分应该与上面相同.

                  There's an alternative syntax, which I actually prefer, which will achieve exactly the same thing. Over to you which one of these you choose. This is just the setUp method - the rest of the test class should be the same as above.

                  @Before
                  public void setUp() {
                      doAnswer(new Answer<String>(){
                          @Override
                          public String answer(InvocationOnMock invocation){
                              if ("value".equals(theProperty)){
                                  return "result";
                              }
                              else if("otherValue".equals(theProperty)) {
                                  return "otherResult";
                              }
                              return theProperty;
                          }}).when(mockObject).myMethod(anyString());
                  }
                  

                  这篇关于基于参数属性的模拟返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito 匹配器和基元数组 下一篇:mockito 的学习资源

                  相关文章

                  最新文章

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

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

                    1. <tfoot id='AMQfO'></tfoot>
                        <bdo id='AMQfO'></bdo><ul id='AMQfO'></ul>