在STS中独立运行时,Junit测试通过,但在使用Maven构建运行时失败。
我用来构建的命令是:
- mvn clean jacoco:prepare-agent install
我遇到了下面提到的错误。
- mvn全新安装
当我在第一个命令之后运行第二个命令时,虽然测试通过了,但是部分代码没有涵盖。
我尝试使用并不是完全有帮助的maven-surefire-plugin的forkMode。
package com.dbs.unittesting;
import static org.junit.Assert.assertEquals;
import java.net.URISyntaxException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.RestTemplate;
import com.dbs.beans.request.internal.InternalRequest;
import com.dbs.beans.response.FinalResponse;
import com.dbs.beans.response.InternalResponse;
import com.dbs.common.ResponseInfo;
import com.dbs.controller.TESTController;
import com.fasterxml.jackson.core.JsonProcessingException;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@RunWith(MockitoJUnitRunner.class)
public class TestVerificationNotification {
@InjectMocks
@Autowired
TESTController controller;
@MockBean
RestTemplate restTemplate;
// @MockBean
// TransactionRepository transactionRepository;
//
// @MockBean
// ResponseRepository responseRepository;
@Test
public void validationTestFailCase() {
InternalRequest req = new InternalRequest();
FinalResponse<InternalResponse> response = controller.corporateApplication(req);
assertEquals(ResponseInfo.FAILED, response.getStatus());
}
@Test
public void apiCallMockCase() throws JsonProcessingException, URISyntaxException {
InternalRequest req = new InternalRequest();
req.setCaseId("123");
req.setTxSN("string");
req.setCity("tes12");
req.setRemarks("testremarks");
req.setSource("source");
ResponseEntity<String> value = new ResponseEntity<>(
"{\r\n" + " \"Status\":\"20\",\r\n" + " \"ResponseCode\":\"2000\",\r\n" + " \"TxCode\":\"1305\",\r\n"
+ " \"Message\":\"SUCCESS\",\r\n" + " \"InstitutionID\":\"100241\",\r\n"
+ " \"TxSN\":\"201903041440510912286505035\",\r\n" + " \"ResponseMessage\":\"SUCCESS\",\r\n"
+ " \"Code\":\"2000\",\r\n" + " \"TraceNo\":\"1903041500395593995549946\"\r\n" + "}",
HttpStatus.OK);
Mockito.when(restTemplate.postForEntity(Mockito.anyString(), Mockito.any(), Mockito.eq(String.class)))
.thenReturn(value);
// Mockito.when(transactionRepository.save(Mockito.any())).thenReturn(null);
// Mockito.when(responseRepository.save(Mockito.any())).thenReturn(null);
FinalResponse<InternalResponse> response = controller.corporateApplication(req);
assertEquals(ResponseInfo.SUCCESS, response.getStatus());
}
}
内部配置
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<configuration>
<excludes>
<exclude>**/lib/*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<!-- <configuration> <propertyName>jaCoCoArgLine</propertyName> </configuration> -->
</execution>
<execution>
<id>default-report</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
<!-- <configuration> <argLine>${jaCoCoArgLine}</argLine> </configuration> -->
</execution>
</executions>
</plugin>
</plugins>
我希望测试能够成功,但是测试失败并显示错误
java.lang.ArrayIndexOutOfBoundsException: 14
at com.dbs.beans.request.InternalRequest.<init>(InternalRequest.java:13)
at com.dbs.service.impl.TESTServiceImpl.corporateApplication(TESTServiceImpl.java:99)
at com.dbs.controller.TESTController.corporateApplication(TESTController.java:115)
at com.dbs.unittesting.TestVerificationNotification.apiCallMockCase(TestVerificationNotification.java:72)
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.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:532)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:108)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:142)
at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:117)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)