junit注释_JUnit注释

junit注释

JUnit testing framework is built on annotations. JUnit 5 is a major upgrade from JUnit 4. There are different modules and we need JUnit Platform and JUnit Jupiter API to create and run the test cases for our java project.

JUnit测试框架基于注释构建。 JUnit 5是对JUnit 4的主要升级。有不同的模块,我们需要JUnit Platform和JUnit Jupiter API才能为我们的java项目创建和运行测试用例。

JUnit注释 (JUnit Annotations)

Let’s look at the most important annotations in JUnit 5 Jupiter API. Most of them are located in the org.junit.jupiter.api package in the junit-jupiter-api module.

让我们看一下JUnit 5 Jupiter API中最重要的注释。 它们中的大多数位于junit-jupiter-api模块的org.junit.jupiter.api包中。

@测试 (@Test)

@Test annotation is used to specify that the annotated method is a test method.

@Test注释用于指定带注释的方法是测试方法。

@Test methods must not be private or static. @Test methods must not return a value.

@Test方法不得为私有或静态。 @Test方法不能返回值。

@Test methods may optionally declare parameters to be resolved by ParameterResolvers.

@Test方法可以选择声明要由ParameterResolvers解析的参数。

Here is a simple example of the @Test method.

这是@Test方法的简单示例。

@Test
void testMethod1() {
  System.out.println("test method");
}

@ParameterizedTest (@ParameterizedTest)

JUnit @ParameterizedTest annotation is used to run a test method multiple times with different arguments. This annotation is defined in junit-jupiter-params module. This annotation also requires us to define a source for the method arguments. Some of the annotations that can be used as arguments provider are ValueSource, EnumSource, MethodSource, CsvSource, and CsvFileSource. Here is a simple example of using @ParameterizedTest annotation.

JUnit @ParameterizedTest批注用于使用不同的参数多次运行测试方法。 此注释在junit-jupiter-params模块中定义。 此注释还要求我们为方法参数定义源。 可用作参数提供者的一些注释是ValueSourceEnumSourceMethodSourceCsvSourceCsvFileSource 。 这是使用@ParameterizedTest批注的简单示例。

@ParameterizedTest
@ValueSource(strings = { "1", "2", "3" })
void test_ValueSource_String(String s) {
	assertTrue(Integer.parseInt(s) < 5);
}

You can read more about them at JUnit Parameterized Tests.

您可以在JUnit Parameterized Tests上阅读有关它们的更多信息。

@RepeatedTest (@RepeatedTest)

JUnit @RepeatedTest annotation is used to repeat a test specified number of times.

JUnit @RepeatedTest批注用于重复指定次数的测试。

@RepeatedTest(5)
void test() {
	System.out.println("@RepeatedTest Simple Example");
}

Above test will be executed 5 times, read more at JUnit Repeated Tests.

上面的测试将执行5次,更多内容请参见JUnit重复测试

@TestFactory (@TestFactory)

JUnit @TestFactory annotation coupled with DynamicTest can be used to create a test factory method. JUnit @TestFactory methods must not be private or static. These methods must return a Stream, Collection, Iterable, or Iterator of DynamicNode instances. Here is a simple example of the @TestFactory method to create dynamic tests at runtime.

可以将JUnit @TestFactory批注与DynamicTest结合使用,以创建测试工厂方法。 JUnit @TestFactory方法不得为私有或静态。 这些方法必须返回DynamicNode实例的Stream,Collection,Iterable或Iterator。 这是@TestFactory方法的简单示例,可在运行时创建动态测试。

@TestFactory
Collection<DynamicTest> dynamicTests() {
    return Arrays.asList(
        dynamicTest("simple dynamic test", () -> assertTrue(true)),
        dynamicTest("My Executable Class", new MyExecutable()),
        dynamicTest("simple dynamic test-2", () -> assertTrue(true))
    );
}

We can generate dynamic tests for a class method too, read more at JUnit 5 Dynamic Tests.

我们也可以为类方法生成动态测试,更多内容请参见JUnit 5 Dynamic Tests

@TestInstance (@TestInstance)

We can use @TestInstance annotation to change the lifecycle behavior of a class. By default, JUnit creates a new instance of each test class before executing each test method. This behavior is called “per-method” test instance lifecycle. We can change it to “per-class” mode for faster processing of tests because a new test instance will be created once per test class.

我们可以使用@TestInstance批注来更改类的生命周期行为。 默认情况下,JUnit在执行每个测试方法之前为每个测试类创建一个新实例。 此行为称为“按方法”测试实例生命周期。 我们可以将其更改为“每类”模式,以便更快地处理测试,因为每个测试类都会创建一次新的测试实例。

@TestInstance(Lifecycle.PER_CLASS)
class InnerClass {
//tests
}

@显示名称 (@DisplayName)

This annotations is used to define a custom display name for test class and methods.

此批注用于为测试类和方法定义自定义显示名称。

