1
As I test the System.out.println()
a method? The method I want to test displays the phrase:
Enter the name >
How do I test whether or not this phrase has been changed in the Test
?
main class:
public class Cliente extends PessoaFisica {
protected static Cliente pegarDados() {
String nome, estado, cidade;
int cpf, telefone, numeroConvenio;
System.out.println("Informe o nome");
nome = Clinica.entrada.nextLine();
System.out.println("Informe CPF");
cpf = Clinica.readInt();
System.out.println("Informe telefone");
telefone = Clinica.readInt();
System.out.println("Informe o estado");
estado = Clinica.entrada.nextLine();
System.out.println("Informe a cidade");
cidade = Clinica.entrada.nextLine();
System.out.println("Informe o número do do convênio");
numeroConvenio = Clinica.readInt();
Cliente cliente = new Cliente(nome, cpf, telefone, estado, cidade, numeroConvenio);
return cliente;
}
test class:
import java.io.ByteArrayInputStream;
import org.junit.Assert;
import org.junit.Test;
public class ClienteTest {
public void pegarDadosTest() {
String nome = "André Nascimento";
ByteArrayInputStream entradaTest = new ByteArrayInputStream(
nome.getBytes());
System.setIn(entradaTest);
String esperado = "Informe nome" + System.getProperty("line.separator");
Cliente.pegarDados();
String atual = entradaTest.toString();
Assert.assertEquals("metodo pegarDados falhou.", esperado, atual);
}
Are you trying to test whether the
Scanner
works? You should be testing your code, not the Java SDK code.– Pablo Almeida
@Pablo Almeida actually, I just want to test the message that the method writes and not the reading.
– André Nascimento
It seems to me that your method does two things? A more modular code is a more testable code. If you separate the responsibilities of these methods, you will see that you have nothing to test there. The function takes a string and prints. If there’s something wrong with that, it’s a Java problem.
– Pablo Almeida