【问题标题】:Cannot resolve symbol 'Before' - Intellij jUnit无法解析符号“之前” - Intellij jUnit
【发布时间】:2018-06-18 13:12:17
【问题描述】:

我是 Intellij IDEA 的新手。我正在尝试将 jUnit 注释 @Before 和 @After 用于我的 selenium 测试。即使我在课堂上导入 jUnit,我也无法使用注释。有什么帮助吗?

package Config;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;

public class browserConfig {

    public static WebDriver driver = null;
    public static Properties config = new Properties();
    public static FileInputStream fis;

    @Before
    public void initBrowser(){

        if (driver ==null){

            try{

                fis = new FileInputStream(System.getProperty("user.dir") + "//src//main//java//Config//config.properties");

            } catch (FileNotFoundException e) {

                e.printStackTrace();

            }

【问题讨论】:

  • 你使用的依赖是什么?
  • 为什么是“if driver == null”?该检查违背了@Before 注释的目的
  • 当您点击“构建项目”时,IntelliJ 会抱怨吗?
  • @Pooya 我在构建项目中收到以下错误:错误:(10、17)java:包 org.junit 不存在错误:(11、17)java:包 org.junit 不存在错误:(19、6)java:找不到符号符号:类之前的位置:类Config.browserConfig
  • @UzIT 这意味着你的依赖有问题

标签: java intellij-idea junit


【解决方案1】:

你的测试应该在测试文件夹中,否则idea会忽略测试注释。 确保该类放置在 src/test/ 文件夹中。 还要检查 Project Settings -> Modules 您的测试文件夹是否存在于结构中并具有绿色(标记为测试)。如果缺少 - 将其添加到模块中并标记为测试。

【讨论】:

  • 谁说它不在测试源文件夹中?
  • 请检查 IDEA 是否将您的文件夹识别为测试,正如我在更新中所述。这是您尝试从非测试文件夹运行测试时的标准行为。
  • @user2529284 我以这样一种方式构建了我的代码,即我在 main 中有配置文件,该文件将具有 Before 和 After 方法,并且我将在我的测试中使用 Test 将驻留在我的测试文件夹中跨度>
  • @UzIT 您需要将测试与应用程序分开,这是一个很好的做法,IDEA 不允许您从非测试文件夹运行任何与测试相关的注释。您应该在方法之前和之后移动到相应的测试类
  • @user2529284 如果是gradle项目,一定要刷新gradle依赖
【解决方案2】:

如果您在 Intellij 中使用 gradle 构建,请尝试将其添加到 build.gradle。

testCompile('org.springframework.boot:spring-boot-starter-test')

如果您不使用 gradle,请对您的依赖项进行任何控制。

【讨论】:

    猜你喜欢
    • 2020-11-24
    • 1970-01-01
    • 2016-10-12
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 2019-02-21
    • 2019-03-09
    相关资源
    最近更新 更多