• <tfoot id='AYeZs'></tfoot>

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

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

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

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

        mockito return value based on property of a parameter(基于参数属性的模拟返回值)
      2. <small id='7S9yz'></small><noframes id='7S9yz'>

          <tbody id='7S9yz'></tbody>

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

                  <tfoot id='7S9yz'></tfoot>
                • <legend id='7S9yz'><style id='7S9yz'><dir id='7S9yz'><q id='7S9yz'></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());
                  }
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  1. <legend id='xvNv9'><style id='xvNv9'><dir id='xvNv9'><q id='xvNv9'></q></dir></style></legend>
                  2. <small id='xvNv9'></small><noframes id='xvNv9'>

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