Skip to content

Commit

Permalink
Feat: Test code for GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
bohub12 committed Sep 11, 2023
1 parent e3dca55 commit 853cfbf
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 6 deletions.
41 changes: 41 additions & 0 deletions spring-for-graphql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@


```graphql
mutation {
saveMember(body: {
name: "멤버"
age: 15
role: ADMIN
}) {
id
age
name
role
}
}

query {
getMember(id:1) {
id
name
age
role
}
}

query {
getMemberList {
id
name
age
role
}
}
```


# _Reference_
- [GraphQL official documentation](https://graphql.org/learn)
- [GraphQL official documentation - schema](https://graphql.org/learn/schema/)
- [Baeldung Blog - Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql)
- [tech blog1](https://danawalab.github.io/spring/2022/06/06/Spring-for-GraphQL.html)
5 changes: 5 additions & 0 deletions spring-for-graphql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ dependencies {
// graph ql - supported various scalars
implementation 'com.graphql-java:graphql-java-extended-scalars:21.0'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.projectlombok:lombok'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'

annotationProcessor 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework:spring-webflux'
testImplementation 'org.springframework.graphql:spring-graphql-test'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.demo;

import com.example.demo.domain.Member;
import com.example.demo.repository.MemberRepository;
import com.example.demo.domain.MemberRole;
import com.example.demo.repository.MemberRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -18,11 +18,11 @@ public static void main(String[] args) {
@Bean
public CommandLineRunner runner(MemberRepository repository) {
return args -> {
repository.save(new Member(1L, "member1", MemberRole.NORMAL, 10));
repository.save(new Member(2L, "member2", MemberRole.NORMAL, 20));
repository.save(new Member(3L, "member3", MemberRole.NORMAL, 30));
repository.save(new Member(4L, "member4", MemberRole.ADMIN, 40));
repository.save(new Member(5L, "member5", MemberRole.ADMIN, 50));
repository.save(new Member("member1", MemberRole.NORMAL, 10));
repository.save(new Member("member2", MemberRole.NORMAL, 20));
repository.save(new Member("member3", MemberRole.NORMAL, 30));
repository.save(new Member("member4", MemberRole.ADMIN, 40));
repository.save(new Member("member5", MemberRole.ADMIN, 50));
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.example.demo.controller;

import com.example.demo.config.GraphQlConfig;
import com.example.demo.domain.Member;
import com.example.demo.domain.MemberRole;
import com.example.demo.repository.MemberRepository;
import lombok.SneakyThrows;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.graphql.tester.AutoConfigureGraphQlTester;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.graphql.test.tester.GraphQlTester;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;


@SpringBootTest
@AutoConfigureGraphQlTester
@Import(GraphQlConfig.class)
class MemberGraphQLControllerTest {

@Autowired
private GraphQlTester graphQlTester;

@Autowired
private MemberRepository repository;


@BeforeEach
void addDummyData() {
repository.save(new Member("member1", MemberRole.NORMAL, 10));
repository.save(new Member("member2", MemberRole.NORMAL, 20));
repository.save(new Member("member3", MemberRole.NORMAL, 30));
repository.save(new Member("member4", MemberRole.ADMIN, 40));
repository.save(new Member("member5", MemberRole.ADMIN, 50));
}

@AfterEach
void clear() {
repository.deleteAll();
}


@Test
void getMember_thenReturnResponse1() {
long id = repository.findAll().stream().findFirst().orElseThrow().getId();

String documentName = "getMember";

graphQlTester.documentName(documentName)
.variable("id", id)
.execute()
.path("$")
.matchesJson(expected(documentName));
}

@Test
void getMember_thenReturnResponse2() {
long id = repository.findAll().stream().findFirst().orElseThrow().getId();

String query = String.format("{ getMember(id: %d) { id name age role }}", id);
Member member = graphQlTester.document(query)
.execute()
.path("data.getMember")
.entity(Member.class)
.get();

assertThat(member).isNotNull();
assertThat(member.getAge()).isEqualTo(10);
}


@Test
void getMemberList_thenReturnResponse1() {
String documentName = "getMemberList";

List<Member> memberList = graphQlTester.documentName(documentName)
.execute()
.path("data.getMemberList[*]")
.entityList(Member.class)
.get();

assertThat(memberList.size()).isEqualTo(5);
}

@Test
void saveMember_thenReturnResponse1() {
String name = "멤버";
MemberRole role = MemberRole.ADMIN;
int age = 15;

HashMap<String, Object> map = new HashMap<>();
map.put("name", name);
map.put("role", role);
map.put("age", age);

String documentName = "saveMember";

graphQlTester.documentName(documentName)
.variable("body", map)
.execute()
.path("$")
.matchesJson(expected(documentName));
}

@SneakyThrows
public static String expected(String fileName) {
Path path = Paths.get("src/test/resources/graphql-response/" + fileName + "ExpectedResponse.json");
return new String(Files.readAllBytes(path));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"data": {
"getMember": {
"name": "member1",
"age": 10,
"role": "NORMAL"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"data": {
"getMemberList": [
{
"name": "member1",
"age": 10,
"role": "NORMAL"
},
{
"name": "member2",
"age": 20,
"role": "NORMAL"
},
{
"name": "member3",
"age": 30,
"role": "NORMAL"
},
{
"name": "member4",
"age": 40,
"role": "ADMIN"
},
{
"name": "member5",
"age": 50,
"role": "ADMIN"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"data": {
"saveMember": {
"age": 15,
"name": "멤버",
"role": "ADMIN"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query getMember ($id: Long!) {
getMember(id: $id) {
name
age
role
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query getMemberList {
getMemberList {
name
age
role
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mutation saveMember ($body: CreateMemberRequestDto!) {
saveMember(body: $body) {
age
name
role
}
}

0 comments on commit 853cfbf

Please sign in to comment.