views:

405

answers:

2

I use Google Appengine for Java (GAE/J). On top, I use the Jersey REST-framework.

Now i want to run local JUnit tests. The test

Unfortunately, the Jersey/Jetty combo spawns new threads. GAE expects only one thread to run. In the end, I end up having either no datstore inside the Jersey-resources or multiple, having different datastore.

As a workaround I initialise the GAE local env only once, put it in a static variable and inside the GAE resource I add many checks (This threads has no dev env? Re-use the static one). And these checks should of course only run inside JUnit tests.. (which I asked before: "How can I find out if code is running inside a JUnit test or not?" - I'm not allowed to post the link directly here :-|)

+1  A: 

Maybe use a System property... When it's a Junit run set a Java system property via a JVM arg which you can then test for something like this:

  • pass in a JVM arg via run config parms when testing (in IDE etc maybe) like this: -DRunningTestsOnly=true
  • in code: if ("true".equals(System.getProperty("RunningTestsOnly")))

I've done similar to this in JUnit tests before, using assume to ignore tests I think take too long to run on my underpowered dev PC...

  • pass in JVM arg: -DexecuteQuickRunningTestsOnly=true
  • To ignore a slow running test method if property set I put at the top of the method : assumeThat(System.getProperty("executeQuickRunningTestsOnly", "false"), is("false"));
Chris B
+1  A: 

Just an idea: You could create an Exception and then iterate over the exception's stack trace to see if thers's a class from the junit package in there.

sme