【问题标题】:Test classes for getters and settersgetter 和 setter 的测试类
【发布时间】:2018-10-09 06:15:02
【问题描述】:

所以我得到了一些代码,我必须为它编写方法,打印列表的大小、第一个元素和其他一些东西,我做了所有这些。不过,对于这个项目的下一部分,我必须为它编写 Junit 测试,但我只有 getter、setter 和一个 tostring 方法,所以我不知道我应该测试什么。我知道如何进行junit测试,并且我为它设置了一个类,但我只是不知道我应该测试什么。我只为具有实际计算的方法编写过测试,所以我不确定我应该如何为 getter 和 setter 编写 junit 测试。这是我的课和我的主要课程。我真的不能在课程中添加更多方法,因为我已经完成了我需要为程序规范做的所有事情。这是我的课,也是我的主课。

public class FishData {
    private String species;  //species of fish
    private int minSize;  //min size allowed in inches
    private String season;  //fishing season
    private int limit;  //number
    private Boolean willEat;
    //constructor
    FishData(String s1, int m, String s2, int I, Boolean w){
        species = s1;
        minSize = m;
        season = s2;
        limit = I;
        willEat = w;
    }
    public String toString(){
        return species + "" + minSize + "" + season + "" + limit + "" + willEat;
    }
    public String getName(){
        return this.species;
    }
    public String getSpecies(){
        return this.species;
    }
    public String getSeason(){
        return this.season;
    }
    public Boolean getWillEat(){
        return this.willEat;
    }
    public void setSpecies(String species) {
        this.species = species;
    }
    public void setSeason(String season) {
        this.season = season;
    }
    public void setWillEat(Boolean willEat) {
        this.willEat = willEat;
    }
    public void setminSize(int minSize) {
        this.minSize = minSize;
    }
    public void setlimit(int limit) {
        this.limit = limit;
    }

}

主要

import java.util.Collections;
import java.util.LinkedList;
import java.util.Iterator;
public class GoneFishin {
    public static void main(String[] args) {
        //create linked list of fish data called fl
        LinkedList<FishData> fl = new LinkedList<FishData>();
        //Here are a few data items
        fl.add(new FishData("American Eel ", 9, " Summer/Spring ", 25, false));
        fl.add(new FishData("Hammerhead Shark ", 0, " All Year ", 36, false));
        fl.add(new FishData("Horseshoe Crab ", 7, " All Year except May ", 60, false));
        fl.add(new FishData("Haddock ", 18, " All Year ", 0, true));
        fl.add(new FishData("Tautog ", 16, " late Spring to end of year ", 3, true));

        for (FishData element : fl) {
            System.out.println(element.toString());
        }
        System.out.print("SIZE OF LISTt: ");
        System.out.println(fl.size());

        System.out.print("SECOND ELEMENT IN LIST: ");
        System.out.println(fl.get(1));

        System.out.print("LAST ELEMENT IN LIST: ");
        System.out.println(fl.getLast());

        Iterator<FishData> itr2 = fl.iterator();

        System.out.println("PRINTING ALL ELEMENTS USING ITERATOR");
        while (itr2.hasNext()) {
            System.out.println(itr2.next());
        }
        Iterator<FishData> itr = fl.iterator();
        while (itr.hasNext()) {
            FishData b = (FishData) itr.next();
            if (b.getWillEat()== false) {
                itr.remove();
            }
        }
        System.out.println("FISH THAT ARE GOOD TO EAT");
        for (FishData element : fl) {
            System.out.println(element);
        }
        System.out.println("CHANGE ALL DATA");
        Iterator<FishData> itr3 = fl.iterator();
        while (itr3.hasNext()) {
                FishData b = (FishData) itr3.next();
                b.setSpecies(" Shrekfish ");
                b.setSeason(" Whenever it wants ");
                b.setlimit(300000000);
                b.setminSize(900000000);
                b.setWillEat(false);


        for (FishData element : fl){
            System.out.println(element.toString());
            }
        }
    }
}

【问题讨论】:

  • 一般来说,为诸如getter和setter之类的琐碎方法编写单元测试的价值很小。但是,如果必须,您可以设置一个值,将其读回并断言这些值是相同的。
  • 还想补充一点,如果您为 toString() 方法编写单元测试,您会注意到您忘记了一些空格。
  • 实际上可以为“琐碎”的二传手做一些测试。例如,setMinSize(int) 没有条件检查。所以,我可以将最小尺寸设置为负数。好像这样就错了。

标签: java


【解决方案1】:

测试 getter setter 没有任何意义,但有时,我们可能已经自定义了 getter 和 setter,在这种情况下,单元测试可以方便地避免错误。

我使用OpenPojo 测试了我的 Java DTO。

注意:您需要提供您的 DTO 包名称以及名称过滤器(可选)。 我正在测试给定包下所有以Dto 结尾的类。

final Validator validator = ValidatorBuilder.create()
                .with(new GetterMustExistRule())
                .with(new SetterMustExistRule())
                .with(new GetterTester())
                .with(new SetterTester())
                .with(new ReflectionToStringTester())
                .build()

final List<PojoClass> pojos = PojoClassFactory.getPojoClassesRecursively("com.myProject.component.model", new FilterClassName("\\w*Dto*\$"))
Assert.assertTrue(validator.validate(pojos));

我已经展示了一些测试规则的示例 还有一些可用的规则。 Check Here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2011-06-26
    • 2015-01-20
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多