JUnit3测试类中assertThat方法未定义问题求助
问题:JUnit3测试中assertThat方法未定义报错,无法满足自动评分要求
问题概述
学习入门Java课程时,单元测试作业因assertThat方法调用报错无法提交。自动评分要求必须使用assertThat,但调用时始终提示:"The method assertThat(Point) is undefined for the type CleverBrainTest",Eclipse对应行显示红色警告,运行测试也触发该错误。Eclipse建议使用assertTrue可正常运行,但自动评分系统仅接受assertThat。
补充背景
- 按课程要求将测试类设置为JUnit3测试
- 已导入指定的导入语句
- CleverBrain实现了Brain接口(一个用于自动玩俄罗斯方块的机器人接口),这是首次为接口编写测试
- 尝试编写
setUp()方法失败,只能在每个测试方法中重复初始化代码 - 使用基于Ubuntu的Linux发行版
测试类CleverBrainTest代码
// ------------------------------------------------------------------------- import student.*; import student.tetris.*; import static org.assertj.core.api.Assertions.*; /** * Tests to measure performance of CleverBrain * * @author My Name * @version Today's Date */ public class CleverBrainTest extends TestCase { //~ Fields ................................................................ //~ Constructors .......................................................... //~Public Methods ........................................................ /** * Tests brain's ability to place sticks in deep holes */ public void testStickInDeepHole() { Board deepHole = new Board(10, 24, "######### ", "######### ", "######### ", "######### " ); Brain myBrain = new CleverBrain(); Piece stick = Piece.getPiece(Piece.STICK, 0); Move best = myBrain.bestMove(deepHole, stick, 20); // Now call the brain best = myBrain.bestMove(deepHole, stick, 20); // Expect the piece will go to the bottom right hole this.assertThat(best.getLocation()).isEqualTo(new Point(10, 0)); // Expect the piece is rotated counter-clockwise 0 times this.assertThat(best.getPiece()).isEqualTo(Piece.SQUARE, 0); // make sure it's correct } /** * Tests Brain's ability to choose a reasonable target */ public void testSetTarget() { Board squareHole = new Board(10, 24, " ", "##### ###", "##### ###", "##########" ); Brain myBrain = new CleverBrain(); Piece square = Piece.getPiece(Piece.SQUARE, 0); Move best = myBrain.bestMove(squareHole, square, 20); // Now call the brain best = myBrain.bestMove(squareHole, square, 20); // Expect the lower left destination is where the hole is System.out.println(best.getLocation()); this.assertThat(best.getLocation()).isEqualTo(new Point(5, 1)); } }
错误堆栈信息
java.lang.Error: Unresolved compilation problem: The method assertThat(Point) is undefined for the type CleverBrainTest at CleverBrainTest.testSetTarget(CleverBrainTest.java:66) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at junit.framework.TestCase.runTest(TestCase.java:176) at junit.framework.TestCase.runBare(TestCase.java:141) at junit.framework.TestResult$1.protect(TestResult.java:122) at junit.framework.TestResult.runProtected(TestResult.java:142) at junit.framework.TestResult.run(TestResult.java:125) at junit.framework.TestCase.run(TestCase.java:129) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
已尝试的解决方法
- 将assertj-core-3.19.0.jar添加到项目构建路径,但错误依旧
- 尝试切换到JUnit4,未成功,且课程要求使用JUnit3
内容的提问来源于stack exchange,提问作者Pellet Muncher
相关产品推荐
相关产品推荐

