简体   繁体   English

使用 couchbase 进行 Spring Junit 测试

[英]Spring junit tests with couchbase

I have some Services that should get documents from couchbase.我有一些服务应该从 couchbase 获取文件。

Services:服务:

    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;
    }

Also I have DocRepository interface with necessary methods and views on couchbase server.我也有 DocRepository 接口,在 couchbase 服务器上有必要的方法和视图。 When I run my app and call Services its work fine, but I need tests for this Services.当我运行我的应用程序并调用服务时,它工作正常,但我需要对此服务进行测试。

Tests:测试:

@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() );
    }

Running tests successful only in 90%.运行测试成功率只有 90%。 And another moment, I use maven, and when it runs tests of project it always fail...还有一刻,我使用 maven,当它运行项目测试时,它总是失败......

Can anybody advise something how write test for for working with couchbase.任何人都可以建议如何为使用 couchbase 编写测试。

Problem resolved.问题已解决。 Reason was that after document added to couchbase view was not refresh.原因是文档添加到 couchbase 视图后没有刷新。 So just needed to add所以只需要添加

query.setStale( Stale.FALSE );

View will be refresh before receiving data.视图将在接收数据之前刷新。

query.setStale(Stale.FALSE) query.setStale(Stale.FALSE)

This will refresh the view, and stale data will not be there这将刷新视图,并且不存在陈旧的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM