11

When writing code that interacts with external resources (such as using a web service or other network operation), I often structure the classes so that it can also be "stubbed" using a file or some other input method. So then I end up using the stubbed implementation to test other parts of the system and then one or two tests that specifically test calling the web service.

The problem is I don't want to be calling these external services either from Jenkins or when I run all of the tests for my project (e.g. "gradle test"). Some of the services have side effects, or may not be accessible to all developers.

Right now I just uncomment and then re-comment the @Test annotation on these particular test methods to enable and disable them. Enable it, run it manually to check it, then remember to comment it out again.

// Uncomment to test external service manually //@Test public void testSomethingExternal() { 

Is there is a better way of doing this?

EDIT: For manual unit testing, I use Eclipse and am able to just right-click on the test method and do Run As -> JUnit test. But that doesn't work without the (uncommented) annotation.

3
  • It's not clear whether you're wanting an automated "profile" of unit tests that you can switch back and forth between or just a cleaner way of manually toggling them during development. Commented Sep 6, 2013 at 8:21
  • @chrylis What I was trying to do originally was to run an individual test. However, the idea of making a separate profile for these types of tests is probably a better approach. Although I still would want the ability to run some tests separately (in cases where there are side effects, for example - which I try to avoid but is sometimes inevitable). Commented Sep 7, 2013 at 22:51
  • As you've probably discovered by now, it may be best to start using JUnit's Assume. Invent a build property (or env var) that is checked; only run the test when your condition is satisfied. (E.g. this other SO answer.) Commented Jul 2, 2024 at 14:31

4 Answers 4

7

I recommend using junit categories. See this blog for details : https://community.oracle.com/blogs/johnsmart/2010/04/25/grouping-tests-using-junit-categories-0.

Basically, you can annotate some tests as being in a special category and then you can set up a two test suites : one that runs the tests of that category and one that ignores tests in that category (but runs everything else)

@Category(IntegrationTests.class) public class AccountIntegrationTest { @Test public void thisTestWillTakeSomeTime() { ... } @Test public void thisTestWillTakeEvenLonger() { .... } 

}

you can even annotate individual tests"

public class AccountTest { @Test @Category(IntegrationTests.class) public void thisTestWillTakeSomeTime() { ... } 

Anytime I see something manually getting turned on or off I cringe.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - this sounds like a good idea. Not sure how this interacts with Gradle or Eclipse, will have to check that out further.
4

As far as I can see you use gradle and API for JUnit says that annotation @Ignore disables test. I will add gradle task which will add @Ignore for those tests.

Comments

3

If you're just wanting to disable tests for functionality that hasn't been written yet or otherwise manually disable some tests temporarily, you can use @Ignore; the tests will be skipped but still noted in the report.

If you are wanting something like Spring Profiles, where you can define rulesets for which tests get run when, you should either split up your tests into separate test cases or use a Filter.

1 Comment

Interesting. See my edit above - maybe I could make a Filter that detects when I'm running a single test in Eclipse and (un)filters that test method appropriately. (Hadn't thought of this as being like Spring profiles, but yeah it's quite similar.)
3

You can use @Ignore annotation to prevent them from running automatically during test. If required, you may trigger such Ignored tests manually.

 @Test public void wantedTest() { return checkMyFunction(10); } @Ignore @Test public void unwantedTest() { return checkMyFunction(11); } 

In the above example, unwantedTest will be excluded.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.