Gonzo RI
Gonzo RI

Reputation: 71

Junit 2.0 : Different results for same test

I'm having different results for the same test in JUnit. The first test seems to be OK and to verify all the assertions, I believe that the class works fine. Then I rerun the test and I get an assertion failure. I realized that the problem was that I didn't clear the "data" after the test, and I tried to fix it doing exactly that, but I'm still having those assertion failures.

Any ideas?

Unit Test:

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The test class TestHistorial.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class TestHistorial extends junit.framework.TestCase {
    Historial hist;

    /**
     * Default constructor for test class TestHistorial
     */
    public TestHistorial() {
    }

    /**
     * Sets up the test fixture.
     * <p/>
     * Called before every test case method.
     */
    @Before
    public void setUp() {
        hist = new BaseDatos();
    }

    /**
     * Tears down the test fixture.
     * <p/>
     * Called after every test case method.
     */
    @After
    public void tearDown() {
        hist.eliminarTodosLosRegistros();
    }

    /**
     * Prueba
     */

    public void testHistorial() {

        for (int i = 0; i < 20; i++) {
            Registro reg = new RegistroTemperatura(273 + i, 1900 + i, i + 1, i + 1, i);
            try {
                hist.agregarRegistro(reg);
            } catch (Exception e) {
                assertEquals(false, true);
            } // No debería llegar aquí.
            String str = "Reg_" + i;
            Registro tReg = hist.getRegistroPorId(str);
            try {
                Temperatura t = new Temperatura(273 + i);
                assertEquals(true, t.equals(((RegistroTemperatura) tReg).getTemperatura()));
            } catch (Exception e1) {
                assertEquals(false, true);
            } // No debería llegar aquí.
            try {
                Fecha f = new Fecha(1900 + i, i + 1, i + 1, i);
                assertEquals(true, f.equals(tReg.getFecha()));
            } catch (Exception e2) {
                assertEquals(false, true);
            } // No debería llegar aquí.
        }
        assertEquals(20, hist.getNumeroDeRegistros());
        assertEquals(true, hist.getRegistroMasViejo().equals(new RegistroTemperatura(273, 1900, 1, 1, 0)));

    }
}

The output of the test after first testing is:

$> expected: false but was: true

The real question is, is the tearDown() method being executed after `testHistorial()``? If it isn't, how should I do it?

Upvotes: 0

Views: 2047

Answers (1)

Makoto
Makoto

Reputation: 106498

First, don't use any version of JUnit below 4.0. Second, don't use anything in junit.framework.* or junit.extensions.*.

With that out of the way...

To answer the first question, in JUnit 4.x, your @After-annotated method is always executed after the execution of your test case.

To get to the meat of the problem...your unit tests should be testing one thing. They're only a unit of functionality. Right now, you're testing at least five different pieces, which is why it's complicated to understand what's going on, or why it's breaking.

Take it piece by piece: Test Registro and feel confident that it's doing what it should be. Then move on to Historial. Then move on to Temperatura. Then Fecha. I can't tell you specifically why it's giving you inconsistent assertion failures, but I can tell you that it's very difficult to understand what you're actually testing, which may be the cause of the misunderstanding.

Also, as generic advice - if you are catching Exception, consider only catching it in a test if it's either a checked exception (enforced by Java), or if you need it to throw the exception and need to assert things about the state after the exception is thrown.

Upvotes: 2

Related Questions