java PowerMockito ClassNotPreparedException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46624589/
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
PowerMockito ClassNotPreparedException
提问by Lazaro
I have the following jars:
我有以下罐子:
javax.servlet-api-3.1.9.jar
junit-4.10.jar
mockito-all-1.10.19.jar
mockito-core-1.10.19.jar
powermock-api-mockito-1.6.5.jar
powermock-api-mockito-common-1.6.5.jar
powermock-api-support-1.6.5.jar
powermock-core-1.6.5.jar
powermock-module-junit4-1.6.5.jar
powermock-reflect-1.6.5.jar
I want to test this method called createPanel
which is inside a class called Controller
:
我想测试这个名为的方法createPanel
,它在一个名为的类中Controller
:
public static void createPanel(HttpServletRequest req, HttpServletResponse res, HttpSession hs) throws IOException
{
PanelManager.createPanel(req, res, hs);
}
In the ControllerTest
class (junit tester), I have this:
在ControllerTest
课堂上(junit tester),我有这个:
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import ApplicationLogic.Controller;
import ApplicationLogic.PanelManager;
import ApplicationLogic.RegistrationManager;
import ApplicationLogic.SessionManager;
@PrepareForTest(PanelManager.class)
@RunWith(MockitoJUnitRunner.class)
public class ControllerTest {
Controller controller = Mockito.mock(Controller.class);
SessionManager sessionManager = Mockito.mock(SessionManager.class);
RegistrationManager registrationManager = Mockito.mock(RegistrationManager.class);
.
.
.
.
@Test
public void testcreatePanel() throws ServletException, IOException{
HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
HttpServletResponse res = Mockito.mock(HttpServletResponse.class);
HttpSession hs = Mockito.mock(HttpSession.class);
//PowerMockito.mock(SessionManager.class);
PrintWriter writer = new PrintWriter("somefile.txt");
when(res.getWriter()).thenReturn(writer);
PowerMockito.mockStatic(Controller.class);
PowerMockito.doCallRealMethod().when(controller).createPanel(req, res, hs);
All the tests for the other methods in the controller class are running fine. Why it is causing this error at PowerMockito.doCallRealMethod()
:
控制器类中其他方法的所有测试都运行良好。为什么会导致此错误PowerMockito.doCallRealMethod()
:
org.powermock.api.mockito.ClassNotPreparedException:
The class ApplicationLogic.Controller not prepared for test.
To prepare this class, add class to the '@PrepareForTest' annotation.
In case if you don't use this annotation, add the annotation on class or method level.
at org.powermock.api.mockito.expectation.reporter.MockitoPowerMockReporter.classNotPrepared(MockitoPowerMockReporter.java:31)
at org.powermock.api.mockito.internal.mockcreation.MockTypeValidatorFactory$DefaultMockTypeValidator.validate(MockTypeValidatorFactory.java:38)
at org.powermock.api.mockito.internal.mockcreation.AbstractMockCreator.validateType(AbstractMockCreator.java:18)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMock(MockCreator.java:57)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:47)
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:71)
at JUnitTest.ControllerTest.testcreatePanel(ControllerTest.java:253)
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.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access@Rule
public PowerMockRule rule = new PowerMockRule();
0(ParentRunner.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
回答by joao cunha jeronimo
It seems like you're missing the PowerMockRule
declaration:
似乎您缺少PowerMockRule
声明:
@PowerMockIgnore({"org.mockito.*"})
Also, keep in mind that you should let Powermock know that it shouldn't load mockito classes to the system class loader. Add the following code at the top of your class:
另外,请记住,您应该让 Powermock 知道它不应该将 mockito 类加载到系统类加载器。在类的顶部添加以下代码:
@PrepareForTest(PanelManager.class)
@RunWith(MockitoJUnitRunner.class)
@PowerMockIgnore({"org.mockito.*"})
public class ControllerTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
//...
}
Your class declaration should look like this:
您的类声明应如下所示:
@RunWith(PowerMockRunner.class)
@PrepareForTest({PanelManager.class})
public class ControllerTest {
Controller controller = Mockito.mock(Controller.class);
@Test
public void testcreatePanel() throws Exception {
HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
HttpServletResponse res = Mockito.mock(HttpServletResponse.class);
HttpSession hs = Mockito.mock(HttpSession.class);
PrintWriter writer = new PrintWriter("somefile.txt");
Mockito.when(res.getWriter()).thenReturn(writer);
PowerMockito.mockStatic(PanelManager.class);
PowerMockito.doCallRealMethod().when(controller).createPanel(req, res, hs);
}
}
回答by glytching
You have to use the PowerMockRunner
instead of the MockitoJUnitRuner
.
您必须使用PowerMockRunner
代替MockitoJUnitRuner
。
This example addresses the ClassNotPreparedException
:
这个例子解决了ClassNotPreparedException
:
Controller controller = Mockito.mock(Controller.class);
However, it looks like there might still be some issues even after that; you are mocking Controller and then treating it as a partial mock by invoking doCallRealMethod()
on it. Perhaps there is a genuine reason for this (maybe that reason would be revealed by showing more of your code) but if you do not have a genuine reason for that then you could remove this line:
但是,看起来即使在那之后仍然可能存在一些问题;您正在模拟 Controller,然后通过调用doCallRealMethod()
它来将其视为部分模拟。也许这有真正的原因(也许可以通过显示更多代码来揭示该原因),但如果您没有真正的原因,那么您可以删除此行:
PowerMockito.doCallRealMethod().when(controller).createPanel(req, res, hs);
And replace this line ...
并替换此行...
Controller.createPanel(req, res, hs);
with this:
有了这个:
##代码##This would then allow you to mock the behaviour of PanelManager
and test the actualbehaviour of Controller
in response to the PanelManager
's mocked behaviour.
这将允许您模拟 的行为PanelManager
并测试 的实际行为Controller
以响应 的模拟PanelManager
行为。