2013-06-11 2 views
0

: 나는 ClassB가 개체로 null과 방법을 실행하려고 할 때까지Powermock 화이트 박스 호출 I 개인 방법을 테스트하고 다음과 같은 설정이려고

 
public class MyClass { 
    private boolean myprivatemethod(ClassB classBObject, boolean b) { 
    // do stuff here 
    someOtherMethod(); 
    } 

    private void someOtherMethod() { 
    // more stuff 
    } 
} 

final MyClass testsubject = PowerMockito.spy(new MyClass()); 
// spy is needed because "someOtherMethod" is mocked 
// which is not shown here for simplicity 
ClassB classBObject = mock(ClassB.class); 

boolean result = Whitebox.invokeMethod(testsubject, "myprivatemethod", classBObject, true); 

이것은 좋은 작동합니다

 
boolean result = Whitebox.invokeMethod(testsubject, "myprivatemethod", null, true); 

이 다음과 같은 예외가 발생합니다 난

 
java.lang.NullPointerException 
    at java.lang.Class.isAssignableFrom(Native Method) 
    at org.powermock.reflect.internal.WhiteboxImpl.checkIfParameterTypesAreSame(WhiteboxImpl.java:2432) 
    at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1934) 
    at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:1025) 
    at org.powermock.reflect.internal.WhiteboxImpl.findMethodOrThrowException(WhiteboxImpl.java:948) 
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:882) 
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:713) 
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401) 
    at test.myproject.TestMyClass.testMyClass(TestMyClass.java:10) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at junit.framework.TestCase.runTest(TestCase.java:154) 
    at junit.framework.TestCase.runBare(TestCase.java:127) 
    at junit.framework.TestResult$1.protect(TestResult.java:106) 
    at junit.framework.TestResult.runProtected(TestResult.java:124) 
    at junit.framework.TestResult.run(TestResult.java:109) 
    at junit.framework.TestCase.run(TestCase.java:118) 
    at junit.framework.TestSuite.runTest(TestSuite.java:208) 
    at junit.framework.TestSuite.run(TestSuite.java:203) 
    at org.powermock.modules.junit3.internal.impl.PowerMockJUnit3RunnerDelegateImpl.run(PowerMockJUnit3RunnerDelegateImpl.java:113) 
    at org.powermock.modules.junit3.internal.impl.JUnit3TestSuiteChunkerImpl.run(JUnit3TestSuiteChunkerImpl.java:165) 
    at org.powermock.modules.junit3.PowerMockSuite.run(PowerMockSuite.java:51) 
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) 
    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) 

Powermockito 1.5를 사용합니다. null을 매개 변수로 사용하여 메서드를 실행할 수 있습니까?

+0

도움이 되었습니까? –

+0

내가 할 수있는 한 빨리 답변을 시도하겠습니다. 시간이 있기까지는 며칠이 걸릴 수 있습니다. ;-) – kel

답변

1

인수를 사용해보십시오 대신 정합 : 당신이 모든 인수를위한 매처 (Matchers)를 사용하면

import static org.mockito.Matchers.*; 

//... 

boolean result = Whitebox.invokeMethod(testsubject, 
    "myprivatemethod", isNull(ClassB.class), eq(true)); 

, 당신은 모든 인수를 사용해야합니다. 이것은 Mockito 문서 here에 명시되어 있습니다 (하위 섹션 인수 matchers에 대한 경고 참조).

1

어쩌면이 시도 : 다음

Object[] params = {null, true}; 

:

Whitebox.invokeMethod(testsubject, "myprivatemethod", params); 
0

PowerMock는 매개 변수 유형과 일치 할 수 없습니다. 이 매개 변수 유형을 명시 적으로 말할 수 있습니다. -

ClassB classBObject = null; 
boolean result = Whitebox.invokeMethod(testsubject, 
     "myprivatemethod", new Class<?>[] { ClassB.class,Boolean.class},classBObject,true);