JUnit 4: Anotaciones JUnit en un vistazo

Aunque JUnit 4 ya tiene unos añitos siempre se me olvidan las anotaciones que soporta.

La forma más fácil de recordarlas es ver un ejemplo con las principales…ahí va:

Y el código:

import java.util.logging.Logger; 

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Ignore;

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

public class TestAnotacionesJUnit {

private static Logger logger = null;

private SimpleMath simpleMath = null;

@BeforeClass

public static void ejecutaAntesTests() {

logger = Logger.getLogger(«TestAnotacionesJUnit»);

logger.info(«Se van a ejecutar los Tests…»);

}

@AfterClass

public static void ejecutaFinTests() {

logger.info(«Se han ejecutado los Tests…»);

logger = null;

}

@Before

public void ejecutaAntesCadaTest() {

simpleMath = new SimpleMath();

}

@After

public void ejecutaFinCadaTest() {

simpleMath = null;

}

@Test

public void sumar() {

assertEquals(12, simpleMath.add(7, 5));

}

@Test

public void restar() {

assertEquals(9, simpleMath.substract(12, 3));

}

@Test(expected = ArithmeticException.class)

public void dividirPorCero() {

simpleMath.divide(1, 0);

}

@Ignore(«Aún no lo he implementado»)

@Test

public void multiplication() {

assertEquals(15, simpleMath.multiply(3, 5));

}

}

Podéis descargar el JAR de la última versión estable de JUnit (la 4.8.2) de aquí: https://github.com/downloads/KentBeck/junit/junit-4.8.2.jar

El Getting Started aquí.

Deja un comentario