Skip to content

Commit

Permalink
guarantee test execution order in AnnotationTest
Browse files Browse the repository at this point in the history
Fixes #67
  • Loading branch information
robfletcher committed Oct 7, 2012
1 parent 820261e commit 49a89d9
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/test/groovy/co/freeside/betamax/AnnotationTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.