Java 无法解析符号“Before”-Intellij jUnit
我对Intellij的想法还不熟悉。我正在尝试在selenium测试中使用jUnit注释@Before和@After。即使我在类中导入jUnit,我也不能使用注释。有什么帮助吗Java 无法解析符号“Before”-Intellij jUnit,java,intellij-idea,junit,Java,Intellij Idea,Junit,我对Intellij的想法还不熟悉。我正在尝试在selenium测试中使用jUnit注释@Before和@After。即使我在类中导入jUnit,我也不能使用注释。有什么帮助吗 package Config; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import java.io.FileInputStream; import java.io.FileNotF
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();
}
您的测试应该在测试文件夹中,否则idea将忽略测试注释。 确保该类位于src/test/文件夹中。
另外,在项目设置->模块中检查您的测试文件夹是否存在于结构中,并且有绿色标记为测试。如果缺少-将其添加到模块并标记为测试。如果您使用gradle在Intellij中构建,请尝试将其添加到build.gradle
testCompile('org.springframework.boot:spring-boot-starter-test')
如果您不使用gradle,请对控制依赖项的内容执行类似操作。您使用的依赖项是什么?为什么If driver==null?该检查违背了@Before注释的目的当您点击构建项目IntelliJ抱怨吗?@Pooya我在构建项目上遇到以下错误:错误:10,17 java:package org.junit不存在错误:11,17 java:package org.junit不存在错误:19,6 java:在位置:class Config之前找不到symbol:class。browserConfig@UzIT这意味着您的依赖项存在问题,谁说它不在测试源文件夹中?请检查IDEA是否将您的文件夹识别为测试,如我在更新中所述。当您试图从非测试文件夹运行测试时,这是一种标准行为。@user2529284我以这样一种方式构造了我的代码,即我在main中有配置文件,其中包含Before和After方法,并且我将在测试中使用testfolder@UzIT您需要将测试与应用程序分开,首先,这是一个很好的实践,IDEA不允许您从非测试文件夹运行任何与测试相关的注释。您应该在方法之前和之后移动到相应的测试class@user2529284如果是gradle项目,请确保刷新gradle依赖项