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

