小编Luc*_*uca的帖子

Maven在运行时不会运行@BeforeEach方法

我有一个测试类可以调用它TestSomething,一个测试对象可以调用它SomeObject

现在,我在每项新的单项测试中都需要该对象,这意味着我在我的代码中有一个@BeforeEach将该对象加载到字段中:

import me.test.SomeObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TestSomething {
    private SomeObject someObject;

    @BeforeEach
    public void load() {
        someObject = new SomeObject();
    }

    @Test
    public void test1() {
        boolean result = someObject.checkForSomething();
        Assertions.assertEquals(true, result);
    }

    @Test
    public void test2() {
        boolean result = someObject.checkForSomethingElse();
        Assertions.assertEquals(false, result);
    }
Run Code Online (Sandbox Code Playgroud)

来自测试模块的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <parent>
    <artifactId>test</artifactId>
    <groupId>me.test</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>


  <properties>
    <projectVersion>1.0.0</projectVersion>
    <maven.deploy.skip>false</maven.deploy.skip>
  </properties>


  <artifactId>Tests</artifactId>
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId> …
Run Code Online (Sandbox Code Playgroud)

java junit maven maven-surefire-plugin junit5

4
推荐指数
2
解决办法
1653
查看次数

标签 统计

java ×1

junit ×1

junit5 ×1

maven ×1

maven-surefire-plugin ×1