我正在尝试使用Junit
在Spring Boot
进行Mockito
单元测试。
但是我收到以下错误。 我无法弄清楚错误出了什么问题 Codesnippet: https://pastebin.com/RDUXk5QF
public List<JobPartRecentActivity> jobPartRecentActivityList() {
Date timestamp = new Date();
JobPartRecentActivity jobData = new JobPartRecentActivity();
jobData.setCommments("TestComments");
jobData.setEventId(1234L);
jobData.setJobNumber(1234L);
jobData.setTagNumber(1L);
jobData.setSerialNumber("6789");
jobData.setPartNumber("12345");
jobData.setOperationName("Receiving");
jobData.setOperationSequence(1L);
jobData.setOperationCode("02");
jobData.setPartStatus("COMPLETE");
jobData.setTimestamp(timestamp);
jobData.setSystemActionTaken("Done");
List<JobPartRecentActivity> dataList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
dataList.add(jobData);
}
return dataList;
}
@Test
public void testremoveDuplicateEntries() throws Exception {
//setup
List<JobPartRecentActivity> actualdataList = new ArrayList<>();
Date timestamp = new Date();
JobPartRecentActivity jobDataActual = new JobPartRecentActivity();
jobDataActual.setCommments("TestComments");
jobDataActual.setEventId(1234L);
jobDataActual.setJobNumber(1234L);
jobDataActual.setTagNumber(1L);
jobDataActual.setSerialNumber("6789");
jobDataActual.setPartNumber("12345");
jobDataActual.setOperationName("Receiving");
jobDataActual.setOperationSequence(1L);
jobDataActual.setOperationCode("02");
jobDataActual.setPartStatus("COMPLETE");
jobDataActual.setTimestamp(timestamp);
jobDataActual.setSystemActionTaken("Done");
actualdataList.add(jobDataActual);
//execute
List<JobPartRecentActivity> expecteddataList = new ArrayList<>();
expecteddataList = eventEngine.removeDuplicateEntries(jobPartRecentActivityList());
//verify
assertThat(actualdataList, is(expecteddataList));
}
}
-
错误: java.lang.AssertionError: 预期:是&lt; [JobPartRecentActivity [rnum = null,eventId = 1234,jobNumber = 1234,tagNumber = 1,serialNumber = 6789,partNumber = 12345, operationSequence = 1,operationName =正在接收,operationCode = 02, partStatus = COMPLETE,comments = TestComments,changeMadeBy = null, timestamp = Thu Nov 09 16:11:42 CST 2017,systemActionTaken = Done]]&gt; 但是:是&lt; [JobPartRecentActivity [rnum = null,eventId = 1234,jobNumber = 1234,tagNumber = 1,serialNumber = 6789,partNumber = 12345, operationSequence = 1,operationName =正在接收,operationCode = 02, partStatus = COMPLETE,comments = TestComments,changeMadeBy = null, timestamp = Thu Nov 09 16:11:42 CST 2017,systemActionTaken = Done]]&gt; 在org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20) 在org.junit.Assert.assertThat(Assert.java:956) 在org.junit.Assert.assertThat(Assert.java:923) 在com.ge.power.brs.engines.impl.EventEngineTests.testremoveDuplicateEntries(EventEngineTests.java:172) 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) 在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268) 在org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) 在org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) 在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
答案 0 :(得分:1)
您注意将equals
和hashcode
添加到JobPartRecentActivity
,因为断言(...,是(...));如果对象没有equals
和hashcode
,则按引用进行比较。
答案 1 :(得分:0)
问题在于日期。它正在比较时间戳。当创建expecteddatalist对象并创建了realdatalist对象时,确实存在时差。一旦我们删除了Date代码,断言就可以了。