吸气剂和吸气剂的测试班

时间:2018-10-08 21:59:15

标签: java

所以给了我一些代码,我不得不为其编写方法,打印列表的大小,第一个元素和其他一些东西,我做了所有这些。虽然对于该项目的下一部分,我必须为其编写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());
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

测试getter设置器没有意义,但是有时,我们可能已经定制了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