简体   繁体   English

Jmockit:无法模拟内部字段。 总是以null结尾

[英]Jmockit: Unable to mock inner field. Always ends up being null

I have trouble controlling what is going on with the variables inside the methods I would like to test. 我无法控制要测试的方法中的变量的状态。 In this example, the input to the method being tested is a mocked or injectable object yet if you try to do anything with it, you get Null* exceptions. 在此示例中,被测试方法的输入是模拟对象或可注入对象,但是,如果您尝试对其进行任何操作,则会得到Null *异常。

Class and method being tested: 要测试的类和方法:

public class SomeClass {
    public SomeClass() {}

    public void myMethod(InputClass input) {
        //how do I set this field so that no matter what, I don't get an exeption?
        long someLong = Long.parseLong(input.getSomeLong());

        // how do I prevent the NumberFormatException when Long.parseLong() is called in this situation?
        SomeInnerClass innerClass = SomeInnerClass(Long.parseLong(input.getSomeLong()));
        // how do I prevent the NumberFormatException when Long.parseLong() is called in this situation?
        innerClass.doStuff(Long.parseLong(input.getSomeLong()));

        // ...
    }
}

Test Class: 测试类别:

public class TestSomeClass {
    @Tested private SomeClass classBeingTested;
    @Injectable SomeInnerClass innerClass;
    @Injectable InputClass input; // doesn't matter if I use @mocked here

    @Test
    public void shouldTestSomething() {
        new NonStrictExpectations() {{
            // some expectations with innerClass...
        }};     

        classBeingTested.myMethod(input); // at this point I would get an error from Long.parsLong() 

        // ... 
    }
}

I want to be able to ignore errors in certain parts of the code within the method I am testing. 我希望能够忽略正在测试的方法中代码某些部分的错误。 I don't care at all about the Long.parseLong() error in this specific case. 在这种情况下,我完全不关心Long.parseLong()错误。 I'm currently not trying to test that, yet it is getting in the way of the real test. 我目前不打算测试它,但是它正在妨碍真正的测试。 How do you create a fake object, in place of the object causing issues, so that it can be ignored while I test some other part of the method? 您如何创建伪造的对象来代替引起问题的对象,以便在测试方法的其他部分时可以将其忽略?

Please use below code and check if it help. 请使用下面的代码,并检查是否有帮助。 You need to add dependency to mockito into your project.One important think make sure you use @RunWith(MockitoJUnitRunner.class) in your test class. 您需要@RunWith(MockitoJUnitRunner.class)依赖项添加到项目中。一个重要的思想是确保在测试类中使用@RunWith(MockitoJUnitRunner.class)

@RunWith(MockitoJUnitRunner.class)
public class TestSomeClass {
    @Tested private SomeClass classBeingTested;
    @Injectable SomeInnerClass innerClass;
    @Mock
    InputClass input; // doesn't matter if I use @mocked here

    @Test
    public void shouldTestSomething() {
        new NonStrictExpectations() {{
            // some expectations with innerClass...
        }};     
        Mockito.when(input).getSomeLong().thenReturn("11").thenReturn("11");//This will return 11 in place of input.getSomeLong()for 2 times
        classBeingTested.myMethod(input); // In this case yoou will not get an error from Long.parsLong() 

        // ... 
    }
}

======================================================= ================================================== =====

Using JMockit : 使用JMockit:

 import org.junit.Test;

import mockit.Injectable;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import mockit.Tested;

public class SomeClassTest {
    @Tested
    private SomeClass classBeingTested;
    @Injectable
    SomeInnerClass innerClass;

    @Mocked
    InputClass input;

    @Test
    public void shouldTestSomething() {
        new NonStrictExpectations() {

            {
                input.getSomeLong();
                returns("11");
            }
        };

        classBeingTested.myMethod(input); 

        // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM