0

Let's consider a class like this

public class Element {

private String elementId;
private double[] elementFeatures;

public Element() { }

public Element(String elementId, double[] elementFeatures) throws BadElementInitializationException {

    checkElementId(elementId);
    checkElementFeatures(elementFeatures);

    this.elementId = elementId;
    this.elementFeatures = elementFeatures;
}

private void checkElementId(String elementId) throws BadElementInitializationException {

    if(elementId == null)
        throw new BadElementInitializationException("Element id must not be null");
    if(elementId.isEmpty())
        throw new BadElementInitializationException("Element id must not be empty");
}

private void checkElementFeatures(double[] elementFeatures) throws BadElementInitializationException {

    if(elementFeatures == null)
        throw new BadElementInitializationException("Element features must not be null");
    if(elementFeatures.length == 0)
        throw new BadElementInitializationException("Element features length must be greater than 0");
}

public String getElementId() {
    return elementId;
}

public void setElementId(String elementId) throws BadElementInitializationException {
    checkElementId(elementId);

    this.elementId = elementId;
}

public double[] getElementFeatures() { return elementFeatures; }

public void setElementFeatures(double[] elementFeatures) throws BadElementInitializationException {
    checkElementFeatures(elementFeatures);

    this.elementFeatures = elementFeatures;
}
}

Which unit tests would it make sense to have?

  • Should I test setters, constructors, and methods that check parameters? (it seems a bit redundant to do that)
  • Should I test only constructors and setters, without the private method?
  • Something else?

Additional: should I test combinations of bad parameters in constructor? That is, have a test for each combination of parameters in constructor (even though I'm testing setters for each of parameters)?

Kobe-Wan Kenobi
  • 3,694
  • 2
  • 40
  • 67

1 Answers1

0

you should test everything that scares you. are you afraid of the getter? do you think it may break? i wouldn't test it. there is a different story with setters - they contain logic

regarding private methods: testing them usually makes your code harder to change. the of the exception is when you use your private method in many places. then testing it may saves you a lot of time and code.

however in your case, the problem is not the private method but code duplication. constructor and setters share the same code. refactor so your constructor uses setters and then all you need is to test constructor - setters will be tested for free.

piotrek
  • 13,982
  • 13
  • 79
  • 165
  • Thank you for your answer, but what do you think about this? http://stackoverflow.com/questions/4893558/calling-setters-from-a-constructor There is an opinion that is not desirable to call setters from constructor. – Kobe-Wan Kenobi Nov 03 '15 at 16:57
  • if you want to avoid calling setters from construtor then refactor. add setter-like private/final methods. just deduplicate your code and you will have less lines to test – piotrek Nov 03 '15 at 21:11