不能将类junit.framework.testSuite强制转换为类API

eqoofvh9  于 2023-02-19  发布在  其他
关注(0)|答案(2)|浏览(140)

我是JAVA和Junit的新手,尝试做一些简单的事情。我通过了测试,但我在终端initailizationError端看到这个错误“class junit.framework.TestSuite无法转换为class org.junit.jupiter.API.Test”这是我的Junit依赖项的版本。

<dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.9.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.9.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>

这是我要做的测试

import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;

/**
 * Unit test for simple App.
 */
public class AppTest
    extends TestCase
{
    @Test
    public void firstTest() {
        Assertions.assertEquals(2, 2);

    }

    /**
     * Create the test case
     *
     * @param testName name of the test case
     */

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return (Test) new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

我无法理解这个错误,因为这是我第一次使用它

mf98qq94

mf98qq941#

我认为最简单的解决方法是删除suite方法。您不需要它,没有它您的测试也会运行得很好。一旦您对Java和JUnit更有信心和熟悉,那么 * 也许 * 套件将帮助您组织和分组测试,但您肯定可以开始没有它们。
您尝试创建测试套件的方式似乎遵循了JUnit 3的方法,但您使用的是JUnit 5。JUnit与JUnit 3和JUnit 4相比有很大变化,从JUnit 4到JUnit 5也有很大变化,因此JUnit 3中的某些内容不适用于JUnit 5也就不足为奇了。
我以前从未见过这种编写测试套件的方式,但我确实发现this page介绍了JUnit 5,然后给出了使用JUnit 3的示例。坦率地说,我发现该页面的内容质量很差,不推荐使用。如果您正在使用该页面了解JUnit,那么我建议您到别处去看看。

np8igboo

np8igboo2#

问题来自以下几行:

public static Test suite()
    {
        return (Test) new TestSuite( AppTest.class );
    }

你正在尝试施放2个不同的职业:

class junit.framework.TestSuite

class org.junit.jupiter.api.Test

这是2个不同的类,您可以简单地删除此函数,错误将被修复。
或者如果你想有一个TestSuite来捆绑一些单元测试用例并一起运行它们,它可以在一个单独的类中。
article中所述
希望这对你有帮助!

相关问题