简体   繁体   中英

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.

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. 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)
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 :

 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); 

        // ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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