前提・実現したいこと
JUnitでMockitoによるDynamoDBのstubを書いているのですが、下記エラーが発生してテストを通すことができません。どのようにテストコードを書けば良いでしょうか。もしくは解消にあたってのデバッグ方法等共有いただけますと幸いです。
ローカルDynamoDBによるテストではなく、Mockを使った実装をしたいと考えています。
類似した質問は下記Stackoverflowで確認しているものの、答えが出ておらず解決できていない状況となります。
a Easier DynamoDB local testing
実装例としては以下のようなものとなります。
発生している問題・エラーメッセージ
------------------------------------------------------- T E S T S ------------------------------------------------------- Running sample.tools.mydic.MyDicTest Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.176 sec <<< FAILURE! initializationError(sample.tools.mydic.MyDicTest) Time elapsed: 0.013 sec <<< ERROR! java.lang.ExceptionInInitializerError at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17) at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:41) at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:30) at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18) at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:66) at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24) at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:12) at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:11) at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24) at org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues.<init>(ReturnsEmptyValues.java:58) at org.mockito.internal.stubbing.defaultanswers.ReturnsMoreEmptyValues.<init>(ReturnsMoreEmptyValues.java:51) at org.mockito.internal.stubbing.defaultanswers.ReturnsSmartNulls.<init>(ReturnsSmartNulls.java:40) at org.mockito.Answers.<clinit>(Answers.java:43) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at java.base/java.lang.Class.getEnumConstantsShared(Class.java:3553) at java.base/java.lang.Class.enumConstantDirectory(Class.java:3575) at java.base/java.lang.Enum.valueOf(Enum.java:234) at java.base/sun.reflect.annotation.AnnotationParser.parseEnumValue(AnnotationParser.java:484) at java.base/sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:348) at java.base/java.lang.reflect.Method.getDefaultValue(Method.java:674) at java.base/sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:132) at java.base/sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85) at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:267) at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:121) at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:73) at java.base/java.lang.reflect.Field.declaredAnnotations(Field.java:1175) at java.base/java.lang.reflect.Field.declaredAnnotations(Field.java:1173) at java.base/java.lang.reflect.Field.getDeclaredAnnotations(Field.java:1161) at java.base/java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:533) at org.junit.runners.model.FrameworkField.getAnnotations(FrameworkField.java:41) at org.junit.runners.model.TestClass.addToAnnotationLists(TestClass.java:84) at org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:71) at org.junit.runners.model.TestClass.<init>(TestClass.java:57) at org.junit.runners.JUnit4.<init>(JUnit4.java:23) at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70) at org.junit.internal.requests.ClassRequest.createRunner(ClassRequest.java:28) at org.junit.internal.requests.MemoizingRequest.getRunner(MemoizingRequest.java:19) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:250) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75) Caused by: java.lang.NullPointerException at org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:17) at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21) ... 55 more
該当のソースコード
java
1 public List<Word> getSpecifiedWords(List<Word> words) { 2 List<Word> dic = new ArrayList<Word>(); 3 if (words.size() == 0) return dic; 4 5 for (Word word : words) { 6 Map<String,AttributeValue> keyToGet = new HashMap<String,AttributeValue>(); 7 keyToGet.put(keyCol, AttributeValue.builder() 8 .s(word.key).build()); 9 GetItemRequest request = GetItemRequest.builder() 10 .key(keyToGet) 11 .tableName(tableName) 12 .build(); 13 14 try { 15 Map<String,AttributeValue> returnedItem = ddb.getItem(request).item(); 16 17 if (returnedItem != null && !returnedItem.keySet().isEmpty()) { 18 String key = returnedItem.get(keyCol).s(); 19 String value = returnedItem.get(valCol).s(); 20 dic.add(new Word(key, value)); 21 } 22 } catch (DynamoDbException e) { 23 System.err.println(e.getMessage()); 24 System.exit(1); 25 } 26 } 27 return dic; 28 }
public class MyDicTest { final Region region = Region.US_WEST_2; final String tableName = "mydic"; final static String keyCol = "word"; final static String valCol = "meaning"; String testKey; String testValue; @InjectMocks private MyDic mydic; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private DynamoDbClient ddb; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Map<String,AttributeValue> map; @Before public void setUp() throws Exception { byte[] array = new byte[7]; new Random().nextBytes(array); String generatedString = new String(array, Charset.forName("UTF-8")); testKey = "foo" + generatedString; testValue = "bar" + generatedString; MockitoAnnotations.initMocks(this); } @Test public void testGetSpecifiedWords() { MyDic m = new MyDic(); Word word = m.new Word("foo3"); List<Word> dic = new ArrayList<Word>(); dic.add(word); Map<String,AttributeValue> keyToGet = new HashMap<String,AttributeValue>(); keyToGet.put(keyCol, AttributeValue.builder() .s("foo3").build()); GetItemRequest request = GetItemRequest.builder() .key(keyToGet) .tableName(tableName) .build(); Mockito.when(ddb.getItem(request).item()).thenReturn(map); Mockito.when(map.keySet().isEmpty()).thenReturn(false); Mockito.when(map.get(keyCol).s()).thenReturn("foo3"); Mockito.when(map.get(valCol).s()).thenReturn("bar3"); List<Word> ans = mydic.getSpecifiedWords(dic); List<Word> target = new ArrayList<Word>(Arrays.asList(m.new Word("foo3", "bar3"))); assertTrue(ans.get(0).equals(target.get(0))); } }
補足情報(FW/ツールのバージョンなど)
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.10.19</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-inline</artifactId> <version>3.3.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> <scope>test</scope> </dependency>
回答1件
あなたの回答
tips
プレビュー