eclipse 环境 JUnit 测试框架(junit.framework.* 与 org.junit.*)

如下所示,先通过 build path 导入 junit 环境依赖的 jar 包:


这里写图片描述

1. junit.framework.*

  • junit.framework.* 主要类和函数:
    • Test
    • TestCase
    • TestSuite

实现并运行(run as => Java Application,因其有 java 应用所需的入口函数:main 函数)如下的代码:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

public class FileReaderTester extends TestCase
{
    private FileReader input = null;

    public FileReaderTester(String name)
    {
        super(name);
    }

    protected void setUp()
    {
        try
        {
            input = new FileReader("data.txt");
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
    }

    public void testRead() throws IOException
    {
        char ch = '&';
        for (int i = 0; i < 4; ++i)
        {
            ch = (char)input.read();
        }
        assertEquals('d', ch);
    }

    protected void tearDown()
    {
        try
        {
            input.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public static Test suite()
    {
        TestSuite suite = new TestSuite();
        suite.addTest(new FileReaderTester("testRead"));
        return suite;
    }

    public static void main(String[] args)
    {
        TestRunner.run(suite());
    }

}

2. org.junit.*

  • 待测试的功能类的工作,不依赖任何相关的测试类;
    • 可以独立运行;
    • 测试类的对象则是待测试的功能类;
    • 测试类构造的用例是为了保证待测试的功能类能够如期望的那样运行;
    • 测试类构造用例的对象是功能类的某一成员函数

这种类库层级形式,一般是通过 eclipse 界面操作完成的:

  • 完成功能类的开发;
  • 右键此待测类:
    • new => JUnit Test Case,选择 setUp, tearDown 等上下文函数;
    • next => 勾选待测类中的待测函数;
  • eclipse 自动生成相关代码;
  • 右键 run as => Junit Test

3. references

org.junit.ComparisonFailure: expected:<456[]> but was:<456[ ]> at org.junit.Assert.assertEquals(Assert.java:115) at org.junit.Assert.assertEquals(Assert.java:144) at Dao.BookDaoTest.testAddBook(BookDaoTest.java:60) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
06-12
<think>我们正在解决Eclipse中Java项目报错"Thetypejunit.framework.TestCaseisnotaccessible"的问题。根据错误信息,问题通常是由于JUnit库未正确配置或版本兼容导致的。以下是逐步解决方案:1.**检查JUnit库是否已添加到构建路径**:-在Eclipse中,右键单击项目->选择"Properties"->选择"JavaBuildPath"->查看"Libraries"选项卡。-确保JUnit库存在于列表中。如果没有,需要添加JUnit库。2.**添加JUnit**:-在"Libraries"选项卡中,点击"AddLibrary..."->选择"JUnit"->点击"Next"。-选择JUnit版本(推荐使用JUnit4或更高版本,因为`junit.framework.TestCase`是JUnit3的类)->点击"Finish"。3.**检查JUnit版本**:-如果项目原本使用JUnit3,但你的测试类尝试导入`junit.framework.TestCase`,请确保你使用的是JUnit3的测试用例结构(即测试类继承`TestCase`)。-注意:在JUnit4中,测试类需要继承`TestCase`,而是使用注解(如`@Test`)。因此,如果项目升级到JUnit4,应修改测试类,再继承`TestCase`。4.**修改测试类(如果使用JUnit4)**:-移除测试类对`junit.framework.TestCase`的继承。-使用JUnit4的注解:```javaimportorg.junit.Test;importstaticorg.junit.Assert.*;publicclassYourTestClass{//再继承TestCase@TestpublicvoidtestMethod(){//测试代码}}```5.**检查模块信息(如果项目是模块化的)**:-如果项目使用Java9及以上版本并定义了`module-info.java`,请确保在模块中导入了JUnit模块。-例如,在`module-info.java`中添加:```javarequiresorg.junit;//根据实际模块名调整,可能是requiresjunit;```-或者,如果JUnit库是以JAR文件形式添加且未模块化,可能需要使用`requirestransitive`或添加必要的导出。6.**清理和重建项目**:-在Eclipse中,选择项目->点击"Project"菜单->"Clean..."->选择当前项目->点击"Clean"。-然后重新构建项目。7.**检查Eclipse的JRE系统库**:-在"JavaBuildPath"的"Libraries"选项卡中,确保JRE系统库存在且正确。如果JRE系统库有错误(如显示红色叉号),则需要重新配置。8.**更新Eclipse和插件**:-确保Eclipse和JDT插件是最新的,以避免已知的兼容性问题。如果以上步骤仍能解决问题,请检查项目是否有其他构建路径问题或依赖冲突。引用说明:在Eclipse中配置测试环境可以参考官方文档[^1],关于构建路径和库的配置可参考相关文档[^2][^3]。§§相关问题§§1.如何在Eclipse中为Java项目添加外部库?2.JUnit3和JUnit4的主要区别是什么?3.如何在Java9及以上版本中配置模块以使用JUnit?4.Eclipse中如何清理和重建项目?</think>### Eclipse中"The type junit.framework.TestCase is not accessible"解决方案 该错误通常由JUnit版本冲突或构建路径配置问题引起,以下是具体解决步骤: #### 1. **检查JUnit版本兼容性** - `junit.framework.TestCase`属于**JUnit 3**,若项目使用JUnit 4/5需调整: ```java // JUnit 3 写法(需继承TestCase) public class MyTest extends TestCase { ... } // JUnit 4/5 写法(使用注解) import org.junit.Test; public class MyTest { @Test public void testMethod() { ... } // 无需继承 } ``` - 若需使用JUnit 3,确保项目添加`junit.jar`(通常位于Eclipse插件目录的`org.junit_*.jar`) #### 2. **配置构建路径** 1. 右键项目 → `Build Path` → `Add Libraries` 2. 选择`JUnit` → 选择版本(**推荐JUnit 4**) ![添加JUnit库示意图](https://example.com/junit-add-lib.png) *若已存在旧版本,先移除`JUnit 3`库[^1]* #### 3. **检查模块化配置(Java 9+)** 若项目有`module-info.java`,添加JUnit模块声明: ```java module your.module { requires org.junit.jupiter.api; // JUnit 5 // 或 requires junit; // JUnit 4 } ``` #### 4. **验证依赖冲突** - 检查`pom.xml`(Maven)或`build.gradle`(Gradle)中的JUnit版本: ```xml <!-- Maven示例 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <!-- 避免3.x版本 --> </dependency> ``` #### 5. **清理缓存** 执行以下操作后重启Eclipse: - `Project` → `Clean...` → 清理当前项目 - 删除`bin`目录下的编译文件 #### 6. **检查访问限制规则** 1. 右键项目 → `Properties` → `Java Compiler` → `Errors/Warnings` 2. 确保`Forbidden reference`设置为`Warning`或`Ignore` > **注意**:若使用Groovy-Eclipse插件,需确保兼容当前Eclipse版本(如Oxygen/Neon等)[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值