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

Eclipse调试Java执行step into/over报Junit4容器不允许修改源码附件错误

M1芯片Mac上Eclipse调试JUnit代码触发源码附件报错问题

问题描述

使用M1芯片版MacBook Air运行Eclipse调试Java代码时,执行step into或step over操作会弹出报错:

The jar of this class file belongs to container 'Junit4' which does not allow modifications too source attachments of it's entries.
代码本身可正常运行,触发报错的时机为调试到assertEquals(u1, u2)这一行执行单步操作时。已多次尝试重装Eclipse未解决,同配置M1芯片MacBook Air可复现该问题。

相关测试代码

@Test
public void test_unit_03() {
    /*
     * Create three new units with their intended functions and dimensions. 
     */
    Unit u1 = new Unit("Master Bedroom", 14, 9);
    Unit u2 = new Unit("Master Bedroom", 18, 7);
    Unit u3 = new Unit("Master Bedroom", 18, 8);
    Unit u4 = new Unit("Office", 18, 7); 
    
    assertEquals("A unit of 126 square feet (14' wide and 9' long) functioning as Master Bedroom", u1.toString());
    assertEquals("A unit of 126 square feet (18' wide and 7' long) functioning as Master Bedroom", u2.toString());
    assertEquals("A unit of 144 square feet (18' wide and 8' long) functioning as Master Bedroom", u3.toString());
    assertEquals("A unit of 126 square feet (18' wide and 7' long) functioning as Office", u4.toString());
    
    /*
     * Two units are considered equal if their intended functions are the same (case-sensitive)
     *  and the areas (in square feet) are the same (even if the dimensions may be different). 
     */
    assertEquals(u1, u2);
//  assertTrue(u1.equals(u2));
    /* Note. Result of assertNotEquals is just the opposite to assertEquals. */
    assertNotEquals(u1, u3);
    assertNotEquals(u1, u4);
    assertNotEquals(u2, u3);
    assertNotEquals(u2, u4);
    
    /* 
     * The above assertions do not cover all cases of the equals method as discussed in the lecture.
     * Your implementation of the overridden equals method should cover them.    
     */
}

解决方案

该问题本质是Eclipse默认的JUnit4容器限制了源码绑定配置的修改权限,你在调试时单步进入了JUnit的assertEquals方法,Eclipse尝试自动为JUnit jar包绑定源码失败才触发报错,和M1芯片适配无关,按以下步骤操作即可解决:

  • 配置调试过滤规则(推荐,日常调试业务代码不需要进入JUnit框架逻辑)
    1. 打开Eclipse偏好设置(Mac上按command + , 唤起)
    2. 依次进入Java > Debug > Step Filtering
    3. 勾选Use Step Filters、Filter synthetic methods、Filter static initializers、Filter constructors选项
    4. 点击Add Filter,依次输入org.junit.*和junit.*,添加完成后勾选这两个新增的过滤规则
    5. 保存设置后重启调试,单步操作时会自动跳过JUnit框架的类方法,不会再触发报错
  • 若确实需要查看JUnit源码调试,可手动配置JUnit源码包:
    1. 找到项目依赖列表里的JUnit4容器,右键选择Properties
    2. 在条目列表里选中JUnit 4.jar,点击右侧的Edit按钮
    3. 在弹出的窗口中选择External File,导入本地提前下载好的对应版本JUnit4源码jar包,确认保存即可
      后续调试进入JUnit方法时会自动加载绑定的源码,不会再触发报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 17:15:06