JUnit 5 @Test Annotation – JUnit 5 @Test 注释

最后修改: 2017年 12月 19日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we’ll make a quick review of JUnit’s @Test annotation. This annotation provides a powerful tool for performing unit and regression testing.

在这篇文章中,我们将对JUnit的@Test注解做一个简单的回顾。这个注解为执行单元和回归测试提供了一个强大的工具。

2. Maven Configuration

2.Maven配置

To use the latest version of JUnit 5, we’ll need to add the following Maven dependency:

要使用最新版本的JUnit 5,我们需要添加以下Maven依赖。

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.8.1</version>
    <scope>test</scope>
</dependency>

We use the test scope because we don’t want Maven to include this dependency in our final build.

我们使用test范围,因为我们不希望Maven在最终构建时包含这一依赖关系。

Since the surefire plugin doesn’t still natively fully support JUnit 5, we’ll also need to add a provider, which tells Maven where to find our tests:

由于surefire插件并不完全支持JUnit 5,我们还需要添加一个提供者,它告诉Maven在哪里可以找到我们的测试。

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.0.2</version>
        </dependency>
    </dependencies>
</plugin>

In our configuration, we’ll use surefire 2.19.1 because, at the time of writing, version 2.20.x is not compatible with the junit-platform-surefire-provider.

在我们的配置中,我们将使用surefire 2.19.1,因为,在写作时,2.20.x版本与 junit-platform-surefire-provider不兼容。

3. Method Under Test

3.被测方法

First of all, let’s build a simple method that we’ll use in our test scenarios to showcase the @Test annotation’s capabilities:

首先,让我们建立一个简单的方法,我们将在测试场景中使用它来展示@Test注解的功能。

public boolean isNumberEven(Integer number) {
    return number % 2 == 0;
}

This method should return true if the argument passed is an even number and false otherwise. Now, let’s check out if it works the way it’s supposed to.

如果传递的参数是一个偶数,这个方法应该返回true,否则false。现在,让我们检查一下它是否按预期的方式工作。

4. Testing the Method

4.测试方法

For our example, we want to specifically check two scenarios:

对于我们的例子,我们要特别检查两种情况。

  • when given an even number, the method should return true
  • when given an odd number, the method should return false

This means that the implementation code will call our isNumberEven method with different parameters and check that the result is what we expect.

这意味着实现代码将用不同的参数调用我们的isNumberEven方法,并检查结果是否符合我们的预期。

In order for the tests to be recognized as such, we’ll add the @Test annotation. We can have as many of these as we want in a class, but it’s a good practice to put together only the related ones. Notice also that a test must not be private, nor may it return a value —otherwise it’ll just be ignored.

为了使测试被识别出来,我们将添加@Test 注解。我们可以在一个类中拥有任意多的注解,但只将相关的注解放在一起是一个好的做法。请注意,测试不能是private,也不能返回一个值–否则它将被忽略。

Given these considerations, let’s write our test methods:

鉴于这些考虑,让我们来编写我们的测试方法。

@Test
void givenEvenNumber_whenCheckingIsNumberEven_thenTrue() {
    boolean result = bean.isNumberEven(8);

    Assertions.assertTrue(result);
}

@Test
void givenOddNumber_whenCheckingIsNumberEven_thenFalse() {
    boolean result = bean.isNumberEven(3);

    Assertions.assertFalse(result);
}

If we now run a Maven build, the surefire plugin will go through all the annotated methods in the classes placed under src/test/java and execute them, causing the build to fail if any test failures occur.

如果我们现在运行Maven构建,surefire插件将浏览放置在src/test/java 下的类中的所有注释方法并执行它们,如果出现任何测试失败,将导致构建失败

If you come from JUnit 4, be aware that in this version the annotation doesn’t accept any parameters. To check for a timeout or an exception thrown we would use assertions instead:

如果你来自JUnit 4,请注意,在这个版本中,注解不接受任何参数。为了检查超时或抛出的异常,我们将使用断言来代替。

@Test
void givenLowerThanTenNumber_whenCheckingIsNumberEven_thenResultUnderTenMillis() {
    Assertions.assertTimeout(Duration.ofMillis(10), () -> bean.isNumberEven(3));
}
	
@Test
void givenNull_whenCheckingIsNumberEven_thenNullPointerException() {
    Assertions.assertThrows(NullPointerException.class, () -> bean.isNumberEven(null));
}

5. Conclusion

5.结论

In this quick tutorial, we showed how to implement and run a simple JUnit test with the @Test annotation.

在这个快速教程中,我们展示了如何用@Test注解来实现和运行一个简单的JUnit测试。

More about the JUnit framework can be found in this post which provides a general introduction.

关于JUnit框架的更多信息可以在这篇文章中找到,这篇文章提供了一个总体介绍。

All the code used in the examples is available in the GitHub project.

例子中使用的所有代码都可以在GitHub项目中找到。