I have a Java codebase that is developed exclusively in Eclipse. There
are a set of JUnit4 tests that can be divided into two mutually exclusive
subsets based on when they are expected to run:
1. "Standard" tests should run when a developer right-clicks the test
class (or containing project) and selects Run As > JUnit Test. Nothing
unusual hereâ€”this is exactly how JUnit works in Eclipse.
2. "Runtime" tests should only run when called programmatically from
within the application when it is started up in a specific state.
The two types of tests might sit adjacent to each other in the same Java
package. (Ideally we could intermix them in the same class, though
that's not a hard requirement.)
My first solution was to annotate the "Runtime" test classes with a new
`@TestOnLaunch` annotation. The application is able to find these classes,
and was running the tests contained in them (annotated with `@Test`) using
`JUnitCore.run(Class>...)`. However, these tests leak into the
"Standard" scenario above, because the Eclipse test runner will run any
method annotated with `@Test`, regardless of the intent of my custom class
Next I tried moving the `@TestOnLaunch` annotation to the method level.
This prevents the "leakage" of the "Runtime" tests into the "Standard"
scenario, but now I can't seem to get `JUnitCore` to run those test
methods. `run.(Request)` with a `Request` targeted at the correct class and
method, for example, fails with "No runnable methods", presumably
because it can't find the `@Test` annotation (because it's not there).
I'm very interested to know if there's a "JUnit way" of solving this
kind of problem. Presumably I could write my own `Runner` (to run methods
annotated with `@TestOnLaunch`)â€”is this the right approach? If so, how do
I then kick off the testing programmatically with a bunch of classes,
analogous to calling `JUnitCore.run(Class>...)`?
以上就是How can I run particular sets of JUnit tests under specific circumstances?的详细内容，更多请关注web前端其它相关文章！