我知道有吨类似的问题在SO,但我无法找到任何解决方案。我有这样的服务类。其中有一个仓库方法调用,返回void。我已经创建了一个这个repo类的模拟对象。并注入到autowired服务类中,但我得到了“想要但未调用..实际上,与此mock的交互为零。”我已经验证了存储库确实被调用。
` @Service
public class DecileConfigService
{
@Autowired
private ItemAdditionalInfoRepository itemAdditionalInfoRepository;
@Transactional
public Boolean triggerBatchJob(ItemAdditionalInfo existingIAI, ItemAdditionalInfo latestIAI, DecileConfig existingDC, DecileConfig latestDC, String decileNbr, String decileType,String tenantId, String isBulkUpload)
{
if(!existingIAI.equals(latestIAI))
{
System.out.println("Callled ::::::");
itemAdditionalInfoRepository.updateAllocationStatusByItemAndTenantId(ItemSetupAllocationStatus.PROGRESS.getValue(),Integer.valueOf(decileNbr),lastUpdatedBy,lastUpdatedOn,tenantId);
}
}
}`
”
Test class :
@Mock
ItemAdditionalInfoRepository itemAdditionalInfoRepository;
@Autowired
@InjectMocks
private DecileConfigService decileConfigService;`
@Test
public void testRepo() throws Exception
{
ItemAdditionalInfo oldIad = DatabricksUtil.createItem();
oldIad.setVendorNbrFull(12);
ItemAdditionalInfo newIad = DatabricksUtil.createItem();
newIad.setVendorNbrFull(13);
DecileConfig dc = DatabricksUtil.createDecile();
decileConfigService.triggerBatchJob(oldIad, newIad, dc, dc, "200", "item", ConstantsUtil.tenantIdValueUS,"false");
//doNothing().when(itemAdditionalInfoRepository).updateAllocationStatusByDeptAndTenantId(ItemSetupAllocationStatus.PROGRESS.getValue(), Integer.valueOf("200"),newIad.getLastUpdatedBy(),newIad.getLastUpdatedOn(), newIad.getTenantId());
verify(itemAdditionalInfoRepository,times(1)).updateAllocationStatusByDeptAndTenantId(ItemSetupAllocationStatus.PROGRESS.getValue(), Integer.valueOf("200"),newIad.getLastUpdatedBy(),newIad.getLastUpdatedOn(), newIad.getTenantId());
}`
日志:
Callled :::::: Hibernate: update item_additional_info set allocation_status=?, last_updated_by=?, last_updated_on=? where item_nbr=? and tenant_id=? 27/May/2023 15:47:34:909 [main] INFO com.walmart.sams.services.allocation.configuration.service.TriggerJobService:59 - info env dev-test url test jobId testid params {decileLevelType=item, autoUpdate=false, decileLevelNumber=200, triggerBy=null, source=Single Edit Trigger, isBulkUpload=false} 27/May/2023 15:47:34:910 [main] INFO com.walmart.sams.services.allocation.configuration.service.TriggerJobService:61 - info env dev-test url test jobName testid params {decileLevelType=item, autoUpdate=false, decileLevelNumber=200, triggerBy=null, source=Single Edit Trigger, isBulkUpload=false} 27/May/2023 15:47:34:989 [main] ERROR com.walmart.sams.services.allocation.configuration.service.TriggerJobService:34 - Error {} java.lang.IllegalArgumentException: URI is not absolute at java.net.URL.fromURI(URL.java:692) ~[?:?] at java.net.URI.toURL(URI.java:1116) ~[?:?] at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145) ~[spring-web-5.3.27.jar:5.3.27] at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:98) ~[spring-web-5.3.27.jar:5.3.27] at com.walmart.platform.txn.springboot.interceptor.SpringBootClientTxnMarkingInterceptor.intercept(SpringBootClientTxnMarkingInterceptor.java:82) ~[strati-af-txn-marking-springboot-client-4.5.9.jar:?] at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:93) ~[spring-web-5.3.27.jar:5.3.27] at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:77) ~[spring-web-5.3.27.jar:5.3.27] at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.3.27.jar:5.3.27] at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66) ~[spring-web-5.3.27.jar:5.3.27] at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:782) ~[spring-web-5.3.27.jar:5.3.27] at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:717) ~[spring-web-5.3.27.jar:5.3.27] at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:474) ~[spring-web-5.3.27.jar:5.3.27] at com.walmart.sams.services.allocation.configuration.service.TriggerJobService.runDatabricksJob(TriggerJobService.java:75) ~[classes/:?] at com.walmart.sams.services.allocation.configuration.service.TriggerJobService.runBatchJob(TriggerJobService.java:31) [classes/:?] at com.walmart.sams.services.allocation.configuration.service.DecileConfigService.triggerBatchJob(DecileConfigService.java:496) [classes/:?] at com.walmart.sams.services.allocation.configuration.service.DecileConfigService$$FastClassBySpringCGLIB$$1122e4d.invoke(<generated>) [classes/:?] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) [spring-core-5.3.27.jar:5.3.27] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793) [spring-aop-5.3.27.jar:5.3.27] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) [spring-aop-5.3.27.jar:5.3.27] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) [spring-aop-5.3.27.jar:5.3.27] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) [spring-tx-5.3.27.jar:5.3.27] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) [spring-tx-5.3.27.jar:5.3.27] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) [spring-tx-5.3.27.jar:5.3.27] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.27.jar:5.3.27] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) [spring-aop-5.3.27.jar:5.3.27] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) [spring-aop-5.3.27.jar:5.3.27] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.27.jar:5.3.27] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) [spring-aop-5.3.27.jar:5.3.27] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708) [spring-aop-5.3.27.jar:5.3.27] at com.walmart.sams.services.allocation.configuration.service.DecileConfigService$$EnhancerBySpringCGLIB$$8cc50e9e.triggerBatchJob(<generated>) [classes/:?] at com.walmart.sams.services.allocation.configuration.test.DatabricksJobServiceTest.testRepo(DatabricksJobServiceTest.java:163) [test-classes/:?] at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?] at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?] at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) [junit-4.13.2.jar:4.13.2] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.13.2.jar:4.13.2] at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) [junit-4.13.2.jar:4.13.2] at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.13.2.jar:4.13.2] at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74) [spring-test-5.3.27.jar:5.3.27] at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84) [spring-test-5.3.27.jar:5.3.27] at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) [spring-test-5.3.27.jar:5.3.27] at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) [spring-test-5.3.27.jar:5.3.27] at org.mockito.internal.junit.VerificationCollectorImpl$1.evaluate(VerificationCollectorImpl.java:37) [mockito-core-4.5.1.jar:?] at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) [spring-test-5.3.27.jar:5.3.27] at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) [junit-4.13.2.jar:4.13.2] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251) [spring-test-5.3.27.jar:5.3.27] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.3.27.jar:5.3.27] at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) [junit-4.13.2.jar:4.13.2] at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) [junit-4.13.2.jar:4.13.2] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) [junit-4.13.2.jar:4.13.2] at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) [junit-4.13.2.jar:4.13.2] at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) [junit-4.13.2.jar:4.13.2] at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.3.27.jar:5.3.27] at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.3.27.jar:5.3.27] at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) [junit-4.13.2.jar:4.13.2] at org.junit.runners.ParentRunner.run(ParentRunner.java:413) [junit-4.13.2.jar:4.13.2] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.3.27.jar:5.3.27] at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.13.2.jar:4.13.2] at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) [junit-rt.jar:?] at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) [junit-rt.jar:?] at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) [junit-rt.jar:?] at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) [junit-rt.jar:?]
org.mockito.exceptions.base.MockitoAssertionError: There were multiple verification failures: Wanted but not invoked: itemAdditionalInfoRepository.updateAllocationStatusByDeptAndTenantId(
"In progress", 200, null, null,
"sams_us" ); at com.walmart.sams.services.allocation.configuration.test.DatabricksJobServiceTest.testRepo(DatabricksJobServiceTest.java:165) Actually, there were zero interactions with this mock.
1条答案
按热度按时间smdncfj31#
你必须做出选择:你想实现单元测试还是集成测试?
1.集成测试
当您使用@SpringBootTest annotation时,应用程序中的所有Spring bean都将示例化并创建Spring ApplicationContext。您正在创建一个集成测试。
如果你需要在测试中操作一些Spring bean,你可以使用@Autowire来注入你的bean:
如果您需要在集成测试中模拟一些Spring bean,请为您的测试创建一个配置类,替换您想要在其中模拟的bean:
并在测试类上添加ActiveProfiles注解:
@ActiveProfiles(value={"decile-test"})
现在,您的“ItemAdditionalInfoRepository”类型的“普通”Springbean被应用程序上下文中的Mock替换,并且可以“自动连接”。
1.单元测试
如果你只想测试你的“DecileConfigService”类,而不是所有的SpringBoot应用程序,在应用程序上下文中使用所有的Spring bean,不要在测试类上设置“@SpringBootTest”,并使用Mockito注入模拟对象。
考虑到您的测试类的内容,我认为您正在进行单元测试。因此,不要使用@SpringBootTest注解。