-
Notifications
You must be signed in to change notification settings - Fork 52
/
CommentRepositoryTest.java
63 lines (53 loc) · 2.05 KB
/
CommentRepositoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.example.demo;
import com.example.demo.domain.Comment;
import com.example.demo.domain.PostId;
import com.example.demo.repository.CommentRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import reactor.test.StepVerifier;
import static org.assertj.core.api.Assertions.assertThat;
@DataMongoTest
@Slf4j
@Testcontainers
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
public class CommentRepositoryTest {
@Container
static MongoDBContainer mongoDBContainer = new MongoDBContainer();
@Autowired
CommentRepository comments;
@DynamicPropertySource
static void neo4jProperties(DynamicPropertyRegistry registry) {
registry.add("spring.data.mongodb.uri", () -> mongoDBContainer.getReplicaSetUrl());
}
@BeforeEach
public void setup() {
this.comments.deleteAll().then()
.then(
this.comments.save(Comment.builder().content("test").post(new PostId("post-id")).build())
)
.block();
}
@Test
public void testFindByPostId() {
this.comments.findByPost(new PostId("post-id"))
.as(StepVerifier::create)
.consumeNextWith(c -> assertThat(c.getContent()).isEqualTo("test"))
.verifyComplete();
}
@Test
public void testAddComment() {
this.comments.countByPost(new PostId("post-id"))
.as(StepVerifier::create)
.consumeNextWith(c -> assertThat(c.longValue()).isEqualTo(1L))
.verifyComplete();
}
}