2015-03-03 26 views
0

问题: 我有服务方法,它以相反的顺序返回对象列表。 它使用MongoTemplate,Query从MongoDB获取对象列表,并使用ListIterator来反转列表。我必须编写使用ListIterator并返回列表的服务层的JUnit测试用例

现在,我必须为上述编写JUnit测试用例。

我在那里我嘲笑mongotemplate

MongoTemplate mongoTemplate = Mockito.mock(MongoTemplate.class); 
    Query query = Mockito.mock(Query.class); 
    CRDetails crDetails = new CRDetails(); 
    CertificateGuidelines certG1 = new CertificateGuidelines("certG1", Constants.CERTIFICATION_TEAM, Constants.DOC_DEVELOPER_STATUS, 1); 
    CertificateGuidelines certG2 = new CertificateGuidelines("certG2", Constants.DOCDEVELOPER, Constants.CERT_TEAM_STATUS, 2); 
    CertificateGuidelines certG3 = new CertificateGuidelines("certG3", Constants.CERTIFICATION_TEAM, Constants.DOC_DEVELOPER_STATUS, 3); 
    List<CertificateGuidelines> newHistoryList = new ArrayList<CertificateGuidelines>(); 
    newHistoryList.add(certG1); 
    newHistoryList.add(certG2); 
    newHistoryList.add(certG3); 
    Mockito.when(mongoTemplate.findOne((org.springframework.data.mongodb.core.query.Query) Matchers.any(Query.class), Matchers.any(Class.class))).thenReturn(crDetails); 

    CRService service = new CRService(); 
    service.setCertMongoTemplate(mongoTemplate); 

assertEquals(historyList, newHistoryList); 

我的服务方法本次代码:

public List<CertificateGuidelines> getHistoryForCertGuidelines(String crNum) { 
Query query = new Query(Criteria.where("cr").is(crNum)); 
CRDetails details = certMongoTemplate.findOne(query, CRDetails.class, 
     Constants.COLLECTION_NAME); 
logger.debug("in history"); 

if (details != null) { 
    logger.debug("Certification Guidelines has " + details.getCertGuidelinesList().size() + "previous updates"); 
} 
List<CertificateGuidelines> historyList = new ArrayList<CertificateGuidelines>(); 

// Add objects to list. 

// Generate an iterator. Start just after the last CG object. 
ListIterator<CertificateGuidelines> listItr = details.getCertGuidelinesList().listIterator(
    details.getCertGuidelinesList().size()); 

// Iterate in reverse. 
while (listItr.hasPrevious()) { 
    historyList.add((CertificateGuidelines) listItr.previous()); 
} 
return historyList; 
    } 

我知道我现在做书面测试情况下,一些错误。不清楚如何处理。请指导。 错误跟踪

java.lang.AssertionError: expected:<historyList> but was:<[[email protected], [email protected]176, [email protected]]> 
    at org.junit.Assert.fail(Assert.java:88) 
    at org.junit.Assert.failNotEquals(Assert.java:743) 
    at org.junit.Assert.assertEquals(Assert.java:118) 
    at org.junit.Assert.assertEquals(Assert.java:144) 
    at com.cerner.docworks.service.test.CRServiceTest.testServiceHistoryDB(CRServiceTest.java:113) 
    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:483) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) 
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

在此先感谢

回答

0

我相信,你正在测试两个列表,平等,当你真正想要做的是检查列表包含相同的元素。

最简单的方法是使用Hamcrest,它具有额外的优势,即在出现故障时可以显示更好的信息。

Assert.assertThat(historyList,IsIterableContainingOrder.contains(newHistoryList.toArray())); 
+0

感谢您的回复Robert。 – prashanth 2015-03-03 08:49:01

相关问题