You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IntelliJ中JUnit测试未找到(退出码0)问题排查求助

测试未被识别及JUnit依赖报错的排查解决

问题现象

  • 运行测试后提示 Process finished with exit code 0,未找到任何测试用例
  • 同时出现JUnit Vintage和Jupiter引擎的报错信息

测试类代码

public class MyStoreTest {
private WebDriver driver;

@BeforeEach
public void beforeEach() {
    System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver.exe");
    this.driver = new ChromeDriver();
    this.driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(16));
    this.driver.get("https://mystore-testlab.coderslab.pl/index.php");
}

@Test
public void MyStoreTest() {
    MyStoreMainPage MyStoreMainPage = new MyStoreMainPage(driver);
    MyStoreMainPage.clickSignIn();

    MyStoreAuthenticationPage authenticationPage = new MyStoreAuthenticationPage(driver);
    String email =  "oetuomldfaacfducoz@bvhrk.com";
    String password = "1992Dawid";
    authenticationPage.submitSignIn(email , password);

    MyStoreAddAnAddressPage AddAnAddress = new MyStoreAddAnAddressPage(driver);
    AddressData AddressData = new AddressData()
            .setAddress("Ul.Pulawska")
            .setZip("02-508")
            .setCity("Warszawa");

    AddAnAddress.AddAnAddress(AddressData);

    MyStoreMyAccountPage myAccountPage = new MyStoreMyAccountPage(driver);
    assertTrue(myAccountPage.isAddressSuccessfullyAdded());
}

报错信息

WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
org.junit.platform.commons.JUnitException: Failed to parse version of junit:junit: 4.13.2
at org.junit.vintage.engine.JUnit4VersionCheck.parseVersion(JUnit4VersionCheck.java:54)
...(省略后续栈追踪内容)

WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
java.lang.NoClassDefFoundError: org/junit/jupiter/api/io/CleanupMode
at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:66)
...(省略后续栈追踪内容)

相关辅助类代码

AddressData类

public class AddressData {

private String Address;
private String City;
private String Zip;

public String getAddress() {
    return Address;
}

public AddressData setAddress(String Address) {
    this.Address = Address;
    return this;
}

public String getCity() {
    return City;
}

public AddressData setCity(String City) {
    this.City = City;
    return this;
}

public String getZip() {
    return Zip;
}

public AddressData setZip(String Zip) {
    this.Zip = Zip;
    return this;
}

MyStoreAddAnAddressPage类

public class MyStoreAddAnAddressPage {
public WebDriver driver;
public MyStoreAddAnAddressPage(WebDriver driver) {
    this.driver = driver;
}
public void AddAnAddress(AddressData AddressData) {

    WebElement AddressInput = driver.findElement(By.name("address1"));
    AddressInput.sendKeys(AddressData.getAddress());
    WebElement CityInput = driver.findElement(By.name("city"));
    CityInput.sendKeys(AddressData.getCity());
    WebElement ZipInput = driver.findElement(By.name("postcode"));
    ZipInput.sendKeys(AddressData.getZip());

    WebElement saveButton = this.driver.findElement(
            By.name("submitAddress")
    );
    saveButton.click();
}

MyStoreAuthenticationPage类

public class MyStoreAuthenticationPage {

@FindBy(css = "href#log.in.to.your.customer.account")
private WebElement signIn;

@FindBy(name = "email")
private WebElement emailInput;

@FindBy(name = "password")
private WebElement passwordInput;

@FindBy(id = "submit-login")
private WebElement signInButton;
private WebDriver driver;


public MyStoreAuthenticationPage(WebDriver driver) {
    PageFactory.initElements(driver, this);
}

public void submitSignIn(String email, String password) {
    emailInput.sendKeys(email);
    passwordInput.sendKeys(password);
    signInButton.click();
}

MyStoreMainPage类

public class MyStoreMainPage {
public WebDriver driver;
public MyStoreMainPage(WebDriver driver) {
    this.driver = driver;
}
public void clickSignIn() {
    WebElement signIn = this.driver.findElement(
            By.cssSelector("href#log.in.to.your.customer.account")
    );
    signIn.click();
}

MyStoreMyAccountPage类

public class MyStoreMyAccountPage {
public WebDriver driver;

public MyStoreMyAccountPage(WebDriver driver) {
    this.driver = driver;
}

public boolean isAddressSuccessfullyAdded() {
    WebElement confirmationPanel = this.driver.findElement(
            By.cssSelector("article.alert.alert-success")
    );
    String panelText = confirmationPanel.getText();

   return panelText.equals("Address successfully added!");
}

排查与解决步骤

1. 修复JUnit依赖版本冲突

报错中的CleanupMode类找不到、JUnit版本解析失败,都是依赖版本不兼容导致的:

  • 统一JUnit Jupiter(JUnit 5)所有依赖的版本,比如使用5.9.0或更高稳定版
  • 移除不必要的JUnit 4依赖;若需兼容JUnit 4,确保junit-vintage-engine版本与JUnit Jupiter匹配
  • 检查构建文件(Maven的pom.xml或Gradle的build.gradle),示例Maven配置:
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.9.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.9.0</version>
        <scope>test</scope>
    </dependency>
    
  • 刷新项目依赖,清理IntelliJ缓存(File -> Invalidate Caches...)

2. 修正测试方法命名规范

JUnit要求测试方法名不能与类名完全相同,将测试方法名修改为符合规范的名称:

@Test
public void testAddAddressSuccessfully() {
    // 原有测试逻辑保持不变
}

3. 检查注解导入正确性

确保@Test、@BeforeEach来自JUnit Jupiter包,而非JUnit 4:

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

4. 修复页面元素定位问题

虽然不是测试未识别的直接原因,但错误的定位会导致测试执行失败:

  • MyStoreMainPage和MyStoreAuthenticationPage中的CSS选择器无效,替换为正确的选择器,比如:
    WebElement signIn = this.driver.findElement(By.cssSelector("a[title='Log in to your customer account']"));
    
  • 遵循Java命名规范,成员变量和方法名使用小驼峰(如Address改为address,AddAnAddress改为addAnAddress)

5. 验证IntelliJ测试运行配置

  • 确保运行配置为JUnit类型,而非Application
  • 检查配置中的测试类路径,选择正确的模块和测试类

内容的提问来源于stack exchange,提问作者Katarzyna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 07:05:24