2012-01-10 12 views

Respuesta

4

Hay un decorador de prueba para esto. Ver API Junit en http://junit.org/apidocs/junit/extensions/RepeatedTest.html

por ejemplo

@Test 
@Repeat(10) 
public void FailRandomlyNeedToKnowWhy() { 
    .... 
} 
+1

El decorador 'RepeatedTest' viene de JUnit mientras que el ejemplo de su código contiene una anotación de Spring, ¿no es así? – javanna

+1

Funciona sólo en primavera –

8

¿Has probado algo como esto?

@Test 
public void runMultipleTests() { 
    for (int i = 0; i < 10; i++) { 
     myTestMethod(); 
    } 
} 
+1

Ahhh. Hay un pensamiento. Podría intentarlo si me desespero. – Thom

13

acabo de encontrar la siguiente solución que no requiere ningún depedency adicional (se requiere la primavera para una de las respuestas que dieron).

Ejecutar la prueba con el Parameterized corredor:

@RunWith(Parameterized.class) 

continuación, agregue el siguiente método para proporcionar una serie de parámetros vacíos es igual al número de veces que desea ejecutar la prueba:

@Parameterized.Parameters 
public static List<Object[]> data() { 
    return Arrays.asList(new Object[10][0]); 
} 

De esta manera ni siquiera tiene que escribir un bucle. IntelliJ y eclipse también agrupan los resultados de cada iteración juntos.

1

inspirada en this solution:

Uso @Repita anotación de la siguiente manera:

public class MyTestClass { 

    @Rule 
    public RepeatRule repeatRule = new RepeatRule(); 

    @Test 
    @Repeat(10) 
    public void testMyCode() { 
     //your test code goes here 
    } 
} 

Sólo necesitarás estas dos clases:

Repeat.java:

import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 
import static java.lang.annotation.ElementType.METHOD; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME) 
@Target({ METHOD, ANNOTATION_TYPE }) 
public @interface Repeat { 
    int value() default 1; 
} 

RepeatRule.java:

import org.junit.rules.TestRule; 
import org.junit.runner.Description; 
import org.junit.runners.model.Statement; 

public class RepeatRule implements TestRule { 

    private static class RepeatStatement extends Statement { 
     private final Statement statement; 
     private final int repeat;  

     public RepeatStatement(Statement statement, int repeat) { 
      this.statement = statement; 
      this.repeat = repeat; 
     } 

     @Override 
     public void evaluate() throws Throwable { 
      for (int i = 0; i < repeat; i++) { 
       statement.evaluate(); 
      } 
     } 

    } 

    @Override 
    public Statement apply(Statement statement, Description description) { 
     Statement result = statement; 
     Repeat repeat = description.getAnnotation(Repeat.class); 
     if (repeat != null) { 
      int times = repeat.value(); 
      result = new RepeatStatement(statement, times); 
     } 
     return result; 
    } 
} 

2016-10-25 Editar: Para utilizar esta solución cuando se utiliza @RunWith(PowerMockRunner.class), actualización de Powermock 1.6.5 (que incluye this patch).

Cuestiones relacionadas