- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 jar :
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
我想测试这个名为 createPanel
的方法,它位于一个名为 Controller
的类中:
public static void createPanel(HttpServletRequest req, HttpServletResponse res, HttpSession hs) throws IOException
{
PanelManager.createPanel(req, res, hs);
}
在 ControllerTest
类(junit 测试器)中,我有这个:
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);
Controller 类中其他方法的所有测试都运行良好。为什么它在 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$1.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$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.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)
最佳答案
您必须使用 PowerMockRunner
而不是 MockitoJUnitRuner
。
此示例解决了 ClassNotPreparedException
:
@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);
}
}
但是,看起来即使在那之后可能仍然存在一些问题;您正在模拟 Controller,然后通过在其上调用 doCallRealMethod()
将其视为部分模拟。也许这有一个真正的原因(也许这个原因会通过显示更多你的代码来揭示)但如果你没有真正的原因那么你可以删除这一行:
Controller controller = Mockito.mock(Controller.class);
并替换这一行...
PowerMockito.doCallRealMethod().when(controller).createPanel(req, res, hs);
用这个:
Controller.createPanel(req, res, hs);
这将允许您模拟 PanelManager
的行为并测试 Controller
的实际行为以响应 PanelManager
的模拟行为。
关于java - PowerMockito ClassNotPreparedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46624589/
我有以下 jar : javax.servlet-api-3.1.9.jar junit-4.10.jar mockito-all-1.10.19.jar mockito-core-1.10.19.j
我使用Testng&Mockito&Powermock(不使用Junit 3或4)来测试我的类JeebiesKwCheckUtil,其中包含静态和最终,我已经将代码和pom作为Powermock介绍,
我正在编写一个单元测试来模拟 verticle 中的静态方法,但总是收到 ClassNotPreparedException 。我认为只有类是静态的,但我有非静态类时,才可以以这种方式进行模拟。我错过
我有一个示例测试类,我想在其中模拟一个静态类。我的 build.gradle 就像 testCompile group: 'org.junit.jupiter', name: 'junit-jupit
我有以下测试... import org.scalatest.junit.JUnitRunner ... @PowerMockRunnerDelegate(classOf[JUnitRunner])
我有一个图像加载器类,我想在其中测试一些静态方法。由于 Mockito 不支持静态方法,我切换到 Power Mockito。但是我正在测试的静态方法有一个方法调用 Base64.encodeToS
我有一个图像加载器类,我需要在其中测试一些静态方法。由于 Mockito 不支持静态方法,我切换到 Power Mockito。但是我正在测试的静态方法有一个方法调用 Base64.encodeTo
我是一名优秀的程序员,十分优秀!