jeudi 7 mai 2015

How to test a class only with public getters and private setters with JUnit?

Lets say we have following class:

 package com.porphyrie.examples;

public class TestExample {

    private String name;
    private int id;

    public TestExample() {
        setName("testExample");
        setId(3); 
    }


    public String getName() {
        return name;
    }
    private void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    private void setId(int id) {
        this.id = id;
    }

}

How should I test this class? Since eclipse wants only to test the public methods, how I should test the getters without setting the setters? Is it valid to set the private setter-methods in the class above to public only for testing purposes?

Thanks for any help...

Aucun commentaire:

Enregistrer un commentaire