Maven

Maven is a decentralized marketplace where users can buy and sell goods or services with cryptocurrency. It has an open-source codebase that enables anyone to build applications on top of the platform.

Maven is a software project management and comprehension tool. It allows users to manage their projects with the use of a central repository where they can store any type of files.

Using plenty of examples, we’ll go through how to run single tests or particular unit tests with Maven in this short lesson.

1. Maven Maven Maven Maven Maven Maven Maven Maven Maven Maven

Maven performs all tests in the test phase by default. It is often necessary to run a single test or a set of tests. An overview of how to run unit tests using Maven is shown in the picture below.

maven-run-single-test

Before we get into the examples, let’s have a look at the project structure and testing setup.

Structure of the project:

run-tests-maven-project

Configuring Maven:

run-maven-tests 4.0.0 com.javabydeveloper run-maven-tests 0.0.1-SNAPSHOT jar 5.5.2 Tutorial: https://javabydeveloper.com/junit-5-tutorial/ 3.0.0-M5 org. 1.8 org. junit.jupiter junit-jupiter-engine maven-surefire-plugin $maven-surefire-plugin.version $junit.jupiter.version $maven-surefire-plugin.version $maven-surefire-plugin.version $maven-surefire-plugin.version $maven-surefire-p

A quick glance at the test scenarios is as follows:

