Permalink
Browse files

guarantee test execution order in `AnnotationTest`

Fixes #67
  • Loading branch information...
1 parent 820261e commit 49a89d9adf624ee8a3e65997ae1ca78dc7c882bf Rob Fletcher committed Oct 7, 2012
Showing with 34 additions and 1 deletion.
  1. +34 −1 src/test/groovy/co/freeside/betamax/AnnotationTest.groovy
@@ -5,13 +5,18 @@ import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.RESTClient
import org.junit.*
+import org.junit.runner.RunWith
+import org.junit.runners.BlockJUnit4ClassRunner
+import org.junit.runners.model.FrameworkMethod
import static co.freeside.betamax.proxy.jetty.BetamaxProxy.X_BETAMAX
+import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK
import static org.apache.http.HttpHeaders.VIA
+@RunWith(OrderedRunner)
class AnnotationTest {
- static File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes')
+ static File tapeRoot = newTempDir('tapes')
@Rule public Recorder recorder = new Recorder(tapeRoot: tapeRoot)
SimpleServer endpoint = new SimpleServer()
RESTClient http
@@ -80,3 +85,31 @@ class AnnotationTest {
}
}
+
+/**
+ * This is an evil hack. JUnit does not guarantee test execution order and the methods in this test depend on each
+ * other. In particular `annotatedTestCanPlayBack` will fail if run before `annotatedTestCanRecord`. Really the tests
+ * should be idempotent.
+ */
+class OrderedRunner extends BlockJUnit4ClassRunner {
+
+ private static final ORDER = [
+ 'noTapeIsInsertedIfThereIsNoAnnotationOnTheTest',
+ 'annotationOnTestCausesTapeToBeInserted',
+ 'tapeIsEjectedAfterAnnotatedTestCompletes',
+ 'annotatedTestCanRecord',
+ 'annotatedTestCanPlayBack',
+ 'canMakeUnproxiedRequestAfterUsingAnnotation'
+ ]
+
+ OrderedRunner(Class testClass) {
+ super(testClass)
+ }
+
+ @Override
+ protected List<FrameworkMethod> computeTestMethods() {
+ super.computeTestMethods().sort {FrameworkMethod o1, FrameworkMethod o2 ->
+ ORDER.indexOf(o1.name) <=> ORDER.indexOf(o2.name)
+ }
+ }
+}

0 comments on commit 49a89d9

Please sign in to comment.