0

I was trying to develop some code using TDD, but stumbled on a weird behavior: setUp and tearDown doesn't seem to be "cleaning up" after each test is executed. I expected each test (marked with @Test annotation) to be executed at a random order, one after the other, without influencing each other. With that in mind, I don't understand what is happening as it seems like one specific test (testaSomarMao) is influencing another specific test (testaSplit). The testaSplit test is failling on the first assert: I expected value 6, but I'm getting 9. Anybody can explain me what is going on?

JogadorTest.java

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class JogadorTest{
    private static Jogador p1;
    public JogadorTest(){

    }

    @Before
    public void setUp(){
        p1 = new Jogador();
    }

    @After
    public void tearDown(){
        p1 = null;
    }

    @Test
    public void testaGetNome(){
        assertEquals(null, p1.getNome());
    }

    @Test
    public void testaGetPontos(){
        assertEquals(0, p1.getPontos());
    }

    @Test
    public void testaSetNome(){
        p1.setNome("Lucas");
        assertEquals("Lucas", p1.getNome());
    }

    @Test
    public void testaSomarMao(){
        p1.comprarCarta(1);
        assertEquals(3, p1.somarMao(1));
    }

    @Test
    public void testaSplit(){  
        p1.comprarCarta(1);
        p1.comprarCarta(1);        
        assertEquals(6, p1.somarMao(1));
        p1.split();
        assertEquals(p1.somarMao(1), p1.somarMao(2));
    }
}

Jogador.java

public class Jogador {

    private static String nome;

    private int pontos;

    private static int mao[] = new int[13];

    private static int mao2[] = new int[13];

    public void parar() {

    }

    public  void setNome(String novoNome){    
        nome = novoNome;    
    }

    public static int getPontos() {
        return 0;
    }

    public static void split() { 
        //Usamos mao2 para garantir que soh ocorra um split.
        if(mao[0] == mao[1] && mao2[0] == 0){            
            mao2[0] = mao[1];
            mao[1] = 0;
        }
    }

    public void fecharJogo() {

    }

    public String getNome() {
        return nome;
    }

    public static int somarMao(int maoEscolhida){
        int soma = 0;
        int cartasNaMao[];

        if(maoEscolhida == 2)
            cartasNaMao = mao2;        
        else
            cartasNaMao = mao;

        for(int i = 0; i < cartasNaMao.length; i++)
            soma += cartasNaMao[i];
        return soma;
    }

    public static void comprarCarta(int maoEscolhida){
        int carta = 3; // random futuramente
        int cartasNaMao[];

        if(maoEscolhida == 2)
            cartasNaMao = mao2;        
        else
            cartasNaMao = mao;

        for(int i = 0; i < cartasNaMao.length; i++){
            if(cartasNaMao[i] == 0) {
                cartasNaMao[i] = carta;
                break;
            }
        }
    }

}
Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90

2 Answers2

1

In comprarCarta() you are modifying mao or mao2 which are static arrays. Any changes you make to these arrays will persist across all instances of Jogador.

Since this is not what you expect I suspect that you may not want these to be static.

Mark Sholund
  • 1,273
  • 2
  • 18
  • 32
1

The underlying reason for your problem is, as pointed out by Thevenin, the use of static variables for mao[] and mao2[]

You will also need to remove static from methods comprarCarta and somarMao. You will not need it after all variables referenced inside are not static anymore. From the test it is obvious you are not using them in the way static methods are invoked usually, i.e Jogador.comprarCarta(1) instead of p1.comprarCarta(1).

My guess is you created those methods static and compiler then complained that your variables are not static and cannot be accessed, so you changed the variables as well.

Actually the use of static will cause you issues with other parts of your program as well - you better change nome variable to not be static. Same applies for getPontos() method - if you try to return pontos there instead of 0 you will get a compiler error that pontos is not static.

As you are confusing the use static in general take a look at this great explanation of what is static, when you have the time :)
Static variables - what are they?

Kotse
  • 418
  • 2
  • 8