javaspringjunitcouchbasespring-data-couchbase

Spring junit tests with couchbase


I have some Services that should get documents from 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. 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%. And another moment, I use maven, and when it runs tests of project it always fail...

Can anybody advise something how write test for for working with couchbase.


Solution

  • Problem resolved. Reason was that after document added to couchbase view was not refresh. So just needed to add

    query.setStale( Stale.FALSE );
    

    View will be refresh before receiving data.