我正在使用Spring Boot 2.1.2.RELEASE,并尝试使用JUnit 5测试存储库。
Couchbase的Spring Boot应用程序配置。
spring:
couchbase:
bootstrap-hosts: localhost
bucket:
name: default
password: password
data:
couchbase:
auto-index: true
我使用Docker compose在Docker中启动Couchbase,目前它运行最新的6.0。
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:
项目中使用的文档。
@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;
}
还有分配库。
@ViewIndexed(designDoc = "assignment", viewName = "all")
public interface AssignmentRepository extends CouchbasePagingAndSortingRepository<Assignment, String> {
List<Assignment> findAll();
}
存储库的测试代码。
@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());
}
}
运行此测试时,它失败。我发现saveAll
的设计无法正常工作,log.debug("saved assignments: {}", this.assignmentRepository.findAll());
打印了一个空列表。
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:::
已更新:我按照测试包中的建议添加了@TestConfiguration
,并将其导入了测试类,很不幸,它仍然无法按预期运行。
@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;
}
}
我创建了一个示例项目来重现我遇到的问题,请从my Github中进行检查。
答案 0 :(得分:1)
默认情况下,Couchbase中的视图最终将保持一致。
您可以通过定义Java配置类来更改默认行为:
@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:您可能会猜到,STRONGLY_CONSISTENT不如默认速度快,因此,如果需要最高性能,建议您仅在测试期间启用STRONGLY_CONSISTENT。
PS2:按键操作始终保持高度一致(findById,保存,更新等)