<bdo id='L6W2c'></bdo><ul id='L6W2c'></ul>

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

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

    1. Mockito - thenReturn 总是返回空对象

      时间:2023-10-01
        <bdo id='icfCt'></bdo><ul id='icfCt'></ul>
          <legend id='icfCt'><style id='icfCt'><dir id='icfCt'><q id='icfCt'></q></dir></style></legend>
              <tbody id='icfCt'></tbody>

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

              2. <tfoot id='icfCt'></tfoot>
                本文介绍了Mockito - thenReturn 总是返回空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试实现 Mockito 来测试特定方法,但 .thenReturn(...) 似乎总是返回一个空对象,而不是我想要的:

                I'm trying to implement Mockito to test a particular method but the .thenReturn(...) seems to always be returning a null object instead of what I intended:

                剪切:

                public class TestClassFacade {
                
                  // injected via Spring
                  private InterfaceBP bpService;
                
                  public void setBpService(InterfaceBP bpService) {
                
                      this.bpService = bpService;
                  }
                
                  public TestVO getTestData(String testString) throws Exception {
                
                    BPRequestVO bpRequestVO = new BPRequestVO();
                
                    bpRequestVO.setGroupNumber(testString) ;
                    bpRequestVO.setProductType("ALL") ;           
                    bpRequestVO.setProfileType("Required - TEST") ;
                
                    IBPServiceResponse serviceResponse = bpService.getProduct(bpRequestVO);  //PROBLEM
                
                    if (serviceResponse.getMessage().equalsIgnoreCase("BOB")) {
                
                        throw new Exception();
                
                    } else {
                
                        TestVO testVO = new TestVO();
                    }
                
                    return testVO;
                  }
                
                }
                

                弹簧配置:

                <bean id="testClass" class="com.foo.TestClassFacade">
                
                   <property name="bpService" ref="bpService" />
                
                </bean>
                
                <bean id="bpService" class="class.cloud.BPService" />
                

                Mockito 测试方法:

                @RunWith(MockitoJUnitRunner.class)
                public class BaseTest {
                
                    @Mock BPService mockBPService;
                    @InjectMocks TestClassFacade mockTestClassFacade;
                
                    private String testString = null;
                    private BPRequestVO someBPRequestVO = new BPRequestVO();
                    private IBPServiceResponse invalidServiceResponse = new BPServiceResponse();
                
                    @Test (expected = Exception.class)
                    public void getBPData_bobStatusCode_shouldThrowException() throws Exception {
                
                        invalidServiceResponse.setMessage("BOB");
                
                        someBPRequestVO.setGroupNumber(null);
                        someBPRequestVO.setProductType("ALL");
                        someBPRequestVO.setProfileType("Required - TEST");
                
                        System.out.println("1: " + someBPRequestVO.getGroupNumber());
                        System.out.println("2: " + someBPRequestVO.getProductType());
                        System.out.println("3: " + someBPRequestVO.getProfileType());
                        System.out.println("4: " + someBPRequestVO.getEffectiveDate());
                
                        when(mockBPService.getProduct(someBPRequestVO)).thenReturn(invalidServiceResponse);
                
                        mockTestClassFacade.getTestData(testString);
                
                        verify(mockBPService).getProduct(someBPRequestVO);
                    }
                }
                

                系统输出:

                1: null
                2: ALL
                3: Required - TEST
                4: null
                

                这里发生的情况是,当我运行测试时,serviceResponse 对象在上面标有//PROBLEM 的 CUT 行上为 null.我的愿望是用我的测试方法中的invalidServiceResponse"对象填充该对象.从我的 System.out.println 的输出来看,我的 bpRequestVO 似乎与我的 someBPRequestVO 在内容上相匹配.

                What's happening here is that when I run the test the serviceResponse object is null on the line in the CUT marked with //PROBLEM above. My desire is to have that object be populated with my "invalidServiceResponse" object from my test method. Judging from the output of my System.out.println's it appears that my bpRequestVO matches my someBPRequestVO in content.

                谁能告诉我我在这里缺少什么?

                Could some one show me what I'm missing here?

                感谢您的宝贵时间!

                推荐答案

                when() 中使用的 BPRequestVO 实例与 getTestData() 中使用的实例不同代码>.
                除非您覆盖 equals(),否则它们将不匹配.

                The instance of BPRequestVO that you use with when() is different than the one used in getTestData().
                Unless you override equals(), they will not match.

                如果你重写 equals(),你应该不需要编写自定义 Matcher.请注意 Mockito 文档中的以下内容:

                You should not need to write a custom Matcher if you override equals(). Note the following from the Mockito documentation:

                自定义参数匹配器会使测试的可读性降低.有时最好为传递给 mock 的参数实现 equals()(Mockito 自然使用 equals() 进行参数匹配).这可以使测试更清晰."

                "Custom argument matchers can make the test less readable. Sometimes it's better to implement equals() for arguments that are passed to mocks (Mockito naturally uses equals() for argument matching). This can make the test cleaner."

                这篇关于Mockito - thenReturn 总是返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:无法使用 Mockito 返回类对象 下一篇:doThrow 中看起来正确的 Mockito 异常

                相关文章

                最新文章

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

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

                    <legend id='wWQFP'><style id='wWQFP'><dir id='wWQFP'><q id='wWQFP'></q></dir></style></legend>
                      <bdo id='wWQFP'></bdo><ul id='wWQFP'></ul>