class junit.framework.TestSuite cannot be cast to class org.junit.jupiter.api.Test

huangapple go评论76阅读模式
英文:

class junit.framework.TestSuite cannot be cast to class org.junit.jupiter.api.Test

问题

I am new to JAVA and Junit and trying to do something simple. I have the test passed but I see in the terminal initailizationError side this error "class junit.framework.TestSuite cannot be cast to class org.junit.jupiter.api.Test" This is the version of my Junit dependencies.

<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>
</dependency>

This is the test that I am trying to run

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
     */

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

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

I could not understand the error as it is my first time using it.

英文:

I am new to JAVA and Junit and trying to do something simple. I have the test passed but I see in the terminal initailizationError side this error "class junit.framework.TestSuite cannot be cast to class org.junit.jupiter.api.Test" This is the version of my Junit dependencies.

 &lt;dependency&gt;
      &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
      &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
      &lt;version&gt;5.9.1&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
      &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
      &lt;version&gt;5.9.1&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
      &lt;artifactId&gt;junit-jupiter&lt;/artifactId&gt;
      &lt;version&gt;RELEASE&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;junit&lt;/groupId&gt;
      &lt;artifactId&gt;junit&lt;/artifactId&gt;
      &lt;version&gt;4.12&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;

This is the test that I am trying to run

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 );
    }
}

I could not understand the error as it is my first time useing it

答案1

得分: 2

我认为最简单的修复方法是删除您的 suite 方法。您不需要它,而且您的测试可以在没有它的情况下运行得很顺利。一旦您对Java和JUnit更加自信并熟悉,也许测试套件会帮助您组织和分组测试,但您肯定可以在没有它们的情况下开始。

您尝试创建测试套件的方式似乎遵循JUnit 3的方法,但您正在使用JUnit 5。JUnit从JUnit 3到JUnit 4,以及从JUnit 4到JUnit 5发生了很多变化,所以不足为奇JUnit 3的某些内容在JUnit 5中不起作用。

我以前没有见过以这种方式编写测试套件,但我发现 此页面 讨论了JUnit 5,然后提供了使用JUnit 3的示例。坦率地说,我发现该页面的内容质量很差,不能推荐它。如果您正在使用该页面学习JUnit,我建议您寻找其他资源。

英文:

I think the simplest fix is for you to delete your suite method. You don't need it, and your tests will run quite happily without it. Once you are a bit more confident and familiar with Java and JUnit, then maybe suites will help you organise and group tests, but you can certainly start without them.

The way you are attempting to create a test suite seems to follow the approach of JUnit 3, but you are using JUnit 5. JUnit changed a lot from JUnit 3 and JUnit 4, and also from JUnit 4 to JUnit 5, so it's not surprising that something from JUnit 3 doesn't work with JUnit 5.

I hadn't seen this way of writing test suites before, but I did find that this page talks about JUnit 5 and then presents examples using JUnit 3. To be quite frank I found the content of that page to be of poor quality and cannot recommend it. If you are using that page to learn about JUnit then I would advise you to look elsewhere.

答案2

得分: 0

以下是翻译好的部分:

问题出在以下这些行:

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

你试图将两个不同的类进行强制类型转换:

class junit.framework.TestSuite

class org.junit.jupiter.api.Test

它们是两个不同的类,你可以简单地移除这个函数,错误就会被修复。

或者,如果你想要创建一个 TestSuite 来捆绑一些单元测试用例并一起运行它们,可以放在一个单独的类中,就像在这篇文章中描述的那样。

希望这有所帮助!

英文:

the issue comes from these lines :

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

you are trying to cast 2 different Classes :

class junit.framework.TestSuite

to

class org.junit.jupiter.api.Test

which are 2 different classes , you can simply remove this function and the error will be fixed.

or if you want to have a TestSuite to bundle a few unit test cases and run them together,it can be in a separate class.

like what described in this article

hope this helps !!!

huangapple
  • 本文由 发表于 2023年2月18日 21:03:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定