@Test void test add() public class MathUtilTest assertEquals(5, MathUtil.add(3, 2),”test Add()”); assertEquals(15, MathUtil.multiple(3, 5)); @Test void test multiply(); assertEquals(5, MathUtil.devide(25, 5)); @Test void test devide(); void test is prime @Test @Test @Test @Test @Test @Test @Test () public class MessageUtilTest @Test void msg add test() assertEquals(“Hello Dear!”, MessageUtil.msg add(“Hello”, “Dear!”)); assertEquals(“Hello Dear!”, MessageUtil.msg add(“Hello”, “Dear!”)); assertEquals(“Hello Dear!”, MessageUtil.msg add(” assertEquals(4, MessageUtil.msg length(“Five”)); @Test void msg length test(); assertEquals(true, MessageUtil.isLengthEven(“Four”)); @Test void is length even(); public class Payment Test assertEquals(“00”, Payment.doPay(“SUCCESS”)); @Test void payment success test(); assertEquals(“100”, Payment.doPay(“DECLINE”)); @Test void payment decline test(); assertEquals(“102”, Payment.doPay(“EXPIRED”)); @Test void invalid expiry date test();

Let’s look at how to use Maven to run single tests or particular tests from the test cases above.

1.1. Complete all tests

Run $ mvn test or $ mvn clean test to run all of the unit tests using Maven. Clear will clean all of the resources and compiled java code produced by maven in the target directory, allowing you to run tests again.

[INFO] $ mvn test ———————————————————————————- [INFO] T E S T S [INFORMATION] ———————————————————————————- [INFO] com.javabydeveloper.test is now running. MathUtilTest [INFO] Tests run: 4, failures: 0, errors: 0, errors: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, In com.javabydeveloper.test, time elapsed: 0.643 seconds. MathUtilTest [INFO] com.javabydeveloper.test.MessageUtilTest is being run. [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 3; Failures: 0; Errors: 0; Skipped: 0 In com.javabydeveloper.test.MessageUtilTest, time elapsed: 0.051 seconds. [INFO] com.javabydeveloper.test.Payment Test is now running. [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 3; Failures: 0; Errors: 0; Skipped: 0 0.044 seconds elapsed in com.javabydeveloper.test.Payment Test [INFO] [INFO] Results: [INFO] [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 10; Failures: 0; Errors: 0; Skipped: 0;

1.2. Use Maven to run a single test

Use the command $ mvn test -Dtest=MathUtilTest to run a single test with Maven. Only run a single test. MathUtilTest.

mvn test $ -Dtest=MathUtilTest [INFO] ———————————————————————————- [INFO] T E S T S [INFORMATION] ———————————————————————————- [INFO] com.javabydeveloper.test is now running. MathUtilTest [INFO] Tests run: 4, failures: 0, errors: 0, errors: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, In com.javabydeveloper.test.MathUtilTest, time elapsed: 0.212 s [INFO] [INFO] Results: [INFO] [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 4; Failures: 0; Errors: 0; Skipped: 0;

1.3. Carry out a series of tests

You may need to perform several particular tests at times. To run particular tests, use the Maven command $ mvn test -Dtest=MathUtilTest,Payment Test.

mvn test $ -Dtest=MathUtilTest,Payment Test [INFO] ———————————————————————————- [INFO] T E S T S [INFORMATION] ———————————————————————————- [INFO] com.javabydeveloper.test.MathUtilTest is being run. [INFO] Tests run: 4, failures: 0, errors: 0, errors: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, In com.javabydeveloper.test, time elapsed: 0.163 seconds. MathUtilTest [INFO] com.javabydeveloper.test.Payment Test is being run. [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 3; Failures: 0; Errors: 0; Skipped: 0 0.023 seconds elapsed in com.javabydeveloper.test.Payment Test [INFO] [INFO] Results: [INFO] [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 7; Failures: 0; Errors: 0; Skipped: 0;

1.4. Execute the Test Methods

Additionally, the Maven surefire plugin allows you to execute particular methods inside test classes. To execute test methods, use the maven command $ mvn test -Dtest=MathUtilTest#test add.

mvn test -Dtest=MathUtilTest#test add $ mvn test -Dtest=MathUtilTest#test add [INFO] ———————————————————————————- [INFO] T E S T S [INFORMATION] ———————————————————————————- [INFO] com.javabydeveloper.test.MathUtilTest is being run. [INFO] Tests run: 1, failures: 0, errors: 0, errors: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, In com.javabydeveloper.test.MathUtilTest, the time elapsed was 0.191 seconds. [INFO] [INFO] Results: [INFO] [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 1; Failures: 0; Errors: 0; Skipped: 0;

To execute several methods in a single test class, use the Maven command $ mvn test -Dtest=MathUtilTest#test add+test multiply.

mvn test -Dtest=MathUtilTest#test add+test multiply $ mvn test -Dtest=MathUtilTest#test add+test multiply [INFO] ———————————————————————————- [INFO] S E R V I C E [INFO] ———————————————————————————- [INFO] com.javabydeveloper.test is now running. MathUtilTest [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 2; Failures: 0; Errors: 0; Skipped: 0 0.191 seconds elapsed – in com .javabydeveloper.test.MathUtilTest [INFO] [INFO] Results: [INFO] [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 2; Failures: 0; Errors: 0; Skipped: 0;

1.5. Execute all tests in a single package

With Maven, there are a few different methods to execute the test cases inside a particular package. You may use either the entire package name or patterns instead.

To execute tests in a particular package, use the Maven command $ mvn test -Dtest=”com/javabydeveloper/test/**.” Supports $ mvn test -Dtest=”com.javabydeveloper.test.**” if you’re using Surefire plugin 2.19.1 or above.

mvn test -Dtest=”com.javabydeveloper.test.**” $ mvn test -Dtest=”com.javabydeveloper.test.**” [INFO] ———————————————————————————- [INFO] S E R V I C E [INFO] ———————————————————————————- [INFO] com.javabydeveloper.test is now running. MathUtilTest [INFO] Tests run: 4, failures: 0, errors: 0, errors: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, In com.javabydeveloper.test.MathUtilTest, time elapsed: 0.136 s [INFO] com.javabydeveloper.test.MessageUtilTest is now running. [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 3; Failures: 0; Errors: 0; Skipped: 0 In com.javabydeveloper.test.MessageUtilTest, time elapsed: 0.102 s [INFO] com.javabydeveloper.test.Payment Test is now running. [INFO] 3 tests performed, 0 failures, 0 errors, 0 skipped tests, 0.05 seconds elapsed – in com.javabydeveloper.test.Payment Test [INFO] [INFO] Results: [INFO] [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 10; Failures: 0; Errors: 0; Skipped: 0;

1.6. Use Patterns to Run Tests

Packages, test class method names, and test names may all be matched using patterns. mvn clean test -Dtest=*UtilTest* $ mvn clean test -Dtest=*UtilTest*

clean test $ mvn -Dtest=*UtilTest* [INFO] ———————————————————————————- [INFO] T E S T S [INFORMATION] ———————————————————————————- [INFO] com.javabydeveloper.test is now running. MathUtilTest [INFO] Tests run: 4, failures: 0, errors: 0, errors: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, In com.javabydeveloper.test, time elapsed: 0.238 seconds. MathUtilTest [INFO] com.javabydeveloper.test is now running. MessageUtilTest [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 3; Failures: 0; Errors: 0; Skipped: 0 In com.javabydeveloper.test, time elapsed: 0.023 seconds. MessageUtilTest [INFO] [INFO] Results: [INFO] [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 7; Failures: 0; Errors: 0; Skipped: 0;

You may also use the instructions below:

to run many methods in various test classes using pattern $ mvn test -Dtest=*UtilTest#*add*

mvn test -Dtest=”com.javabydeveloper.test.*#*add*+*is*” $ mvn test -Dtest=”com.javabydeveloper.test.*#*add*+*is*” – in all tests of a particular package, to execute test methods whose names include the words “add” or “is”

2. Final thoughts

With plenty of examples, we learnt how to run single tests or particular unit tests using Maven in this short tutorial.

You may also be interested in these Maven guides:

  1. Maven should be installed on Windows.
  2. Setup Maven on a Mac
  3. On Ubuntu, install Maven.
  4. Eclipse may be used to import a Maven project.
  5. Skip Tests in Maven
  6. Set the Java version using Maven.
  7. Run the Java main class using Maven.

3. List of references

  1. Document Maven

Ezoicthis advertisement should be reported

Maven is a build automation tool. It allows the user to create, manage and execute builds. The maven central is an online repository of all projects that use Maven.

Frequently Asked Questions

What is Maven in simple words?

Maven is a Java-based open source project that provides an application programming interface (API) for building software applications.

Why is Maven so bad?

Maven is a highly intelligent question answering bot. If you ask me a question, I will give you a detailed answer.

Is Maven the same as Jenkins?

Maven is a Java-based build automation tool that helps developers manage the lifecycle of software projects. Jenkins, on the other hand, is an open source continuous integration server written in Java.

  • maven download
  • maven dependency
  • maven tutorial
  • maven download for windows
  • maven clinic