使用模拟的上下文为junit测试创建对象给我NullPointerException

时间:2019-04-01 21:47:33

标签: java android

所以看来我不能只在这里模拟上下文。

在测试类中破坏的代码

@Before
public void setUp() throws Exception {
    context = mock(Context.class);
    testString = "this is a test";
    wikiSpeedi = new WikiSpeediDialog(context, testString);
}

@Test
public void someTest() {
    //some test
}

WikiSpeediDialog扩展了NoDimBottomSheetDialog,构造函数将上下文传递给super

 public WikiSpeediDialog(@NonNull Context context, final String selectedText) {

    super(context);

这是代码中断的地方。我只想测试此类中的一些方法。我可以使用nonNull值获取或模拟上下文吗?

这是我的错误:

java.lang.NullPointerException
at android.support.design.widget.BottomSheetDialog.getThemeResId(BottomSheetDialog.java:205)
at android.support.design.widget.BottomSheetDialog.<init>(BottomSheetDialog.java:55)
at android.support.design.widget.BottomSheetDialog.<init>(BottomSheetDialog.java:51)
at org.wikipedia.page.NoDimBottomSheetDialog.<init>(NoDimBottomSheetDialog.java:17)
at org.wikipedia.wikiSpeedi.WikiSpeediDialog.<init>(WikiSpeediDialog.java:32)
at org.wikipedia.wikiSpeedi.wikispeedi.setUp(wikispeedi.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

1 个答案:

答案 0 :(得分:0)

据我所知,您不能像在以前那样在单元测试中为上下文建模。 如果您使用Mockito,则应该这样做:

@RunWith(MockitoJUnitRunner.class)
public class UnitTestSample {

    private static final String FAKE_STRING = "HELLO WORLD";

    @Mock
    Context mockContext;

    @Test
    public void readStringFromContext_LocalizedString() {
        // Given a mocked Context injected into the object under test...
        when(mockContext.getString(R.string.hello_world))
                .thenReturn(FAKE_STRING);
        ClassUnderTest myObjectUnderTest = new ClassUnderTest(mockContext);

        // ...when the string is returned from the object under test...
        String result = myObjectUnderTest.getHelloWorldString();

        // ...then the result should be the expected one.
        assertThat(result, is(FAKE_STRING));
    }
}

// https://developer.android.com/training/testing/unit-testing/local-unit-tests#mocking-dependencies

或者您可以尝试这种情况

public class UnitTestSampleJava {
    private static final String FAKE_STRING = "HELLO_WORLD";
    private Context context = ApplicationProvider.getApplicationContext();

    @Test
    public void readStringFromContext_LocalizedString() {
        // Given a Context object retrieved from Robolectric...
        ClassUnderTest myObjectUnderTest = new ClassUnderTest(context);

        // ...when the string is returned from the object under test...
        String result = myObjectUnderTest.getHelloWorldString();

        // ...then the result should be the expected one.
        assertThat(result).isEqualTo(FAKE_STRING);
    }
}