Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force run of @Ignore'd JUnit test using Maven command line

Tags:

junit

maven

I have a JUnit test with an @Ignore attribute, for example -

public class HealthCareGov
{
    @Test
    @Ignore
    public void performancetest()
    {
        // stuff
    }
}

In most IDEs (IntelliJ for example) I can forcefully run this test by selecting the method name even though there is an @Ignore attribute. I'm guessing Eclipse also allows this?

However, when I try to do the same behavior in Maven by executing the following -

mvn -Dtest=HealthCareGov#performancetest test

It appears that Maven skips the test. This is the output -

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running HealthCareGov
Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.032 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 1

What Maven command line can I use to force the execution of this test to reproduce the behavior that my IDE produces? Modifying the Java source code is not an option.

like image 870
Josh Unger Avatar asked Dec 17 '13 14:12

Josh Unger


People also ask

How do I ignore a test in Maven?

To skip running the tests for a particular project, set the skipTests property to true. You can also skip the tests via the command line by executing the following command: mvn install -DskipTests.

How can you override skipTests parameter in command line?

Skipping by default by initializing the skipTests element value to true in properties and then overriding it through the command line by mentioning the value of -DskipTests in the maven phase/build execution command.


2 Answers

This can be added to the test.

@IfProfileValue(name="test-profile", value="IntegrationTest")
public class PendingChangesITCase extends AbstractControllerIntegrationTest {
    ...
}

To select the tests to be executed just add the value to the profile for executing the integration tests.

<properties>
    <test-profile>IntegrationTest</test-profile>
</properties>

Also, you can use the command line to set the test-profile variable:

mvn -Dtest-profile=IntegrationTest
like image 146
jordiburgos Avatar answered Sep 28 '22 08:09

jordiburgos


Instead of overriding the @Ignore annotation, I did the following:

  • Name your test that you wish to only run from the command-line outside of the standard maven test runner nomenclature (surefire or failsafe). This will ensure that your test does not get picked up by the regular maven lifecycle test execution.
  • Write your test as normal, to include annotations for your framework on test methods.
  • When you wish to run your test, just include it manually by feeding it as a parameter to the runner.

    -Dtest=Foo # for surefire

    -Dit.test=Foo # for failsafe

This will force you to manually specify the test via a command-line argument when you want to run it, but omit the test otherwise.

like image 32
josh-cain Avatar answered Sep 28 '22 09:09

josh-cain