Maven 如果不提供完整的包名,则无法使用TestNG测试注释

Maven 如果不提供完整的包名,则无法使用TestNG测试注释,maven,intellij-idea,testng,Maven,Intellij Idea,Testng,在最新的Intellij IDEA Ultimate中,我无法使用@Test注释而不获得红色错误线。唯一可行的方法是我提供完整的包名,如下所示: import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.Ch

在最新的Intellij IDEA Ultimate中,我无法使用@Test注释而不获得红色错误线。唯一可行的方法是我提供完整的包名,如下所示:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class Test {
private WebDriver driver;

@BeforeTest
public void setup(){
    System.setProperty("webdriver.chrome.driver", "/Users/jeff/IdeaProjects/Practice/src/chromedriver");
    driver = new ChromeDriver();
    driver.get("http://google.com");
}

@org.testng.annotations.Test
public void test(){
    WebElement searchBox = driver.findElement(By.id("lst-ib"));
    searchBox.sendKeys("Hello World");
    searchBox.sendKeys(Keys.RETURN);
}

@AfterTest
public void tearDown() throws Exception{
    Thread.sleep(3000);
    driver.quit();
}
}
如您所见,其他注释工作正常。由于我在首选项中勾选了“动态添加明确导入”和“动态优化导入”选项,因此当我继续添加以下内容时:

import org.testng.annotations.Test;
它变灰了,因为它不在使用…但它是

这是我的maven 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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.Practice</groupId>
    <artifactId>Practice</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.10</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.mashape.unirest</groupId>
            <artifactId>unirest-java</artifactId>
            <version>1.4.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpasyncclient</artifactId>
            <version>4.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

4.0.0
com.Practice
实践
1.0-快照
org.testng
testng
6.10
org.seleniumhq.selenium
硒爪哇
3.0.1
org.seleniumhq.selenium
硒铬驱动器
3.0.1
org.seleniumhq.selenium
selenium firefox驱动程序
3.0.1
com.mashape.unirest
unirest java
1.4.9
org.apache.httpcomponents
httpclient
4.3.6
org.apache.httpcomponents
httpasyncclient
4.0.2
org.apache.httpcomponents
httpime
4.3.6
org.json
json
20140107
朱尼特
朱尼特
4.11
测试
公地io
公地io
2.4
测试

您的类本身命名为
Test
,因此需要完全限定的注释类名来防止冲突。将类重命名为
SomeTest
,您将能够使用导入的
@Test
注释。

您能尝试从类路径中删除junit吗?@juherr,即使在我删除junit之后,似乎仍然不起作用。看起来,即使我为测试方法执行自动生成,它仍然使用注释的完整包名。