杰瑞科技汇

Java Maven test如何执行与配置?

Of course! This is a fundamental topic in Java development. Let's break down "Java Maven Test" into a comprehensive guide, covering the "what," "why," and "how."

Java Maven test如何执行与配置?-图1
(图片来源网络,侵删)

What is Maven Testing?

In the context of Java and Maven, "testing" refers to the process of automatically running unit tests and integration tests to verify that your application code works as expected.

Maven has a built-in lifecycle and a dedicated plugin for this purpose. The most common goal for running tests is mvn test.

Why Use Maven for Testing?

  1. Standardization: Maven provides a standard directory structure (src/test/java) and a standard way to run tests (mvn test). This makes any Maven project familiar and easy to work with.
  2. Automation: You can easily integrate testing into your build process. This means tests can be run automatically on every code change (e.g., in a CI/CD pipeline like Jenkins, GitHub Actions, etc.), ensuring code quality.
  3. Dependency Management: Maven manages the libraries (JAR files) needed for testing. For example, it automatically includes JUnit or TestNG in your test classpath without you having to manually add them to your project's runtime classpath.
  4. Tool Integration: The Maven Surefire Plugin (which runs tests) has excellent integration with all major testing frameworks (JUnit, TestNG, etc.) and provides detailed, standardized reports on test results.

The Standard Maven Project Structure for Tests

Maven follows a convention-over-configuration philosophy. It expects your test files to be in a specific location:

my-app/
├── pom.xml              // Maven project file
└── src/
    ├── main/
    │   ├── java/        // Your application's source code
    │   │   └── com/
    │   │       └── mycompany/
    │   │           └── app/
    │   │               └── App.java
    │   └── resources/    // Your application's resources (config files, etc.)
    └── test/
        ├── java/        // Your test source code
        │   └── com/
        │       └── mycompany/
        │           └── app/
        │               └── AppTest.java
        └── resources/    // Test-specific resources (e.g., test database configs)

Key Points:

Java Maven test如何执行与配置?-图2
(图片来源网络,侵删)
  • src/main/java: Contains the production code.
  • src/test/java: Contains the test code. This code is not packaged into your final application JAR.
  • The package structure under src/test/java should mirror the structure under src/main/java.

A Practical Step-by-Step Example

Let's create a simple class and write a test for it.

Step 1: Create a New Maven Project

If you don't have a project, you can generate one using the Maven Archetype:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This will create the standard directory structure we saw above.

Step 2: Write the Production Code

Navigate to src/main/java/com/mycompany/app/App.java. Let's modify it to have a simple method we can test.

Java Maven test如何执行与配置?-图3
(图片来源网络,侵删)

src/main/java/com/mycompany/app/App.java

package com.mycompany.app;
/**
 * A simple utility class for calculations.
 */
public class App {
    /**
     * Adds two integers.
     * @param a The first number.
     * @param b The second number.
     * @return The sum of a and b.
     */
    public int add(int a, int b) {
        return a + b;
    }
    /**
     * Says hello.
     * @param name The name to greet.
     * @return A greeting string.
     */
    public String sayHello(String name) {
        return "Hello, " + name + "!";
    }
}

Step 3: Write the Test Code

Maven's archetype usually includes JUnit 4 or 5. We'll use JUnit 5 (the modern standard) for this example.

Navigate to src/test/java/com/mycompany/app/AppTest.java. Let's write a proper test.

src/test/java/com/mycompany/app/AppTest.java

package com.mycompany.app;
import org.junit.jupiter.api.Test; // Import JUnit 5's Test annotation
import static org.junit.jupiter.api.Assertions.assertEquals; // Import assertion methods
/**
 * Unit test for simple App.
 */
public class AppTest {
    @Test
    void testAdd() {
        // Arrange: Set up the test
        App app = new App();
        // Act: Perform the action to be tested
        int result = app.add(5, 10);
        // Assert: Check if the result is what we expect
        assertEquals(15, result, "The add method should correctly sum two integers");
    }
    @Test
    void testSayHello() {
        // Arrange
        App app = new App();
        // Act
        String greeting = app.sayHello("World");
        // Assert
        assertEquals("Hello, World!", greeting, "The sayHello method should return the correct greeting");
    }
}

Explanation of the Test Code:

  • @Test: This annotation from JUnit 5 marks the method as a test case. The Maven Surefire plugin will look for this annotation to find and run tests.
  • assertEquals(expected, actual, message): This is a core assertion method. It checks if the actual value is equal to the expected value. If not, the test fails, and the message is displayed.

Step 4: Run the Tests from the Command Line

Open your terminal or command prompt, navigate to the root directory of your project (where pom.xml is located), and run the following command:

mvn test

You will see Maven output in your console. The key part to look for is the result of the tests.

Expected Output:

...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mycompany.app.AppTest
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.123 s -- in com.mycompany.app.AppTest
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

This output shows that all tests passed successfully.

Step 5: (Optional) Introduce a Failure and See the Result

Let's make one of our tests fail. Change the testAdd method:

@Test
void testAdd() {
    App app = new App();
    int result = app.add(5, 10);
    // Intentionally fail the test by providing a wrong expected value
    assertEquals(99, result, "The add method should correctly sum two integers"); 
}

Now, run mvn test again.

Expected Output (with failure):

...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mycompany.app.AppTest
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.123 s <<< FAILURE!
[INFO] 
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
...
[ERROR] Failures:
[ERROR]   AppTest.testAdd:15 The add method should correctly sum two integers
[ERROR]       Expected :99
[ERROR]       Actual   :15
[ERROR] 
...

Maven now reports a BUILD FAILURE and gives you a detailed message about which test failed and why. This is incredibly useful for debugging.


Maven Test Phases and Goals

Maven's lifecycle is a sequence of phases. The testing phase is part of this lifecycle.

  • validate: Check if the project is correct.
  • compile: Compile the source code.
  • test: Compile and run the tests. This is the most common command.
  • package: Take the compiled code and package it into a distributable format (like a JAR or WAR).
  • verify: Run checks to verify the package is valid and meets quality criteria.

Key Takeaway: When you run mvn test, Maven first runs validate, compile, and then test. It does not run the package phase. This is efficient because you can quickly run tests without creating a package.

Viewing Test Reports

Maven automatically generates HTML reports for your tests. After running mvn test, you can find them in the target/site directory.

To open the report, navigate to target/site/index.html in your web browser. This gives you a nice, clickable view of all your test results, including failures and error traces.


Common Maven Test Scenarios

Skipping Tests

Sometimes you want to skip running tests (e.g., during a quick local build). You can do this in two ways:

  1. From the command line:

    mvn test -DskipTests

    This skips running the tests but still compiles the test code.

  2. From the pom.xml (not recommended for CI/CD):

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <configuration>
            <skipTests>true</skipTests>
        </configuration>
    </plugin>

Running Specific Tests

If you have many tests and only want to run one, you can use the -Dtest flag.

# Run a single test class
mvn test -Dtest=AppTest
# Run a single method within a class
mvn test -Dtest=AppTest#testAdd
# Run all tests in a package
mvn test -Dtest=com.mycompany.app.*

This is extremely useful for fast feedback during development.

分享:
扫描分享到社交APP
上一篇
下一篇