Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run junit tests by maven in terminal with JVM arguments

Tags:

java

junit

maven

As mentioned in here we can run test methods using,

mvn -Dtest=TestCircle#xyz test

But I need to set some JVM arguments before run the test case. Like I need to use

-Djava.security.manager -Djava.security.policy=mypolicy.policy

How can I tell maven to consider those when running a test case.

like image 885
prime Avatar asked Jan 10 '17 16:01

prime


People also ask

How do I run a JUnit test from command line Maven?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests.

How do I run a Maven test in terminal?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.


1 Answers

Two possible solutions:

First, if your JVM arguments are applicable to all tests, you can add such information as a configuration item for Surefire as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    ...
    <configuration>
        <argLine>-Djava.security.manager -Djava.security.policy=mypolicy.policy</argLine>
    </configuration>
</plugin>

Second, if such JVM arguments are to be applied on a test-by-test basis, they can be specified on the command line as follows:

mvn -Dtest=TestCircle#xyz test -DargLine="-Djava.security.manager -Djava.security.policy=mypolicy.policy"
like image 95
Frelling Avatar answered Sep 20 '22 14:09

Frelling