This is my problem: I've been provided an application and I have to write test casesin order to test it using JUnit. For example: Instantiate an object with a property String name
and this field cannot be longer tan 10 characters. How do I catch it into the test method? Here is my code:
The class to be tested is:
package hdss.io;
import hdss.exceptions.HydricDSSException;
public class AquiferPublicData implements WaterResourceTypePublicData {
private String myName;
private float currentHeight;
public AquiferPublicData (String name, float current) throws HydricDSSException{
try {
if(name.length()>10) //name must be shorter than 11 chars
throw new HydricDSSException("Name longer than 10");
else{
myName = name;
currentHeight = current;
}
} catch (HydricDSSException e) {
}
}
public String getMyName() {
return myName;
}
}
My test method is:
package hdss.tests;
import static org.junit.Assert.*;
import org.junit.Test;
import hdss.exceptions.HydricDSSException;
import hdss.io.AquiferPublicData;
public class AquiferPublicDataTest {
@Test
public void testAquiferPublicData() {
String notValidName = "this-name-is-too-long";
try {
AquiferPublicData apd = new AquiferPublicData(notValidName, 10);
fail("Was supposed to throw Exception if name is longer than 10 chars");
} catch (HydricDSSException e) {
assertEquals("Name longer than 10", e.getMessage());
}
}
}
And the Exception is:
package hdss.exceptions;
public class HydricDSSException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
String message;
//Esfuerzo Actual: 1.5 minutos
public HydricDSSException (String message){
this.message = message;
}
//Esfuerzo Actual: 1.5 minutos
public String getMessage(){
return this.message;
}
}