Shell/Bash: maven test class Example


Shell/Bash Example: This is the "maven test class" Example. compiled from many sources on the internet by

maven test class

mvn install -Dmaven.test.skip=true

# or

mvn install -DskipTests

# If -Dmaven.test.skip=true (or simply -Dmaven.test.skip) is specified, 
# the test-jars aren't built, and any module that relies on them will 
# fail its build.

# In contrast, when you use -DskipTests, Maven does not run the tests, 
# but it does compile them and build the test-jar, making it available 
# for the subsequent modules.

maven test class

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
private final PrintStream originalErr = System.err;

public void setUpStreams() {
    System.setOut(new PrintStream(outContent));
    System.setErr(new PrintStream(errContent));

public void restoreStreams() {

sample test cases:

public void out() {
    assertEquals("hello", outContent.toString());

public void err() {
    System.err.print("hello again");
    assertEquals("hello again", errContent.toString());

maven test class

void testExpectedException() {
  Assertions.assertThrows(NumberFormatException.class, () -> {

maven test class

# Run all the unit test classes.
$ mvn test

# Run a single test class.
$ mvn -Dtest=TestApp1 test

# Run multiple test classes.
$ mvn -Dtest=TestApp1,TestApp2 test

# Run a single test method from a test class.
$ mvn -Dtest=TestApp1#methodname test

# Run all test methods that match pattern 'testHello*' from a test class.
$ mvn -Dtest=TestApp1#testHello* test

# Run all test methods match pattern 'testHello*' and 'testMagic*' from a test class.
$ mvn -Dtest=TestApp1#testHello*+testMagic* test

* Summary: This "maven test class" Shell/Bash Example is compiled from the internet. If you have any questions, please leave a comment. Thank you!