Java 找不到 PowerMock 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18913204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:17:05  来源:igfitidea点击:

PowerMock class not found

javajunitmockitopowermock

提问by bas

For some reason I fail to follow a pretty straight forward PowerMock example.

出于某种原因,我没有遵循一个非常直接的 PowerMock 示例。

I included powermock-mockito-1.5.1-fullin my classpath, and I try to test a public final method (following thisexample).

我包含powermock-mockito-1.5.1-full在我的类路径中,并尝试测试公共最终方法(按照示例)。

For some reason I am not able to make the import to the PowerMockclass.

出于某种原因,我无法导入PowerMock课程。

import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.cleancode.lifesaver.camera.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(android.hardware.Camera.class)
public class CameraTests {

    private android.hardware.Camera _cameraMock;

    @Before
    public void setUp() {
        _cameraMock = PowerMockito.mock(android.hardware.Camera.class);
    }

    @Test 
    public void releaseCamera() {
        ICamera camera = new Camera(_cameraMock);

        // Compile error: PowerMock can't be resolved       
        PowerMock.replay(_cameraMock);
        // I also tried PowerMockito.replay(_cameraMock) but that also doesn't exist.

        camera.release();

        Mockito.verify(_cameraMock).release();
    }
}

As the comment explains, the PowerMockclass can't be imported from the power mock jar.

正如评论所解释的那样,PowerMock无法从 power mock jar 导入该类。

It feels like a silly question, but I really can't find anything on the internet.

感觉这是一个愚蠢的问题,但我真的在互联网上找不到任何东西。

Where should I be able to find the static class PowerMock? I also used Java Decompile to search the powermock library, no hits on powermock / replay.

我应该在哪里可以找到静态类PowerMock?我还使用 Java Decompile 来搜索 powermock 库,在 powermock / replay 上没有命中。

采纳答案by Morfic

The example you are following PowerMock.replay(_cameraMock);is using EasyMock, while you seem to be wanting Mockito. Take a look at this tutorialfor mockito & power mock

您所遵循的示例PowerMock.replay(_cameraMock);是使用 EasyMock,而您似乎想要 Mockito。看看本教程,了解 mockito 和 power mock

回答by Gianmarco

I suggest you not to create your mock in your setUp()(Before) method, because a mock is very complicated, for example you can tell it exactly how many time it should expect a method is called, if you declare a "general" mock for all your tests it's very difficult to control this behaviour.

我建议你不要在你的setUp()(Before) 方法中创建你的模拟,因为模拟非常复杂,例如,如果你为所有人声明一个“通用”模拟,你可以准确地告诉它应该期望一个方法被调用多少次您的测试很难控制这种行为。

maybe (without the code I can only guess) you want that your android.hardware.Camerais called inside your Camera.release()method, am I right? so I whould do like this:

也许(没有我只能猜测的代码)您希望android.hardware.Camera在您的Camera.release()方法中调用您的方法,对吗?所以我应该这样做:

The method you are trying to mock is not static, it's a normal final method. You can try to do this:

您尝试模拟的方法不是静态的,而是正常的最终方法。你可以尝试这样做:

android.hardware.Camera mock = PowerMock.createMock(android.hardware.Camera.class);
PowerMock.expect(mock.release());
PowerMock.replay();

ICamera camera = new Camera(mock);
camera.release();
PowerMock.verify(mock);

if inside camera.relase()is not called exactly once the android.hardware.Camera.release()method the test fails.

如果测试失败后camera.relase()没有完全调用insideandroid.hardware.Camera.release()方法。