1

Some of my unit/integration tests depend on external network resources. I understand that it's bad design, but sometimes it happens. I don't want these tests to fail when the build is running on a computer without Internet access (in the airplane, for example). I want these tests to be skipped like this:

class FooTest {
  @BeforeAll
  public static void skipIfOffline() {
    Assumptions.assumeTrue(WeAreOnline.check());
  }
}

Are you aware of such WeAreOnline class somewhere in some Maven dependency, which I can reuse instead of implementing it over and over again in each project?

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • How about testing if a specific address is reachable? [Like this?](https://stackoverflow.com/questions/7685439/how-to-test-if-a-remote-system-is-reachable) "Online" is a rather vague term. – Sweeper Aug 08 '22 at 06:05
  • 1
    You can use [`@DisableIf`](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/condition/DisabledIf.html#value()). Or you could make your own [`ExecutionCondition`](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExecutionCondition.html) extension. In either case you can use [composed annotations](https://junit.org/junit5/docs/current/user-guide/#writing-tests-meta-annotations) (or even register the extension [globally](https://junit.org/junit5/docs/current/user-guide/#extensions-registration-automatic)). – Slaw Aug 08 '22 at 06:09
  • @Sweeper I know how to implement this :) I'm looking for a ready-to-go solution in form of a JAR that I can just add to my project. – yegor256 Aug 08 '22 at 06:23
  • I'm not sure what you mean. How would a JAR know what external address you want to reach? – Sweeper Aug 08 '22 at 06:31
  • @Sweeper I can use it like this: `WeAreOnline.check("google.com")` – yegor256 Aug 08 '22 at 06:37
  • So how does the link in my first comment not answer your question? – Sweeper Aug 08 '22 at 06:40
  • @Sweeper I will have to implement this `WeAreOnline` in _each_ project of mine. Instead, I want someone else to do this for me and share a Maven dependency. – yegor256 Aug 08 '22 at 06:47
  • 1
    So in other words: you are seeking library recommendations, which renders your request off topic here. – GhostCat Aug 08 '22 at 07:13
  • 1
    Could you implement and publish (possibly to a private repo) this library yourself? It seems like it'd be less than 100 lines of code (maybe even including tests, if any). – Slaw Aug 08 '22 at 07:13

0 Answers0