Reputation: 9261
I am using Spring Boot 2.1.2.RELEASE, and tried to use JUnit 5 to test Repository.
Spring Boot application config for couchbase.
spring:
couchbase:
bootstrap-hosts: localhost
bucket:
name: default
password: password
data:
couchbase:
auto-index: true
I use a Docker compose to start up a Couchbase in Docker, it run the latest 6.0 at the moment.
version: '3.3' # specify docker-compose version
# Define the services/containers to be run
services:
couchbase:
image: couchbase
ports:
- "8091:8091"
- "8092:8092"
- "8093:8093"
- "8094:8094"
- "11210:11210"
volumes:
- couchbasedata:/opt/couchbase/var
volumes:
couchbasedata:
The document used in the project.
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document
@Builder
public class Assignment {
@Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
//private int index;
private String roomNumber;
private String assignee;
@Builder.Default
private Status currentStatus = Status.PENDING;
}
And the Repository for Assignment.
@ViewIndexed(designDoc = "assignment", viewName = "all")
public interface AssignmentRepository extends CouchbasePagingAndSortingRepository<Assignment, String> {
List<Assignment> findAll();
}
And test codes for Repository.
@SpringBootTest
@Slf4j
public class AssignmentRepositoryTest {
@Autowired
private AssignmentRepository assignmentRepository;
@BeforeEach
public void beforeEach() {
log.debug("before each:::");
this.assignmentRepository.deleteAll();
Assignment assignment = Assignment.builder().assignee("Tom").roomNumber("101").build();
Assignment assignment2 = Assignment.builder().assignee("Tom").roomNumber("102").build();
this.assignmentRepository.saveAll(Arrays.asList(assignment, assignment2));
log.debug("saved assignments: {}", this.assignmentRepository.findAll());
}
@AfterEach
public void afterEach() {
log.debug("after each:::");
this.assignmentRepository.deleteAll();
}
@Test
public void testGetAll() {
List<Assignment> assignments = this.assignmentRepository.findAll();
assertEquals(2, assignments.size());
}
}
When I run this test, it fails. I find the saveAll
dese not work as expected, the log.debug("saved assignments: {}", this.assignmentRepository.findAll());
prints an empty List.
2019-02-28 09:01:57.865 DEBUG 16608 --- [ main] c.example.demo.AssignmentRepositoryTest : before each:::
2019-02-28 09:01:58.128 DEBUG 16608 --- [ main] d.c.c.m.e.AbstractCouchbaseEventListener : onBeforeConvert(Assignment(id=null, roomNumber=101, assignee=Tom, currentStatus=PENDING))
2019-02-28 09:01:58.174 DEBUG 16608 --- [ main] c.c.m.e.ValidatingCouchbaseEventListener : Validating object: Assignment(id=null, roomNumber=101, assignee=Tom, currentStatus=PENDING)
2019-02-28 09:01:58.456 DEBUG 16608 --- [ main] d.c.c.m.e.AbstractCouchbaseEventListener : onAfterSave(Assignment(id=fbedcace-c6db-45db-a788-9aa688b0a068, roomNumber=101, assignee=Tom, currentStatus=PENDING), CouchbaseDocument{id=fbedcace-c6db-45db-a788-9aa688b0a068, exp=0, payload={roomNumber=101, currentStatus=PENDING, _class=com.example.demo.domain.Assignment, assignee=Tom}})
2019-02-28 09:01:58.457 DEBUG 16608 --- [ main] d.c.c.m.e.AbstractCouchbaseEventListener : onBeforeConvert(Assignment(id=null, roomNumber=102, assignee=Tom, currentStatus=PENDING))
2019-02-28 09:01:58.457 DEBUG 16608 --- [ main] c.c.m.e.ValidatingCouchbaseEventListener : Validating object: Assignment(id=null, roomNumber=102, assignee=Tom, currentStatus=PENDING)
2019-02-28 09:01:58.461 DEBUG 16608 --- [ main] d.c.c.m.e.AbstractCouchbaseEventListener : onAfterSave(Assignment(id=5cdf11e1-f50f-47f0-acfd-a31f0b4970be, roomNumber=102, assignee=Tom, currentStatus=PENDING), CouchbaseDocument{id=5cdf11e1-f50f-47f0-acfd-a31f0b4970be, exp=0, payload={roomNumber=102, currentStatus=PENDING, _class=com.example.demo.domain.Assignment, assignee=Tom}})
2019-02-28 09:02:04.723 DEBUG 16608 --- [ main] c.example.demo.AssignmentRepositoryTest : saved assignments: []
2019-02-28 09:02:04.790 DEBUG 16608 --- [ main] c.example.demo.AssignmentRepositoryTest : after each:::
Updated: I added @TestConfiguration
as suggested in the test package, and imported it in test class, unluckily it still does not work as expected.
@TestConfiguration
@Slf4j
@Order(0)
public class TestCouchbaseConfig extends AbstractCouchbaseConfiguration {
@Autowired
private Environment env;
@Override
public IndexManager indexManager() {
return new IndexManager(true, true, true);
}
@Override
protected List<String> getBootstrapHosts() {
return Arrays.asList(env.getProperty("spring.couchbase.bootstrap-hosts").split(","));
}
@Override
protected String getBucketName() {
return env.getProperty("spring.couchbase.bucket.name");
}
@Override
protected String getBucketPassword() {
return env.getProperty("spring.couchbase.bucket.password");
}
@Override
protected Consistency getDefaultConsistency() {
return Consistency.STRONGLY_CONSISTENT; //READ_YOUR_OWN_WRITES|UPDATE_AFTER ... etc;
}
}
I have created a sample project to reproduce the problem I encountered, check out it from my Github.
Upvotes: 2
Views: 636
Reputation: 2460
Views are eventually consistent by default in Couchbase.
Here is the doc about it: https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.repository.consistency
You can change the default behavior by defining a Java config class:
@Configuration
public class CouchbaseConfig extends AbstractCouchbaseConfiguration {
@Autowired
private Environment env;
@Override
public IndexManager indexManager() {
return new IndexManager(true, true, true);
}
@Override
protected List<String> getBootstrapHosts() {
return Arrays.asList(env.getProperty("spring.couchbase.bootstrap-hosts").split(","));
}
@Override
protected String getBucketName() {
return env.getProperty("spring.couchbase.bucket.name");
}
@Override
protected String getBucketPassword() {
return env.getProperty("spring.couchbase.bucket.password");
}
@Override
protected Consistency getDefaultConsistency() {
return Consistency.STRONGLY_CONSISTENT//READ_YOUR_OWN_WRITES|UPDATE_AFTER ... etc;
}
}
PS: As you might guess, STRONGLY_CONSISTENT is not as fast as the default one, so if you need max performance, I recommend you to enable STRONGLY_CONSISTENT only during tests.
PS2: Operations by the key are always strong consistent (findById, Save, update, etc)
Upvotes: 1