Java 在JUnit中初始化私有字段
如何在JUnit中初始化私有字段,以便在测试方法时使用私有字段的值。如您所见,在Java 在JUnit中初始化私有字段,java,junit,junit4,Java,Junit,Junit4,如何在JUnit中初始化私有字段,以便在测试方法时使用私有字段的值。如您所见,在GetSkillListServiceCustomize中使用了private String searchDate,但是searchDate尚未初始化,因此测试失败。我尝试使用反射,但它抛出一个NoSuchFieldException:GetSkillListServiceCustomizeTest.class是我的JUnit类,而另一个是我正在测试的类。 GetSkillListServiceCustomizeT
GetSkillListServiceCustomize
中使用了private String searchDate
,但是searchDate
尚未初始化,因此测试失败。我尝试使用反射,但它抛出一个NoSuchFieldException:
GetSkillListServiceCustomizeTest.class
是我的JUnit类,而另一个是我正在测试的类。
GetSkillListServiceCustomizeTest.class
try {
Field reader = GetSkillListServiceCustomize .class.getDeclaredField("searchDate ");
reader.setAccessible(true);
StringReader stringReader = new StringReader("2017-01-28");
BufferedReader readerToSet = new BufferedReader(stringReader);
reader.set(testClass, readerToSet);
returnVal = testClass.processOutput(mapVar);
} catch (NoSuchFieldException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
}
assertTrue(returnVal != null);
}
public class GetSkillListServiceCustomize
extends GetSkillListService
implements ServiceIF<GetSkillListInputDTO, GetSkillSummaryDisplayDTO> {
private String searchSite;
private String searchDate;
...more codes
protected GetSkillListOutputDTO processOutput(Map<String, Object> resultMap)
throws ServiceDBException, ServiceAppException {
...//more codes
List<GetSkillListOutputDTOList> getskilllistoutputdtolistTmpWrap = null;
if (employeeMasterList != null && !employeeMasterList.isEmpty()) {
getskilllistoutputdtolistTmpWrap = new ArrayList<>();
}
if (employeeMasterList != null && !employeeMasterList.isEmpty()) {
List<EMPLOYEE_MASTER> empMasterlistNoDeleted = employeeMasterList.stream()
.filter(e -> {
try {
return (e.getDel_Datetime() == null
|| (sdf.parse(e.getDel_Datetime())
.compareTo(sdf.parse(this.searchDate)) > 0));
} catch (ParseException ex) {
ex.printStackTrace();
return false;
}
})
.collect(Collectors.toList());
...//more codes in the bottom
GetSkillListServiceCustomize.class
try {
Field reader = GetSkillListServiceCustomize .class.getDeclaredField("searchDate ");
reader.setAccessible(true);
StringReader stringReader = new StringReader("2017-01-28");
BufferedReader readerToSet = new BufferedReader(stringReader);
reader.set(testClass, readerToSet);
returnVal = testClass.processOutput(mapVar);
} catch (NoSuchFieldException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
}
assertTrue(returnVal != null);
}
public class GetSkillListServiceCustomize
extends GetSkillListService
implements ServiceIF<GetSkillListInputDTO, GetSkillSummaryDisplayDTO> {
private String searchSite;
private String searchDate;
...more codes
protected GetSkillListOutputDTO processOutput(Map<String, Object> resultMap)
throws ServiceDBException, ServiceAppException {
...//more codes
List<GetSkillListOutputDTOList> getskilllistoutputdtolistTmpWrap = null;
if (employeeMasterList != null && !employeeMasterList.isEmpty()) {
getskilllistoutputdtolistTmpWrap = new ArrayList<>();
}
if (employeeMasterList != null && !employeeMasterList.isEmpty()) {
List<EMPLOYEE_MASTER> empMasterlistNoDeleted = employeeMasterList.stream()
.filter(e -> {
try {
return (e.getDel_Datetime() == null
|| (sdf.parse(e.getDel_Datetime())
.compareTo(sdf.parse(this.searchDate)) > 0));
} catch (ParseException ex) {
ex.printStackTrace();
return false;
}
})
.collect(Collectors.toList());
...//more codes in the bottom
公共类GetSkillListServiceCustomize
扩展GetSkillListService
实现ServiceIF{
私有字符串搜索站点;
私有字符串搜索日期;
…更多代码
受保护的GetSkillListOutput到processOutput(映射结果映射)
抛出ServiceDBException、ServiceAppException{
…//更多代码
List getskilllistoutputdtolistTmpWrap=null;
if(employeeMasterList!=null&&!employeeMasterList.isEmpty()){
getskilllistoutputdtolistTmpWrap=new ArrayList();
}
if(employeeMasterList!=null&&!employeeMasterList.isEmpty()){
List empMasterlistNoDeleted=employeeMasterList.stream()
.filter(e->{
试一试{
返回值(如getDel_Datetime()==null
||(sdf.parse(例如getDel_Datetime())
.compareTo(sdf.parse(this.searchDate))>0);
}捕获(解析异常){
例如printStackTrace();
返回false;
}
})
.collect(Collectors.toList());
…//底部有更多代码
我建议在类的构造函数中初始化私有字段。我建议为该字段创建一个setter,并指出该setter仅用于单元测试。JUnit在之前为init method@提供了标记。在init method中,您可以初始化几乎所有需要的内容。为了获得有用的访问权限例如,对于私有字段,我建议使用第三方UTIL
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.10.RELEASE</version>
<scope>test</scope>
</dependency>
尝试从字段名中去掉尾随空格。为什么不在构造函数中初始化它?为什么需要测试甚至没有初始化的变量?因为searchDate字段的初始化是在另一个方法中完成的,它将在上面编写的方法中传递。这就是为什么,我正在寻找一种设置伪值的方法到执行“.compareTo”时将使用的searchDate字段。主要问题是要测试的对象构造不正确。在测试对象之前,应该对其进行完全初始化。使用反射初始化对象的字段不是好的做法,除非您尝试测试的代码是遗留代码…并且将setter设置为“package private”(这是默认的可见性),因为您的单元测试无论如何都应该在同一个包中。