0

So I was given some code and I had to write methods for it, print the size of list, first element and some other stuff, I did all that. for the next part of this project though, i have to write Junit tests for it but i only have getters, setters, and a tostring method so i dont know what im supposed to test. i know how to make junit tests and i have a class set up for it but i just dont know what im supposed to test. I have only ever written tests for methods with actual calculatioms so im not sure how im supposed to write a junit test for getters and setters. here is my class and my main. I cant really add more methods to the class because ive already finished everything i need to do for the specs of the program. here is my class and my main.

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;
    }

}

MAIN

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());
            }
        }
    }
}
  • 7
    Generally speaking there's very little value in writing unit tests for trivial methods such as getters and setters. However, if you must, you can set a value, read it back and assert that the values are the same. – dave Oct 08 '18 at 22:02
  • also wanted to add that if you write a unit test for your toString() method you will notice you forgot some spaces. – Charles Oct 08 '18 at 22:05
  • Check out the SO thread https://stackoverflow.com/questions/6197370/should-unit-tests-be-written-for-getter-and-setters – Rans Oct 08 '18 at 22:07
  • There are actually a couple of tests that could be done for the "trivial" setters. For example, there is no condition check on the `setMinSize(int)`. So, I could set a min size as negative number. Seems like that would be wrong. – KevinO Oct 08 '18 at 22:17

1 Answers1

0

There is no point in testing the getter setter however sometime, we may have customised getter and setter and in that case Unit Tests come handy to avoid errors.

I have used OpenPojo to test my Java DTOs.

Note: You need to provide your DTO package name and also name filter (optional). I am testing all the classes that ends with Dto under the given package.

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));

I have shown an example of few Testing Rules There are few more Rules available. Check Here

Amit Phaltankar
  • 3,341
  • 2
  • 20
  • 37