JMockit:当应该实例化模拟对象时出现空指针错误?

发布于 2024-10-13 08:54:04 字数 1349 浏览 8 评论 0原文

我对 JMockit 的理解是,它将用模拟替换模拟对象的所有实例(除非否则你就告诉它)。

因此,我很困惑 NPE 实例化一个对象后,我试图模拟。

测试的目的不是调查导致 NPE 的对象,但我确实需要模拟它才能执行测试,因为它执行一些数据库操作来验证某些输入。

我的测试代码如下(不是复制意大利面,因为它是工作代码,但仍然应该突出显示问题):

public class ClassToTest{

    public execute(){
       MyDependency myDep = getDependency();

        myDep.doSomething(); //I get a NPE here, implying getDependency returned null 
    }

    protected MyDependency getDependency(){
        return new MyDependency("an Arg", "another Arg");
    }

}

我的测试方法:

@Test
public void testCreateHorseDogMeetingByCodeDataProviderTruncated()
    throws IllegalArgumentException, SQLException,
    IllegalCountryLocationCombo, MEPException {

    // Arrange
    ClassToTest myClass = new ClassToTest();

    new NonStrictExpectations() {

        MyDependency mockDep;

        {
            //Set up my expectations, not related to MyDependency
        }
    };

    // Act
    myClass.execute();

    // Assert
    new Verifications() {
        {
            //some verification stuff
        }
    };
}

任何人都可以帮助我解决此 NPE 问题,以便我可以完成测试吗?

My understanding of JMockit is that it will replace all instances of a mocked object with a mock (unless you tell it otherwise).

Hence I am perplexed to be getting a NPE after instantiating an object I'm attempting to mock.

The purpose of the test is not to investigate the object causing the NPE, but I do need to mock it in order to carry out the test as it carrys out some database actions to validate some input.

My code under test is like this (not copy pasta, as it's work code, but should highlight the issue nonetheless):

public class ClassToTest{

    public execute(){
       MyDependency myDep = getDependency();

        myDep.doSomething(); //I get a NPE here, implying getDependency returned null 
    }

    protected MyDependency getDependency(){
        return new MyDependency("an Arg", "another Arg");
    }

}

My Test method:

@Test
public void testCreateHorseDogMeetingByCodeDataProviderTruncated()
    throws IllegalArgumentException, SQLException,
    IllegalCountryLocationCombo, MEPException {

    // Arrange
    ClassToTest myClass = new ClassToTest();

    new NonStrictExpectations() {

        MyDependency mockDep;

        {
            //Set up my expectations, not related to MyDependency
        }
    };

    // Act
    myClass.execute();

    // Assert
    new Verifications() {
        {
            //some verification stuff
        }
    };
}

Can anyone help me fix this NPE issue so I can finish my test?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪歌 2024-10-20 08:54:04

结果我不小心实例化了 ClassToTest 的子类,它覆盖了 getDependency 的实现并导致出现 null 值。一定是自动完成的事情。

Turns out I was accidentally instantiating a subclass of ClassToTest which overridded the implementation of getDependency and causing the null value to appear. Must have been an autocomplete thing.

成熟稳重的好男人 2024-10-20 08:54:04

MyDependency 是一个接口吗?您可能还需要模拟具体的类。

您还可以尝试将 MyDependency mockDep 放入测试函数的参数列表中。然后您也将获得用于验证步骤的模拟对象。

Is MyDependency an interface? You may need to mock the concrete class as well.

You can also try putting MyDependency mockDep in the argument list of the test function. Then you'll have the mocked object for the Verification step too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文