I'm trying to test a void method using JUnit in NetBeans, my method is changing the value of a double variable and then print the new variable's valur into a TextField, this is my method :
private void calcul(){
if(operateur.equals("+")){
chiffre1 = chiffre1 + Double.valueOf(ecran.getText()).doubleValue();
ecran.setText(String.valueOf(chiffre1));
}
if(operateur.equals("-")){
chiffre1 = chiffre1 - Double.valueOf(ecran.getText()).doubleValue();
ecran.setText(String.valueOf(chiffre1));
}
if(operateur.equals("*")){
chiffre1 = chiffre1 * Double.valueOf(ecran.getText()).doubleValue();
ecran.setText(String.valueOf(chiffre1));
}
if(operateur.equals("/")){
try{
chiffre1 = chiffre1 / Double.valueOf(ecran.getText()).doubleValue();
ecran.setText(String.valueOf(chiffre1));
} catch(DivisionSurZeroException e) {
ecran.setText("0");
}
}
}
I Googled about how to test void method but all I can find is void methods that changes the size of a collection.
So please can you give me some idea to start with ? I really don't know what to start with.
You have to create new entity of tested class and after that call tested function. Later on assert that values have changed as you wanted to.
TestedClass tested = new TestedClass();
tested.calcul();
assertEquals("val", tested.getVal());
However, in your example method is private. To test it you may try to use PowerMock .
you could split your method into several smaller methods - one for each operator - and test them independently.
testing void
methods is not too different from testing methods with return values:
in your case:
yourClass.setOperateur("+");
yourClass.setChiffre1(5);
yourClass.setEcran("7");
yourClass.calcul();
assertEquals("12", yourClass.getEcran());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.