当前位置: 首页 > 知识库问答 >
问题:

外部模拟方法在实际类中返回Null

唐彬炳
2023-03-14

当我测试模拟外部调用时,我没有看到报告的模拟值,而是为 Null,并且我的测试失败。我可以在测试类中看到模拟值(报告),但在BusinessServiceImpl类中看不到,并且应用程序(方法返回)没有像我预期的那样被修改。

我的期望是:当我在Impl类中模拟外部调用时,模拟值应该在那里可用,其余的一切都会发生,就好像调用了真正的方法来完成单元测试一样。

实现代码:

package com.core.business.service.dp.fulfillment;

import com.core.business.service.dp.payment.PaymentBusinessService;

public class BusinessServiceImpl implements BusinessService { // Actual Impl Class
    private PaymentBusinessService paymentBusinessService = PluginSystem.INSTANCE.getPluginInjector().getInstance(PaymentBusinessService.class);

    @Transactional( rollbackOn = Throwable.class)
    public Application  applicationValidation (final Deal deal) throws BasePersistenceException {
        Application application = (Application) ApplicationDTOFactory.eINSTANCE.createApplication();
        //External Call we want to Mock
        String report = paymentBusinessService.checkForCreditCardReport(deal.getId());
        if (report != null) {
            application.settingSomething(true); //report is Null and hence not reaching here
        }
        return application;
    }
}

测试代码:

@Test(enabled = true)// Test Class
public void testReCalculatePrepaids() throws Exception {
    PaymentBusinessService paymentBusinessService = mock(PaymentBusinessService.class);
    //Mocking External Call
    when(paymentBusinessService.checkForCreditCardReport(this.deal.getId())).thenReturn(new String ("Decline by only Me"));
    String report = paymentBusinessService.checkForCreditCardReport(this.deal.getId());
    // Mocked value of report available here
    //Calling Impl Class whose one external call is mocked
    //Application is not modified as expected since report is Null in Impl class
    Application sc = BusinessService.applicationValidation(this.deal);
}

共有2个答案

乐欣可
2023-03-14

我完成了它,并且能够成功地获得模拟的值,而根本不用接触BusinessServiceImpl类。我遵循的步骤是:1 .@ Mock PaymentBusinessService PaymentBusinessService = Mock(PaymentBusinessService . class);2.@ inject mocks private PaymentBusinessService PaymentBusinessService = PluginSystem。INSTANCE.getPluginInjector()。getInstance(paymentbusinessservice . class);

然后简单地运行上面的测试,我可以在BusinessServiceImpl中看到报告的值为“仅由我拒绝”,我的测试用例通过了

包翔
2023-03-14

Mockito的主要目的是隔离测试。如在,当您测试BusinessServiceImpl时,您应该模拟其所有依赖项。

这正是你在上面的例子中想要做的。现在,为了使模拟工作,被模拟的对象必须被注入到您试图测试的类中,在本例中是< code>BusinessServiceImpl。

一种方法是由类的构造者传递依赖关系,即依赖注入。或者您可以看看如何使用< code>Spring和< code>ReflectionTestUtils来实现。

 类似资料:
  • 我对Spock Mock()对象有一个问题。我有一个java类正在尝试测试。这个类做了一些我想模拟的ftp内容。我的示例代码 现在,在“test execute”中的文件。each{}打印预期的内容。但当调用receivedata.execute()时,我的sftpUtils返回null。知道为什么吗?

  • 如何模拟返回已强制转换的模拟对象的方法。 试验方法。

  • 其中authUser()定义为final,如下所示: 我正在学习如何使用PowerMock模拟非静态方法,以及Powermockito是否可以模拟非final具体类中的final方法?。我尝试了一些变体,例如使用Mockito而不是PowerMock来存根authUser,以及将apiclientconnection.class添加到PrepareForTest注释中。我不明白为什么它不起作用。我

  • 我有一个类,它具有外部依赖性,返回列表的未来。如何模拟外部依赖?

  • 问题内容: 是否可以在没有警告警告的情况下模拟(带有模拟)签名方法?我试过了: 但无论我如何声明,我总是会遇到编译错误。例如当我这样宣布 我收到标准的通用/ mockito编译错误 问题答案: 使用doReturn-when备用存根语法。 被测系统: 和测试用例: 无需错误或警告抑制

  • 我有一个类,它有以下实现: 如您所见,方法调用,然后在返回结果之前执行一些逻辑。 当我试图模拟这个存储库进行单元测试时,我很难从中获得结果,因为它总是作为空引用异常而失败。 以下是单元测试: 如何使此测试工作,使模拟方法在调用被模拟的方法之后执行一些逻辑?