【问题标题】:I want to fix issue with @Before annotation我想解决 @Before 注释的问题
【发布时间】:2020-01-12 10:25:25
【问题描述】:

这是我解决不了的@before注解

@Before
public void initDb() {
    {
        User newUser = new User("testUser@mail.com", "testUser", "****");
        userService.createUser(newUser);
    }
    {
        User newUser = new User("testAdmin@mail.com", "testUser", "****");
        userService.createUser(newUser);
    }
}

以下是我的所有导入

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.List;
import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.leadApp.Entities.Task;
import com.leadApp.Entities.User;
import com.leadApp.services.TaskService;
import com.leadApp.services.UserService;

如果有人能告诉我下一步我能做什么,我将不胜感激,我正在使用 eclipse Oxygen

【问题讨论】:

  • 提示:您忘记提出问题或说明您的问题究竟是什么。
  • 我建议删除initDb() 中的块。在 Java 中打开块是不常见的。

标签: java spring spring-boot annotations


【解决方案1】:

您似乎正在使用 JUnit 5 AKA JUnit Jupiter。

注解命名为BeforeEach,而不是Before。当然,它应该从org.junit.jupiter 包中导入,而不是从org.aspectj.lang.annotation 导入,这与JUnit 无关。

有一个指南,你应该阅读它:https://junit.org/junit5/docs/current/user-guide/

【讨论】:

  • 感谢您,当我将注释更改为 @BeforeEach 时,它已修复...
猜你喜欢
  • 2018-06-28
  • 2011-06-21
  • 1970-01-01
  • 2021-06-30
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
相关资源
最近更新 更多