Java – exclude a single JUnit test method without modifying the test class?
I am currently using JUnit 4 tests in another project to oppose my code As part of my automated ant build, I get them directly from the repository of other projects This is great because it ensures that my code stays green in the latest version of the test
However, there are some tests that I never wanted to pass to my code But if I start adding @ ignore annotations to those tests, I will have to maintain a separate copy of my own test implementation, which I really don't want to do
Is there a way to exclude a single test without modifying the test source? This is what I have seen so far:
>As far as I know, the ant JUnit task only allows you to exclude the entire test class, not a separate test method - this is not good for me. I need method granularity. > I consider integrating a TestSuite that uses reflection to dynamically find and add all original tests, and then add code to explicitly delete tests I don't want to run But when I noticed that the TestSuite API did not provide a way to delete tests, I gave up the idea. > I can create my own test class to extend the original test class, overwrite specific tests I don't want to run, and annotate them with @ ignore Then I run JUnit on my subclass The disadvantage here is that if I add new test classes to the original project, I will not automatically select them I will have to monitor the new test classes because they are added to the original project So far, this is my best choice, but it doesn't feel ideal. > The only other option I can think of is to run the wrong test anyway and ignore the failure However, these tests take some time to run (and fail!) So I'd rather not run them at all In addition, I don't see a way to tell ant tasks to ignore the failure of specific test methods (again – I'll see how you can test a single class instead of a method)
Solution
If you have no access to the original test at all, you will face some serious limitations Your most important voice is the best choice, but there are some changes:
Build ant tests that specifically exclude superclasses to run other classes you don't know
You can use the @ rule annotation (new in JUnit 4.7) to understand the running test and abort it (by returning an empty statement Implementation) instead of overriding a specific method, which makes you more flexible in knowing whether to avoid testing The only problem with this method is that you cannot stop the @ before method using this method, which may be slow If this is a problem (you really can't touch the test), then @ ignore in the rewrite method is the only thing I can think of
However, if you can touch these tests, some other options open:
You can run them using a custom runner by specifying the @ runwith tag on the class This runner will only pass the execution to the standard running program (junit4. Class) in the project, but in your project (through system properties or some other mechanism), it will check the test name instead of running the test The advantage of this is that it is the least invasive, but the most difficult to achieve (runners are furry beasts, and one of the established goals of @ rule is to eliminate most of the need to make them)
Another method is to make a hypothetical statement in the test, which will check some configuration settings. If the test should run, the setting is true This actually involves injection testing, which is likely to be any transaction breaker remotely marked as a "separate project"