我有一些服务应该从couchbase获取文件。
服务:
public List<Doc> findByFirstField( String firstFieldValue )
{
Query query = new Query();
query.setKey( ComplexKey.of( firstFieldValue ) );
List<Doc> docs = (List<Doc>) docRepository.findFirstField( query );
return docs;
}
public List<Doc> findBySecondField( String secondFieldValue )
{
Query query = new Query();
query.setKey( ComplexKey.of( secondFieldValue ) );
List<Doc> docs = (List<Doc>) docRepository.findSecondField( query );
return docs;
}
我还有一个DocRepository接口,在couchbase服务器上有必要的方法和视图。 当我运行我的应用程序并调用服务它的工作正常,但我需要测试此服务。
试验:
@RunWith( SpringJUnit4ClassRunner.class )
@TestExecutionListeners( listeners = { DependencyInjectionTestExecutionListener.class } )
@ContextConfiguration( classes = {CouchbaseConfig.class, DocServiceImplTest.class, DocServiceImpl.class } )
@Configuration
@Before
public void CreateDoc()
throws InterruptedException
{
HashMap<String, String> docInfo = new HashMap<String, String>();
docInfo.put( "docId", "testDoc" );
docInfo.put( "field1", "value1" );
docInfo.put( "field2", "value2" );
docService.saveDoc( docInfo );
}
@After
public void deleteTestsDoc()
{
docService.deleteDoc( "testDoc" );
}
@Test
public void testFindByField1()
{
Doc doc = docService.findByFirstField( "value1" );
assertEquals( "value1", doc.getFirstField() );
}
@Test
public void testFindByField2()
{
Doc doc = docService.findBySecondField( "value2" );
assertEquals( "value2", doc.getSecondField() );
}
仅在90%的情况下运行测试成功。 另一个时刻,我使用maven,当它运行项目测试时总是失败......
任何人都可以建议如何使用couchbase进行写测试。
答案 0 :(得分:0)
问题已解决。 原因是文件添加到couchbase视图后没有刷新。所以只需要添加
query.setStale( Stale.FALSE );
在接收数据之前将刷新视图。