我正在尝试使用maven测试mvn test
进行测试,但获得uses unchecked or unsafe operations
。我确实首先检查了不在运行测试问题的maven上的stackoverflow中找到的不同线程here,因为我最初得到There are no tests to run
并且在将版本添加到pom.xml
后我得到了unchecked or unsafe operattions
警告如此跟随this链接并检查List
,它在我的测试类中看起来不错,如
List<String> names = new ArrayList<String>();
但在使用maven-compiler-plugin 3.5.1
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] Building myaccount-service 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ myaccount-service ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\git-workspace\myaccount-service\src\main\resources
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ myaccount-service ---
[INFO] Compiling 1 source file to D:\git-workspace\myaccount-service\target\classes
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ myaccount-service ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\git-workspace\myaccount-service\src\test\resources
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ myaccount-service ---
[INFO] Compiling 2 source file to D:\git-workspace\myaccount-service\target\test-classes
[INFO] /D:/git-workspace/myaccount-service/src/test/java/DetailServiceTest.java: D:\git-workspace\myaccount-service\src\test\java\DetailServiceTest.java uses unchecked or unsafe operations.
[INFO] /D:/git-workspace/myaccount-service/src/test/java/DetailServiceTest.java: Recompile with -Xlint:unchecked for details.
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ myaccount-service ---
[INFO] Surefire report directory: D:\git-workspace\myaccount-service\target\surefire-reports
使用maven-compiler-plugin 2.3.2
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ myaccount-service ---
[INFO] Compiling 1 source file to D:\git-workspace\myaccount-service\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ esg-edge-service-myaccount ---
[INFO] Surefire report directory: D:\git-workspace\myaccount-service\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
我的maven编译器版本有问题吗?或者我在这里做错了什么。请帮帮我。
package com.mydomain.ws;
import com.mydomain.component.ProductComponent;
import com.mydomain.model.myaccount.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class DetailServiceTest {
@InjectMocks
private DetailService detailService;
@Mock
private ProductComponent productComponentMock;
@Mock
private ProductResponse response ;
@Mock
private List<Product> lstProduct ;
@Mock
private Product myProduct ;
@Mock
ProductDetailsRequest productDetailsRequest ;
ProductDetailRequestRoot productDetailRequestRoot = new ProductDetailRequestRoot();
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@After
public void tearDown() throws Exception {
}
@Test
public void getShouldReturnEmptyProductForNonExistingIDNName() {
List<String> productNames= new ArrayList<String>();
productNames.add("1554353");
productNames.add("4252322947");
String productId = "932937184";
when(productComponentMock.readAccount(anyString(), anyList())).thenReturn(lstProduct);
ProductDetailService.get("", "", productId, true, productNames);
verify(productComponentMock, times(1)).readAccount(anyString(), anyList());
assertEquals(0, lstProduct.size());
}
@Test
public void getShouldReturnProductForExistingproductIdNMsdnTest() {
String productId = "932937184";
List<String> productName = new ArrayList<String>();
productName.add("4252322947");
Product Productstub = new Product();
Productstub.setProductId("932937184");
Productstub.setProductName("4252322947");
List<Product> rProduct = new ArrayList<>(); //
rProduct.add(Productstub);
when(productComponentMock.readAccount(productId, productName)).thenReturn(rProduct);
ProductDetailService.get("", "", productId, true, productName);
verify(productComponentMock, times(1)).readAccount(productId, productName);
List<Product> result = productComponentMock.readAccount(productId, productName) ;
assertEquals(1, rProduct.size());
assertEquals(result, rProduct);
response.setProducts(rProduct);
verify(response, times(1)).setProducts(rProduct);
}
}
当我运行此Test类时,我在控制台中获得all 2 tests passed
。
现在我的测试正在运行
,从pom.xml
文件下面的代码段中删除了
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/Test.java</include>
</includes>
</configuration>
</plugin>
Results :
Tests run: 9, Failures: 0, Errors: 0, Skipped: 0