2

我有一些服务应该从 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 编写测试。

4

1 回答 1

0

问题已解决。原因是文档添加到 couchbase 视图后没有刷新。所以只需要添加

query.setStale( Stale.FALSE );

视图将在接收数据之前刷新。

于 2014-12-24T06:10:10.460 回答