我是Mockito的新手,我在Eclipse中将它与JUnit4一起使用。
我试图模拟我的方法,如下所示:
CartLine line = mock(CartLine.class);
然后我得到:FileNotFoundException由所谓的模拟方法的调用引起,如下面的stacktrace中所示。预期的行为如何?我正使用Mockito来避免使用整个环境。

堆栈跟踪:

java.io.FileNotFoundException: cart.properties (file not found)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at com.mycompany.cart.util.ResourceFactory.getFileInputStream(ResourceFactory.java:119)
at com.mycompany.cart.util.ResourceFactory.getProperties(ResourceFactory.java:295)
at com.mycompany.cart.CartSettings.tryLoadSettings(CartSettings.java:215)
at com.mycompany.cart.CartSettings.<init>(CartSettings.java:72)
at com.mycompany.cart.CartSettings.<clinit>(CartSettings.java:60)
at com.mycompany.cart.CartLine.<clinit>(CartLine.java:59)
at sun.reflect.GeneratedSerializationConstructorAccessor4.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:128)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:63)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:56)
at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51)
at org.mockito.Mockito.mock(Mockito.java:1243)
at org.mockito.Mockito.mock(Mockito.java:1120)
at com.redacted.ml.TestConfToJasper.mockCartLine(TestConfToJasper.java:173)
at com.redacted.ml.TestConfToJasper.mockCartLines(TestConfToJasper.java:131)
at com.redacted.ml.TestConfToJasper.testBuildUCSElement(TestConfToJasper.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
1249 [main] WARN com.redacted.cart.util.ResourceFactory  - Can not find property file:   cart.properties
1249 [main] FATAL com.redacted.cart.CartSettings  - Can not load cart properties!


编辑:这是完整的测试代码:

private static final CartLine mockCartLine(LineType type) {
    CartLine line = mock(CartLine.class);
    when(line.getLineType()).thenReturn(type);
    return line;
}


致电者:

private static final CartLine[] mockCartLines() {
    CartLine[] lines = new CartLine[10];
    int i=0;

    lines[i++] = mockCartLine(LineType.FO);
            [...]
    return lines;
}


致电者:

@Test
public void testBuildUCSElement() {
    ConfToJasper ctj = new ConfToJasper(confML);

    CartController controller = mock(CartController.class);
    CartDataObject dataObject = mock(CartDataObject.class);
    CartLine[] lines = mockCartLines();
[...]
}


(我不是此代码的创建者)

最佳答案

在我看来,您的CartLine中有某种静态初始化程序试图打开文件流。

我强烈建议您通过删除此类静态初始化程序逻辑来清理代码,使其对单元测试友好。 (通常使其更适合使用实例方法)

或者您可能在测试资源中有一个虚拟的`cart.properties1,以便CartLine可以读取它。

或者,您可以考虑使用PowerMock来查看它是否有助于避免此类问题(无论如何我还是很怀疑...)

10-05 19:04