1

I was thinking it would be handy to have a custom annotation for junit methods and classes that contain integration tests. This annotation would effectively act like the @Ignore junit annotation when the tests are being executed on our CI (Jenkins) server and the test would run as normal on local dev machines. To determine if the machine running the tests should allow integration test execution I would check System.getenv("WORKSPACE") to see if it exists/contains "jenkins".

I haven't written annotations before and am not familiar with junit's implementation details. Is what I'm describing possible? If so, how would I go about this?

WishIWasJeeping
  • 173
  • 2
  • 11

2 Answers2

3

JUnit 5

JUnit 5 has Environment Variable Conditions for this purpose. In your case, try add the following:


@Test
@EnabledIfEnvironmentVariable(named = "WORKSPACE", matches = "jenkins")
void onlyOnJenkins() {
    // ...
}

@Test
@DisabledIfEnvironmentVariable(named = "WORKSPACE", matches = "jenkins")
void notOnJenkins() {
    // ...
}

Note that the matches annotation attribute is a regex that supports wildcards. For details, please read the JavaDoc of @EnabledIfEnvironmentVariable and @DisabledIfEnvironmentVariable

The Conditional Test Execution chapter in the JUnit user guide has more annotations and examples that you may find useful.


JUnit 4

Please read this answer to learn how org.junit.Assume can be used to conditionally execute tests.

matsev
  • 32,104
  • 16
  • 121
  • 156
0

Great information, thank you! I'm going to have to add a tech debt item in our backlog for upgrading to Junit 5.

However, Assume did exactly what I needed. I was able to add the following to my test class:

    @Before
    public void checkHost(){
        final String workspaceKey = "WORKSPACE";
        final String workspaceValue = System.getenv(workspaceKey);
        final boolean isJenkins = StringUtils.contains(workspaceValue, "Jenkins");
        log.info("checkHost(): " + workspaceKey + ":" + workspaceValue + " -> isJenkins: " + isJenkins);
        assumeFalse(isJenkins);
    }
WishIWasJeeping
  • 173
  • 2
  • 11