繁体   English   中英

在JUnit中初始化私有字段

[英]Initialize private fields in JUnit

如何在JUnit中初始化私有字段,以便在测试方法时,将使用私有字段的值。 如您所见, private String searchDate用于GetSkillListServiceCustomize ,但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);
   }

GetSkillListServiceCustomize.class

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

JUnit为init方法@Before提供标记。 在init方法中,您可以初始化几乎所有您想要的东西。 为了有用访问私有字段,我建议使用第三方工具

<!-- 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>

在你的测试中你可以像这样使用setter作为私有字段:

@Before
public void setUp() {
    org.springframework.test.util
       .ReflectionTestUtils.setField(
           theGetSkillListServiceCustomizeInstance,
           "searchSite",
           "valueToSet");
}

我建议为这个字段创建一个setter,并发表评论说这个setter仅用于单元测试。

我建议在类的构造函数中初始化私有字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM