简体   繁体   English

Mockito:mock()调用真正的方法

[英]Mockito : mock() calls real method

I am new to Mockito, I am using it along with JUnit4 in Eclipse. 我是Mockito的新手,我在Eclipse中使用它和JUnit4 I am trying to mock my method as follows: CartLine line = mock(CartLine.class); 我试图模仿我的方法如下: CartLine line = mock(CartLine.class); Then I get: FileNotFoundException caused by the call of the supposedly mocked method, as shown in the stacktrace below. 然后我得到: FileNotFoundException由所谓的模拟方法的调用引起,如下面的stacktrace所示。 How is this the expected behaviour? 这是预期的行为? I am using Mockito precisely to avoid using the whole environment. 我正在使用Mockito以避免使用整个环境。

Stack Trace: 堆栈跟踪:

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!

Edit: Here is the full test code: 编辑:这是完整的测试代码:

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

called by: 叫做:

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

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

called by: 叫做:

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

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

(I am not the creator of this code) (我不是此代码的创建者)

it seems to me that you have some kind of static initializer in your CartLine which tried to open file stream. 在我看来,你的CartLine中有一些静态初始化程序试图打开文件流。

I will strong suggest you cleanup your code to make it unit-test friendly by removing such kind of static initializer logic. 我强烈建议你清理你的代码,通过删除这种静态初始化程序逻辑使其单元测试友好。 (It is usually more appropriate to make it instance method instead) (通常更适合使用实例方法)

Or you may have a dummy `cart.properties1 in your testing resources so that your CartLine can read that. 或者您可能在测试资源中有一个虚拟的`cart.properties1,以便您的CartLine可以读取它。

Or you may consider using PowerMock to see if it help to avoid such problem (I doubt anyway...) 或者您可以考虑使用PowerMock来查看它是否有助于避免此类问题(我怀疑无论如何......)

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

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