Java test cases

Asked

Viewed 300 times

0

I am trying to perform a test on the banking system that I have done, I am using netbeans for this.

I need you to test the option to withdraw and deposit, in case what I have done so far only the error in everything, but I did not understand why I did not type anything to perform the test, does anyone know how to do? and if you can explain to me.

Class Caixa:

import java.util.Scanner;
import java.util.Random;

public class Caixa {


public static void main(String[] args){
    // Declarando as variáveis, Scanner e Random
    String nome;
    double inicial;
    Scanner entrada = new Scanner(System.in);
    Random numero = new Random();
    int conta = 1 + numero.nextInt(9999);

    //Obtendo os dados iniciais do Cliente
    System.out.println("Cadastrando novo cliente.");
    System.out.print("Ente com seu nome: ");
    nome = entrada.nextLine();

    System.out.print("Entre com o valor inicial depositado na conta: ");
    inicial = entrada.nextDouble();
    if(inicial <= 0){
       System.out.println("Não é possível iniciar com esse valor!\n");
    }
    else
    {

    //Criando a conta de um cliente
    Conta minhaConta = new Conta(nome, conta, inicial);
    minhaConta.iniciar();
}

}
}

Class Conta:

import java.util.Scanner;

public class Conta {
private String nome;
private int conta, saques;
private double saldo;
Scanner entrada = new Scanner(System.in);

public Conta(String nome, int conta, double saldo_inicial){
    this.nome=nome;
    this.conta=conta;
    saldo=saldo_inicial;
    saques=0;
}

public void extrato(){
    System.out.println("\tEXTRATO");
    System.out.println("Nome: " + this.nome);
    System.out.println("Número da conta: " + this.conta);
    System.out.printf("Saldo atual: %.2f\n",this.saldo);
    System.out.println("Saques realizados hoje: " + this.saques + "\n");

}

public void sacar(double valor){
if(valor <= 0){
  System.out.println("Não é possível sacar esse valor!\n");
}
else
{
  if(saldo >= valor){
      saldo -= valor;
      saques++;
      System.out.println("Sacado: " + valor);
      System.out.println("Novo saldo: " + saldo + "\n");
  } else {
      System.out.println("Saldo insuficiente. Faça um depósito\n");
  }
}
}

public void depositar(double valor)
{
if(valor <= 0){
  System.out.println("Não é possível depositar esse valor!\n");
}
else
{
  saldo += valor;
  System.out.println("Depositado: " + valor);
  System.out.println("Novo saldo: " + saldo + "\n");
}
}

public void iniciar(){
    int opcao;

    do{
        exibeMenu();
        opcao = entrada.nextInt();
        escolheOpcao(opcao);
    }while(opcao!=4);
}

public void exibeMenu(){

    System.out.println("\t Escolha a opção desejada");
    System.out.println("1 - Consultar Extrato");
    System.out.println("2 - Sacar");
    System.out.println("3 - Depositar");
    System.out.println("4 - Sair\n");
    System.out.print("Opção: ");

}

public void escolheOpcao(int opcao){
    double valor;

    switch( opcao ){
        case 1:    
                extrato();
                break;
        case 2: 
                if(saques<3){
                    System.out.print("Quanto deseja sacar: ");
                    valor = entrada.nextDouble();
                    sacar(valor);
                } else{
                    System.out.println("Limite de saques diários atingidos.\n");
                }
                break;

        case 3:
                System.out.print("Quanto deseja depositar: ");
                valor = entrada.nextDouble();
                depositar(valor);
                break;

        case 4: 
                System.out.println("Sistema encerrado.");
                break;

        default:
                System.out.println("Opção inválida");
    }
}
}

And here’s the Contactest Class that does the testing, here’s the problem or actually something I couldn’t do:

import org.junit.BeforeClass;
import org.junit.Test;


/**
 *
 * @author aluno
 */
public class ContaTest {

public ContaTest() {
}

@BeforeClass
public static void setUpClass() {
}

@Test
public void testSacar() {
    System.out.println("sacar");
    double valor = 0.0;
    Conta instance = null;
    instance.sacar(valor);
    //fail("The test case is a prototype.");
}


@Test
public void testDepositar() {
    System.out.println("depositar");
    double valor = 0.0;
    Conta instance = null;
    instance.depositar(valor);
    //fail("The test case is a prototype.");
}


}

inserir a descrição da imagem aqui

After I click to test appears this : inserir a descrição da imagem aqui

  • I think that Conta instance = null; must cause NullPointerException. If you post the error, it is easier to help. Try to create another constructor in the classes without input parameters, then you can do: Conta instance = new Conta();

  • @Hamurabiaraujo edited the question and put an image

1 answer

1


Conta instance = null;

instance.sacar(valor); /* ou instance.depositar(valor); */

You haven’t quoted what the test failure is so there’s no way to be 100% assertive, but it will surely result in NullPointerException and your tests will fail.

Do something like Conta instance = new Conta(nome, conta, saldoInicial); and then execute the withdrawal or deposit. Ex:

@Test
public void testSacar() {
    System.out.println("sacar");
    double valor = 0.0;
    Conta instance = new Conta("Joao", 1, 1000.0);
    instance.sacar(valor);
    //fail("The test case is a prototype.");
}
  • I put the pictures in the question of how do I do... I wanted you to perform the tests, like I type the name and initial balance, then make the options and tals

  • 1

    @Ferb in the image of to see the console saying it is NullPointerException. Make an instance of Conta with new Conta(nome, conta, saldoInicial) and will probably work (or at least will not give NullPointerException)

  • Where I’ll do it in my code ?

  • In testing methods. Instead of Conta instance = null.

  • @Ferb edited the question

  • Is appearing a red exclamation mark now, talking cannot find Symbol and variable names

  • @Ferb of course... I even put at the end of the question for you to change these values to correct values...

  • @Ferb edited the question... Try again

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.