1
I am trying to create a test class for my constructor, but it receives three parameters and these parameters are validated within the class itself, to allow or not the creation of its instance.
Follows code:
public class Mamiferos{
private String nome;
private Date dataNasc;
private String sexo;
public Mamiferos(String nome, Date dataNasc, String sexo){
try {
validaSexo();
validaNome();
validaData();
}catch (Exception e) {
// TODO: handle exception
}finally{
this.nome = nome;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
try {
this.dataNasc = format.parse(dataNasc.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.sexo =sexo;
}
}
private Boolean validaData() {
Date dataAtual = new Date(System.currentTimeMillis());
if (dataNasc.before(dataAtual)){
return true;
}else{
System.out.println("Por favor, digite uma data válida.\n");
return false;
}
}
private Boolean validaNome() {
if (nome.isEmpty() || nome.length()<5) {
System.out.println("O nome deve ter ao menos 5 caracteres.\nDigite novamente.");
return false;
}else{
return true;
}
}
private Boolean validaSexo() {
if (sexo != "Male" || sexo != "Female" || sexo.isEmpty()) {
System.out.println("O sexo não pode ser nulo.\nDigite o sexo (Male/Female).\n");
return false;
} else {
return true;
}
}
Down with my test class unsuccessful attempt:
public class NameTest {
@Test
public void test() {
}
Yes, empty, I don’t know how to test the validators. I didn’t want to make them public because it doesn’t make sense.
PS: I accept suggestions.
Thank you!
Hello, I don’t know this Hibernate Validator. Is it necessary to create a persistence to use it? Thanks.
– Tiaurls