7

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 :-|)

Dr. Max Völkel
  • 1,780
  • 2
  • 17
  • 24

2 Answers2

7

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
  • 13
  • 4
  • Also consider using environment variables, accessed in code via for example `String runLevel = System.getenv("RUN_LEVEL");`. That *language independent* mechanism is popular for allowing your program to have different configuration parameters in different environments (dev, test, integration, CI, production, etc) – gb96 Aug 23 '17 at 05:39
  • Also, if you're using Gradle, it auto-sets the property `org.gradle.test.worker` when testing, [see the documentation](https://docs.gradle.org/current/userguide/java_testing.html#sec:test_execution) – CLOVIS Jul 09 '18 at 20:13
  • In case others run into it, I've not dug deeply, but the `org.gradle.test.worker` property seemed to be present when running directly from gradle, but missing when running in IDEA. – Joshua Goldberg Sep 30 '21 at 18:52
1

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
  • 5,673
  • 7
  • 32
  • 30