简体   繁体   English

JUnit:setUp和tearDown方法无法按预期工作

[英]JUnit: setUp and tearDown methods not working as expected

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. 我试图使用TDD开发一些代码,但偶然发现了一个奇怪的行为:在执行每个测试之后,setUp和tearDown似乎并没有“清理”。 I expected each test (marked with @Test annotation) to be executed at a random order, one after the other, without influencing each other. 我希望每个测试(标有@Test注释的测试)将以一个随机顺序执行,一个接一个地执行,而不会互相影响。 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). 考虑到这一点,我不了解发生了什么,因为似乎某个特定测试(testaSomarMao)正在影响另一特定测试(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? testaSplit测试在第一个断言上失败了:我期望值6,但我得到9。有人可以解释我发生了什么吗?

JogadorTest.java 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 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;
            }
        }
    }

}

In comprarCarta() you are modifying mao or mao2 which are static arrays. comprarCarta()您要修改maomao2 ,它们是静态数组。 Any changes you make to these arrays will persist across all instances of Jogador. 您对这些数组所做的任何更改将在Jogador的所有实例中保留。

Since this is not what you expect I suspect that you may not want these to be static. 因为这不是您期望的,所以我怀疑您可能不希望它们是静态的。

The underlying reason for your problem is, as pointed out by Thevenin, the use of static variables for mao[] and mao2[] 如戴维南所指出的,导致问题的根本原因是对mao[]mao2[]使用静态变量

You will also need to remove static from methods comprarCarta and somarMao . 您还需要从方法comprarCartasomarMao删除静态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, ie Jogador.comprarCarta(1) instead of p1.comprarCarta(1) . 从测试中可以明显Jogador.comprarCarta(1) ,您没有以通常调用静态方法的方式使用它们,即Jogador.comprarCarta(1)而不是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. 实际上,使用静态也会导致程序其他部分出现问题-最好将nome变量更改为非静态。 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. 同样适用于getPontos()方法-如果您尝试在那里返回ponto而不是0,则会出现编译器错误,即pontos不是静态的。

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? 静态变量-它们是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM