6

I have a Java based data access layer that interacts with Couchbase. In order to apply unit testing to this layer I would like to mock Couchbase.

Browsing the net I encountered this project which is also hosted in GitHub. I would like to use it but missing some basic examples.

Maybe someone has tried it before and can provide me with some basic usages in Java?

Helder Pereira
  • 5,522
  • 2
  • 35
  • 52
forhas
  • 11,551
  • 21
  • 77
  • 111
  • 1
    That project while officially supported by Couchbase is braid-dead from java prospective for about a year already because of this bug: https://github.com/couchbase/CouchbaseMock/issues/11. If one can't open a bucket the purpose of the whole thing is rather unclear. – eduard.dudar May 19 '16 at 01:27

1 Answers1

3

Personally when testing Couchbase using unit tests I don't use either of those projects, I just use Mockito to mock out the Couchbase calls.

Ideally all your calls to Couchbase are nicely encapsulated into DAO's. Mockito allows me to return what I expect in terms of json payloads etc but at the same time I can simulate timeout and other exceptions.

As a simple example where you are checking what happens if Couchbase throws an exception during an add operation you'd do the following (I expect a runtime exception as I catch the earlier exception and rethrow due to it being non recoverable for this example):

@Test(expected = RuntimeException.class)
public void testSaveUserFailsOnAddDueToTimeout() {
    when(couchbase.incr(anyString(), anyInt())).thenReturn(0L);
    when(couchbase.add(anyString(), anyObject())).thenThrow(InterruptedException.class);
    this.userDao.saveUser(SOURCE);
}

You can view the whole test class here:

https://github.com/scalabilitysolved/couchbase-java/blob/master/src/test/java/com/scalabilitysolved/couchbase/dao/UserDaoTest.java

Or the whole project here (which is a simple Couchbase/Spring/API example)

https://github.com/scalabilitysolved/couchbase-java

scalabilitysolved
  • 2,473
  • 1
  • 26
  • 31
  • Tanks, this is a good approach but with that in mind still I like to know how to use couchbase mock project. – forhas May 15 '14 at 06:23