View Javadoc
1   /*
2    * Copyright 2011 Andres Gomez Casanova. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are
6    * permitted provided that the following conditions are met:
7    *
8    * 1. Redistributions of source code must retain the above copyright notice,
9    * this list of conditions and the following disclaimer.
10   *
11   * 2. Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * THIS SOFTWARE IS PROVIDED BY Andres Gomez Casanova ``AS IS'' AND ANY EXPRESS
16   * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18   * EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
19   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24   *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25   *
26   *  The views and conclusions contained in the software and documentation are
27   *  those of the authors and should not be interpreted as representing official
28   *  policies, either expressed or implied, of Andres Gomez Casanova.
29   */
30  package net.sf.randomjunit;
31  
32  import java.util.Collections;
33  import java.util.List;
34  
35  import org.junit.runners.BlockJUnit4ClassRunner;
36  import org.junit.runners.model.FrameworkMethod;
37  import org.junit.runners.model.InitializationError;
38  
39  /**
40   * This class randomize the execution of tests.
41   * <p>
42   * Taken from
43   * http://stackoverflow.com/questions/1444314/how-can-i-make-my-junit-
44   * tests-run-in-random-order
45   * <p>
46   * <b>Control Version</b>
47   * <p>
48   * <ul>
49   * <li>1.0.0 Class creation.</li>
50   * </ul>
51   *
52   * @author Michael Lloyd Lee (Mlk) - Stackoverflow
53   * @version 1.0.0 2011-06-13
54   */
55  public class RandomTestRunner extends BlockJUnit4ClassRunner {
56  
57      /**
58       * Please see
59       * {@link org.junit.runners.BlockJUnit4ClassRunner#BlockJUnit4ClassRunner(Class)}
60       * .
61       *
62       * @param klass
63       *            Class to execute.
64       * @throws InitializationError
65       *             If there is an error.
66       */
67      public RandomTestRunner(final Class<?> klass) throws InitializationError {
68          super(klass);
69      }
70  
71      /*
72       * (non-Javadoc)
73       *
74       * @see org.junit.runners.BlockJUnit4ClassRunner#computeTestMethods()
75       */
76      @Override
77      protected List<FrameworkMethod> computeTestMethods() {
78          final List<FrameworkMethod> methods = super.computeTestMethods();
79          Collections.shuffle(methods);
80          return methods;
81      }
82  
83  }