どなたか、解決策をご教示いただけますでしょうか。
Javaにて、JUnit+PowerMockでテストを行っております。
privateメソッド(下ソース:SampleTarget.setDto)をMock化しようとしたところ、
SamplePropTest#test001 メソッドの
. PowerMockito.when(mockInstance, MemberMatcher.method(SampleTarget.class, "setDto", ant(), any()))
.withNoArguments().thenReturn(mockDto);
の場所で、以下のエラーが発生してしまいました。
エラーの内容見た限り引数のany()に原因があるようですが、any()以外に当てはまる引数の書き方があるでしょうか?
お手数をおかけいたしますが、よろしくお願いいたします。
発生している問題・エラーメッセージ
java
1org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 2Misplaced or misused argument matcher detected here: 3 4-> at 5com.sample.sampletest(sampleTest.java:10) 6-> at 7com.sample.sampletest(sampleTest.java:10) 8 9You cannot use argument matchers outside of verification or stubbing. 10Examples of correct usage of argument matchers: 11 when(mock.get(anyInt())).thenReturn(null); 12 doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); 13 verify(mock).someMethod(contains("foo")) 14 15This message may appear after an NullPointerException if the last 16matcher is returning an object 17like any() but the stubbed method signature expect a primitive argument, 18in this case, 19use primitive alternatives. 20 when(mock.get(any())); // bad use, will raise NPE 21 when(mock.get(anyInt())); // correct usage use 22 23Also, this error might show up because you use argument matchers with 24methods that cannot be mocked. 25Following methods *cannot* be stubbed/verified: 26final/private/equals()/hashCode(). 27Mocking methods declared on non-public parent classes is not supported. 28 29 at 30org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:82) 31 at 32org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:69) 33 at 34org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.afterTestMethod(ResetMocksTestExecutionListener.java:63) 35 at 36org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:441) 37 at 38org.springframework.test.context.junit.jupiter.SpringExtension.afterEach(SpringExtension.java:140) 39 at 40org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAfterEachCallbacks$11(TestMethodTestDescriptor.java:255) 41 at 42org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$12(TestMethodTestDescriptor.java:271) 43 at 44org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 45 at 46org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:271) 47 at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) 48 at 49org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:270) 50 at 51org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachCallbacks(TestMethodTestDescriptor.java:254) 52 at 53org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:143) 54 at 55org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71) 56 at 57org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135) 58 at 59org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 60 at 61org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) 62 at 63org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) 64 at 65org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) 66 at 67org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 68 at 69org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) 70 at 71org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) 72 at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) 73 at 74org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) 75 at 76org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) 77 at 78org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 79 at 80org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) 81 at 82org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) 83 at 84org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) 85 at 86org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 87 at 88org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) 89 at 90org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) 91 at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) 92 at 93org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) 94 at 95org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) 96 at 97org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 98 at 99org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) 100 at 101org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) 102 at 103org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) 104 at 105org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 106 at 107org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) 108 at 109org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) 110 at 111org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) 112 at 113org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) 114 at 115org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) 116 at 117org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248) 118 at 119org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211) 120 at 121org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226) 122 at 123org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199) 124 at 125org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:141) 126 at 127org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98) 128 at 129org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) 130 at 131org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542) 132 at 133org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770) 134 at 135org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464) 136 at 137org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210) 138Destroy method on bean with name 'restapiLifeCycle' threw an exception: 139java.lang.NullPointerException
該当のソースコード
java
1package sample; 2 3import Sample1; 4import Smaple2; 5 6/** 7 * テスト対象クラス. 8 */ 9public class SampleTarget { 10 11 private Dto setDto(Sample1 sample1, Smaple2 sample2) 12 throws Exception { 13 Dto dto = new Dto(); 14 15 dto.setAAA(sample1.AAA); 16 dto.setBBB(sample2.BBB); 17 18 return dto; 19 } 20}
java
1@RunWith(PowerMockRunner.class) 2@PrepareForTest({SampleTarget.class}) 3public class SamplePropTest extends TestCase{ 4 5 @Test 6 public void test001() throws Exception{ 7 8 Dto mockDto = new Dto(); 9 mockDto.setAAA(aaa); 10 mockDto.setBBB(bbb) 11 12 SampleTarget mockInstance = PowerMockito.mock(SampleTarget.class); 13 PowerMockito.when(mockInstance, MemberMatcher.method(SampleTarget.class, "setDto", ant(), any())) 14 .withNoArguments().thenReturn(mockDto); 15 16 // privateメソッドにアクセスするためにリフレクションを使う 17 Method method = SampleTarget.class.getDeclaredMethod("setDto"); 18 method.setAccessible(true); 19 20 }
補足情報
・JDK:11.0.2
・JUnit:4.12
・Eclipse:4.16
・org.mockito:mockito-core:2.+
・org.powermock:powermock-module-junit4:2.0.0-RC.4
・org.powermock:powermock-api-mockito2:2.0.0-RC.4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/14 04:05
2021/02/14 04:17
2021/02/14 04:53