2017-10-07 2 views
0

나는 다음과 같은 병을 가지고PowerMockito ClassNotPreparedException

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 

내가 Controller라는 클래스 내에 createPanel 불리는이 방법을 테스트하려면 다음 ControllerTest 클래스 (JUnit을 테스터)에서

public static void createPanel(HttpServletRequest req, HttpServletResponse res, HttpSession hs) throws IOException 
{ 
    PanelManager.createPanel(req, res, hs); 
} 

을, 나는 이것을 가지고있다 :

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); 

모든 테스트 컨트롤러 클래스의 다른 메서드에 대해 잘 실행되고 있습니다. 이 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) 

답변

1

을 대신 MockitoJUnitRunerPowerMockRunner를 사용해야합니다. 여전히도 그 이후로 몇 가지 문제가있을 수 있습니다 것 같습니다, 그러나

@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); 
    } 
} 

;

이 예는 ClassNotPreparedException 주소 컨트롤러를 조롱하고 나서 doCallRealMethod()을 호출하여 부분 모의로 처리합니다. 아마도 (아마도 그 이유는 코드의 이상을 보여 밝혀 질 것이다) 이것에 대한 진짜 이유가 있지만 그에 대한 진짜 이유가없는 경우에 당신은이 줄을 제거 할 수 있습니다 :

Controller controller = Mockito.mock(Controller.class); 

을 그리고이 교체 라인 ...이와

PowerMockito.doCallRealMethod().when(controller).createPanel(req, res, hs); 

는 :

Controller.createPanel(req, res, hs); 

이 다음 PanelManager의 행동을 조롱하고 실제를 테스트 할 것 PanelManager의 조롱 된 동작에 대한 응답으로의 동작이 Controller입니다.

+0

감사합니다. 지금 귀하의 답변을 보았습니다. 그런데 왜 그렇게 될까요? 'doCallRealMethod()'를 사용하지 않기 때문에 중요하지 않습니다. – Lazaro

+0

@Lazaro 당신은 "그게 왜 효과가 있겠 어?"라고 물었습니다. "그"가 무엇을 말하는지 나는 명확하지 않습니다. 이 부분에서 대답 : "MockitoJUnitRuner 대신 PowerMockRunner 사용"은 ClassNotPreparedException을 수정합니다. 내 답변에있는 다른 제안은 "PowerMockito.doCallRealMethod()"를 사용하지 않고 대신 실제 Controller 인스턴스를 사용하는 것을 피합니다. – glytching

관련 문제