@Mock注释下的对象是否应该将其构造函数调用为?

时间:2017-03-30 21:05:45

标签: java android unit-testing junit mockito

我正在尝试使用Mockito创建一个模拟某些依赖项的单元测试。 这是主要代码:

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class VolumesPresenterTest {

VolumesPresenter presenter;

@Mock
OrderRepository orderRepository;

@Mock
VolumesRepository volumesRepository;

@Before
public void createPresenter() {
    presenter = new DefaultVolumesPresenter(orderRepository, volumesRepository);
}

@Test
public void testGetVolumes() {
    presenter.getVolumes();
    verify(volumesRepository).getVolumes();
}
}

OrderRepository有一个构造函数,它接收Android上下文作为参数。这导致单元测试失败,并显示以下堆栈跟踪:

java.lang.NoClassDefFoundError: android/content/Context

at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getDeclaredConstructors(Class.java:2020)
at org.mockito.internal.creation.cglib.ClassImposterizer.setConstructorsAccessible(ClassImposterizer.java:85)
at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:71)
at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49)
at org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
at org.mockito.Mockito.mock(Mockito.java:1285)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
at org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:43)
at org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:66)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:71)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:55)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:276)
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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: android.content.Context
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 39 more


Test ignored.
Process finished with exit code 255

据我所知,我不能在单元测试中使用android框架,但我没想到会调用OrderRepository的真正构造函数。作为一个模拟对象,我认为它只会被其他一些哑/模拟实现所取代。

我的问题是:我是否遗漏了任何东西,或者用@Mock注释的对象的构造函数确实应该被调用?如果是这样,我如何模拟一个依赖项,它将Context作为其构造函数的参数?

2 个答案:

答案 0 :(得分:2)

Mockito不会调用构造函数;您可以将日志记录放在一个简单的类构造函数中来测试它。但是,在创建类之前,Mockito将所有构造函数的可访问性设置为public。

你需要让单元测试的类路径包含android库,所以当Mockito加载构造函数时,它可以看到Context,即使它实际上并没有使用它们。

编辑:如果你很好奇它在幕后做了什么,那里有一个解释in this answer的例子,它展示了Mockito的图书馆在做什么堆栈跟踪的5行。

答案 1 :(得分:1)

  

OrderRepository有一个构造函数,它接收Android上下文作为参数。这导致单元测试失败,并显示以下堆栈跟踪:

java.lang.NoClassDefFoundError: android/content/Context

没有!

错误消息告知您的类路径有问题。