PowerMockito Spyを使用して同じクラスのプライベートメソッド「mapCustomerToRule」をモックしようとしましたが、取得できません。それはNoSuchMethodErrorを与えます。
しかし、それでもプライベートメソッドの呼び出しが行われ、その結果、別のthirdPartCallが行われます。 thirdPartyCallが例外をスローすると問題が発生します。私が理解している限り、「mapCustomerToRule」をモックしている場合、メソッド実装の詳細に入り込んでモック応答を返すことはできません。
public Map<String, List<Rule>> fetchCutomerToRules() {
List<ClaimRuleEntity> listOfDHLClaimsRule = dhlClaimRuleRepository.findAllByOrderByRulePriority();
if (Objects.isNull(listOfDHLClaimsRule) || listOfDHLClaimsRule.isEmpty()) {
return null;
}
log.info("claim rules fetched from database");
List<ClaimRuleCustomerEntity> listOfDHLClaimRuleCustomerEntity = dhlClaimRuleCustomerRepository.findAllByEnabledFlagTrue();
if (Objects.isNull(listOfDHLClaimRuleCustomerEntity) || listOfDHLClaimRuleCustomerEntity.isEmpty()) {
return null;
}
log.info("claim customers fetched from database");
return mapCustomerToRule(listOfDHLClaimsRule, listOfDHLClaimRuleCustomerEntity);
}
private Map<String, List<Rule>> mapCustomerToRule(List<ClaimRuleEntity> listOfDHLClaimsRule,
List<ClaimRuleCustomerEntity> listOfDHLClaimRuleCustomerEntity) {
log.info("mapping started for claim rules to customer");
Map<String, List<Rule>> cutomerToRules = new HashMap<>();
listOfDHLClaimRuleCustomerEntity.forEach(dhlClaimRuleCustomerEntity -> {
String customer = dhlClaimRuleCustomerEntity.getCustomer();
List<Rule> rules = cutomerToRules.get(customer);
if (Objects.isNull(rules)) {
rules = new ArrayList<>();
cutomerToRules.put(customer, rules);
}
for (ClaimRuleEntity dhlClaimRule : listOfDHLClaimsRule) {
if (dhlClaimRule.getRuleId() == dhlClaimRuleCustomerEntity.getRuleId()) {
Rule rule = new Rule();
rule.setRuleId(dhlClaimRule.getRuleId());
rule.setRuleValue(dhlClaimRuleCustomerEntity.getRuleValue());
rule.setRulePriority(dhlClaimRule.getRulePriority());
rule.setEnabled(dhlClaimRuleCustomerEntity.getEnabledFlag());
rule.setRuleDescription(dhlClaimRule.getRuleDescription());
rules.add(rule);
break;
}
}
});
log.info("mapping completed for claim rules to customer");
return cutomerToRules;
}
これは私がクラスのために書いたジュントです
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest(FetchUpdateClaimService.class)
@ContextConfiguration(classes = ProcessClaimTestConfiguration.class)
public class FetchUpdateClaimServiceTest {
@Autowired
FetchUpdateClaimService fetchUpdateClaimService;
@Autowired
ClaimRuleRepository dhlClaimRuleRepository;
@Autowired
ClaimRuleCustomerRepository dhlClaimRuleCustomerRepository;
@Autowired
ClaimRepository dhlClaimRepository;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void fetchCutomerToRules_should_pass_Test() throws Exception {
FetchUpdateClaimService fetchUpdateClaimService = PowerMockito.spy(this.fetchUpdateClaimService);
List<ClaimRuleEntity> listOfDHLClaimsRule = new ArrayList<>();
ClaimRuleEntity claimRuleEntity = new ClaimRuleEntity();
listOfDHLClaimsRule.add(claimRuleEntity);
List<ClaimRuleCustomerEntity> listOfDHLClaimRuleCustomerEntity = new ArrayList<>();
ClaimRuleCustomerEntity claimRuleCustomerEntity = new ClaimRuleCustomerEntity();
listOfDHLClaimRuleCustomerEntity.add(claimRuleCustomerEntity);
Map<String, List<Rule>> map = new HashMap<>();
Mockito.when(dhlClaimRuleRepository.findAllByOrderByRulePriority()).thenReturn(listOfDHLClaimsRule);
Mockito.when(dhlClaimRuleCustomerRepository.findAllByEnabledFlagTrue()).thenReturn(listOfDHLClaimRuleCustomerEntity);
PowerMockito.doReturn(map).when(fetchUpdateClaimService,"mapCustomerToRule",Mockito.eq(List.class),Mockito.eq(List.class));
this.fetchUpdateClaimService.fetchCutomerToRules();
}
}
エラースタック:-
Java.lang.NoSuchMethodError: org.mockito.internal.handler.MockHandlerFactory.createMockHandler(Lorg/mockito/mock/MockCreationSettings;)Lorg/mockito/internal/InternalMockHandler;
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMethodInvocationControl(DefaultMockCreator.Java:114)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.Java:69)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.Java:46)
at org.powermock.api.mockito.PowerMockito.spy(PowerMockito.Java:207)
at com.morrison.process.claim.service.FetchUpdateClaimServiceTest.fetchCutomerToRules_should_pass_Test(FetchUpdateClaimServiceTest.Java:54)
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
at Java.lang.reflect.Method.invoke(Method.Java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.Java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.Java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.Java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.Java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.Java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.Java:83)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.Java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.Java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.Java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.Java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.Java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.Java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.Java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.Java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.Java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.Java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.Java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.Java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.Java:190)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$2.call(DelegatingPowerMockRunner.Java:149)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$2.call(DelegatingPowerMockRunner.Java:141)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.withContextClassLoader(DelegatingPowerMockRunner.Java:132)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.run(DelegatingPowerMockRunner.Java:141)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.Java:121)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.Java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.Java:59)
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:538)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:760)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.Java:460)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.Java:206)
私のPom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.Apache.camel</groupId>
<artifactId>camel-aws</artifactId>
<version>${Apache.camel.aws.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
</dependencies>
次のようにアップグレードしてみてください。
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
以下も参照してください。