@DisplayName("MyTestClass")
public class DisplayNameTest {
}

@Test
@DisplayName("Example Test Method with No Business Logic")
void test() {
	assertTrue(3 > 0);
}

You can read more about it at JUnit Display Name.

您可以在JUnit Display Name上了解有关它的更多信息。

@嵌套 (@Nested)

JUnit Jupiter @Nested annotation is used to mark a nested class to be included in the test cases. By default, nested classes are not scanned for test methods. The nested class should be non-static. You can read more about them at JUnit Nested Tests.

JUnit Jupiter @Nested批注用于标记要包含在测试用例中的嵌套类。 默认情况下,不扫描嵌套类的测试方法。 嵌套类应该是非静态的。 您可以在JUnit嵌套测试中阅读有关它们的更多信息。

@已停用 (@Disabled)

JUnit @Disabled annotation is the easiest way to disable a test. It can be applied to a test method as well as on the Class itself.

JUnit @Disabled批注是禁用测试的最简单方法。 它既可以应用于测试方法,也可以应用于类本身。

@Test
@Disabled
void test() {
	assertTrue(true);
}

@Disabled("Explicitly Disabled")
class DisabledTests {
//all tests disabled
}

JUnit Jupiter provides various annotations to enable or disable a test based on specified condition. Some of them are @DisabledOnOs, @EnabledOnOs, @DisabledOnJre, @EnabledOnJre, @DisabledIfEnvironmentVariable, @EnabledIfEnvironmentVariable, @DisabledIfSystemProperty, @EnabledIfSystemProperty, @DisabledIf, and @EnabledIf.

JUnit Jupiter提供了各种批注以根据指定条件启用或禁用测试。 其中一些是@DisabledOnOs @EnabledOnOs@DisabledOnJre @EnabledOnJre@DisabledIfEnvironmentVariable @EnabledIfEnvironmentVariable@DisabledIfSystemProperty @EnabledIfSystemProperty@DisabledIf @DisabledOnOs@EnabledOnOs @DisabledOnJre@EnabledOnJre @DisabledIfEnvironmentVariable@EnabledIfEnvironmentVariable @DisabledIfSystemProperty@EnabledIfSystemProperty @DisabledIf@EnabledIf

You can get complete details about these annotations at JUnit Disable Enable Tests.

您可以在JUnit禁用启用测试中获得有关这些注释的完整详细信息。

JUnit生命周期回调注释 (JUnit Lifecycle Callback Annotations)

There are four annotations that we can use to define callback methods at various stages of test cases execution.

在测试用例执行的各个阶段,可以使用四个注释来定义回调方法。

@BeforeEach (@BeforeEach)

Denotes that the annotated method should be executed before each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class.

表示带注释的方法应该在当前类中的每个 @ Test,@ RepeatedTest,@ ParameterizedTest或@TestFactory方法之前执行。

@AfterEach (@AfterEach)

Used to define a method to be executed after each test method in the current class. These methods gets executed for each tests in the nested classes too.

用于定义当前类中每个测试方法之后要执行的方法。 这些方法也将为嵌套类中的每个测试执行。

@BeforeAll (@BeforeAll)

Used to define a method to be executed before all test methods in the current class. This method must be static, unless TestInstance is set to “per-class” mode.

用于定义要在当前类中的所有测试方法之前执行的方法。 除非将TestInstance设置为“按类”模式,否则此方法必须是静态的。

@毕竟 (@AfterAll)

This annotation is used to define a method to be executed after all test methods in the current class. This method must be static, unless TestInstance is set to “per-class” mode.

此注释用于定义要在当前类中的所有测试方法之后执行的方法。 除非将TestInstance设置为“按类”模式,否则此方法必须是静态的。

@BeforeAll
static void setUpBeforeClass() throws Exception {
	System.out.println("Set Up Before Class - @BeforeAll");
}

@AfterAll
static void tearDownAfterClass() throws Exception {
	System.out.println("Tear Down After Class - @AfterAll");
}

@BeforeEach
void setUp() throws Exception {
	System.out.println("Set Up @BeforeEach");
}

@AfterEach
void tearDown() throws Exception {
	System.out.println("Tear Down @AfterEach");
}

JUnit lifecycle callback methods can be used to initialize or destroy resources for the test. They can also be used to reset any shared variables before test cases are executed.

JUnit生命周期回调方法可用于初始化或破坏测试资源。 它们也可以用于在执行测试用例之前重置任何共享变量。

摘要 (Summary)

JUnit testing framework is built on Java annotations. They are an integral part of JUnit framework and helps us in writing tests easily with less code.

JUnit测试框架基于Java批注构建。 它们是JUnit框架的组成部分,可帮助我们以更少的代码轻松编写测试。

GitHub Repository project. GitHub Repository项目中查看更多JUnit 5示例。

翻译自: https://www.journaldev.com/21735/junit-annotations

junit注释

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值