Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit b3e3c33

Browse files
refactor: migrate all tests to JUnit 5 / AssertJ
This is required for Spring 2.4.0, since it removed JUnit4 support.
1 parent fcd48df commit b3e3c33

File tree

33 files changed

+224
-196
lines changed

33 files changed

+224
-196
lines changed

altair-spring-boot-autoconfigure/src/test/java/graphql/kickstart/altair/boot/test/AbstractAutoConfigurationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package graphql.kickstart.altair.boot.test;
22

3-
import org.junit.After;
3+
import org.junit.jupiter.api.AfterEach;
44
import org.springframework.boot.test.util.TestPropertyValues;
55
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
66
import org.springframework.context.annotation.AnnotationConfigRegistry;
@@ -26,7 +26,7 @@ protected AbstractAutoConfigurationTest(Class<? extends AbstractApplicationConte
2626
this.autoConfiguration = autoConfiguration;
2727
}
2828

29-
@After
29+
@AfterEach
3030
public void tearDown() {
3131
if (this.context != null) {
3232
this.context.close();

altair-spring-boot-autoconfigure/src/test/java/graphql/kickstart/altair/boot/test/AltairControllerTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
import graphql.kickstart.altair.boot.AltairAutoConfiguration;
44
import graphql.kickstart.altair.boot.AltairController;
5-
import org.junit.Assert;
6-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
76
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
87
import org.springframework.context.annotation.Bean;
98
import org.springframework.context.annotation.Configuration;
109
import org.springframework.context.annotation.PropertySource;
1110
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
1211
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
1312

13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
15+
1416
/**
1517
* @author Andrew Potter
1618
*/
@@ -42,13 +44,14 @@ public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderCon
4244
public void altairLoads() {
4345
load(EnabledConfiguration.class);
4446

45-
Assert.assertNotNull(this.getContext().getBean(AltairController.class));
47+
assertThat(this.getContext().getBean(AltairController.class)).isNotNull();
4648
}
4749

48-
@Test(expected = NoSuchBeanDefinitionException.class)
50+
@Test
4951
public void altairDoesNotLoad() {
5052
load(DisabledConfiguration.class);
5153

52-
this.getContext().getBean(AltairController.class);
54+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
55+
.isThrownBy(() -> this.getContext().getBean(AltairController.class));
5356
}
5457
}

example-graphql-tools/src/test/java/com/graphql/sample/boot/GraphQLServletTest.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package com.graphql.sample.boot
33

44
import com.graphql.spring.boot.test.GraphQLTestTemplate
55
import com.graphql.spring.boot.test.GraphQLTest
6-
import org.junit.Assert.assertEquals
7-
import org.junit.Assert.assertNotNull
8-
import org.junit.Test
9-
import org.junit.runner.RunWith
6+
import org.assertj.core.api.Assertions.assertThat
7+
import org.junit.jupiter.api.Test
8+
import org.junit.jupiter.api.extension.ExtendWith
109
import org.springframework.beans.factory.annotation.Autowired
1110
import org.springframework.http.HttpStatus
12-
import org.springframework.test.context.junit4.SpringRunner
11+
import org.springframework.test.context.junit.jupiter.SpringExtension
1312

14-
@RunWith(SpringRunner::class)
13+
@ExtendWith(SpringExtension::class)
1514
@GraphQLTest
1615
class GraphQLServletTest {
1716

@@ -21,8 +20,8 @@ class GraphQLServletTest {
2120
@Test
2221
fun `query over HTTP POST multipart with variables returns data requires multipartconfig`() {
2322
val response = graphQLTestTemplate.postMultipart("query echo(\$string: String!)", """{"string":"echo"}""")
24-
assertNotNull(response)
25-
assertEquals(HttpStatus.OK, response.statusCode)
23+
assertThat(response).isNotNull
24+
assertThat(response.statusCode).isEqualTo(HttpStatus.OK)
2625
}
2726

2827
}

example-graphql-tools/src/test/java/com/graphql/sample/boot/GraphQLToolsSampleApplicationTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,35 @@
55
import com.graphql.spring.boot.test.GraphQLResponse;
66
import com.graphql.spring.boot.test.GraphQLTestTemplate;
77
import com.graphql.spring.boot.test.GraphQLTest;
8-
import org.junit.Ignore;
9-
import org.junit.Test;
10-
import org.junit.runner.RunWith;
8+
import org.junit.jupiter.api.Disabled;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
1111
import org.springframework.beans.factory.annotation.Autowired;
12-
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.test.context.junit.jupiter.SpringExtension;
1314

1415
import java.io.IOException;
1516

1617
import static graphql.Assert.assertNotNull;
17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertTrue;
18+
import static org.assertj.core.api.Assertions.assertThat;
1919

2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23-
@RunWith(SpringRunner.class)
23+
@ExtendWith(SpringExtension.class)
2424
@GraphQLTest
2525
public class GraphQLToolsSampleApplicationTest {
2626

2727
@Autowired
2828
private GraphQLTestTemplate graphQLTestTemplate;
2929

3030
@Test
31-
@Ignore
31+
@Disabled
3232
public void get_comments() throws IOException {
3333
GraphQLResponse response = graphQLTestTemplate.postForResource("graphql/post-get-comments.graphql");
3434
assertNotNull(response);
35-
assertTrue(response.isOk());
36-
assertEquals("1", response.get("$.data.post.id"));
35+
assertThat(response.isOk()).isTrue();
36+
assertThat(response.get("$.data.post.id")).isEqualTo("1");
3737
}
3838

3939
@Test
@@ -42,17 +42,17 @@ public void get_comments_withFragments() throws IOException {
4242
fragments.add("graphql/all-comment-fields-fragment.graphql");
4343
GraphQLResponse response = graphQLTestTemplate.postForResource("graphql/post-get-comments-with-fragment.graphql", fragments);
4444
assertNotNull(response);
45-
assertTrue(response.isOk());
46-
assertEquals("1", response.get("$.data.post.id"));
45+
assertThat((response.isOk())).isTrue();
46+
assertThat(response.get("$.data.post.id")).isEqualTo("1");
4747
}
4848

4949
@Test
5050
public void create_post() throws IOException {
5151
ObjectNode variables = new ObjectMapper().createObjectNode();
5252
variables.put("text", "lorem ipsum dolor sit amet");
5353
GraphQLResponse response = graphQLTestTemplate.perform("graphql/create-post.graphql", variables);
54-
assertNotNull(response);
55-
assertNotNull(response.get("$.data.createPost.id"));
54+
assertThat(response).isNotNull();
55+
assertThat(response.get("$.data.createPost.id")).isNotNull();
5656
}
5757

5858
}

example-graphql-tools/src/test/java/com/graphql/sample/boot/SpringBootTestWithoutWebEnvironmentTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.graphql.sample.boot;
22

3-
import org.junit.Ignore;
4-
import org.junit.Test;
5-
import org.junit.runner.RunWith;
3+
import org.junit.jupiter.api.Disabled;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.ExtendWith;
66
import org.springframework.boot.test.context.SpringBootTest;
7-
import org.springframework.test.context.junit4.SpringRunner;
7+
import org.springframework.test.context.junit.jupiter.SpringExtension;
88

9-
@RunWith(SpringRunner.class)
9+
@ExtendWith(SpringExtension.class)
1010
@SpringBootTest
11+
@Disabled
1112
public class SpringBootTestWithoutWebEnvironmentTest {
1213

1314
@Test
14-
@Ignore
1515
public void loads_without_complaining_about_missing_ServerContainer() {
1616

1717
}

example-request-scoped-dataloader/src/test/java/graphql/servlet/examples/dataloader/requestscope/ApplicationTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package graphql.servlet.examples.dataloader.requestscope;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4-
import org.junit.Test;
5-
import org.junit.runner.RunWith;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.ExtendWith;
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.boot.test.context.SpringBootTest;
88
import org.springframework.boot.test.web.client.TestRestTemplate;
99
import org.springframework.http.HttpEntity;
1010
import org.springframework.http.HttpHeaders;
1111
import org.springframework.http.ResponseEntity;
12-
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.test.context.junit.jupiter.SpringExtension;
1313

14-
import static org.junit.Assert.assertEquals;
15-
import static org.junit.Assert.assertNotEquals;
14+
import static org.assertj.core.api.Assertions.assertThat;
1615

17-
@RunWith(SpringRunner.class)
16+
@ExtendWith(SpringExtension.class)
1817
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
1918
public class ApplicationTest {
2019

@@ -42,14 +41,13 @@ public void testRequestScope() {
4241
headers.add("content-type", "application/graphql");
4342
ResponseEntity<JsonNode> response = this.restTemplate.postForEntity("/graphql", new HttpEntity<>(requestGraphQL, headers), JsonNode.class);
4443

45-
assertEquals(response.getBody().toString(), "{\"data\":{\"walmartCustomers\":[{\"customerId\":101,\"name\":\"Customer Name 1\"},{\"customerId\":102,\"name\":\"Customer Name 2\"},{\"customerId\":103,\"name\":\"Customer Name 3\"},{\"customerId\":104,\"name\":\"Customer Name 4\"}]}}");
44+
assertThat(response.getBody().toString()).isEqualTo("{\"data\":{\"walmartCustomers\":[{\"customerId\":101,\"name\":\"Customer Name 1\"},{\"customerId\":102,\"name\":\"Customer Name 2\"},{\"customerId\":103,\"name\":\"Customer Name 3\"},{\"customerId\":104,\"name\":\"Customer Name 4\"}]}}");
4645

4746
repository.updateUsernameForId(101, "New Name 1");
4847

4948
response = this.restTemplate.postForEntity("/graphql", new HttpEntity<>(requestGraphQL, headers), JsonNode.class);
5049

51-
assertEquals(response.getBody().toString(), "{\"data\":{\"walmartCustomers\":[{\"customerId\":101,\"name\":\"New Name 1\"},{\"customerId\":102,\"name\":\"Customer Name 2\"},{\"customerId\":103,\"name\":\"Customer Name 3\"},{\"customerId\":104,\"name\":\"Customer Name 4\"}]}}");
52-
;
50+
assertThat(response.getBody().toString()).isEqualTo("{\"data\":{\"walmartCustomers\":[{\"customerId\":101,\"name\":\"New Name 1\"},{\"customerId\":102,\"name\":\"Customer Name 2\"},{\"customerId\":103,\"name\":\"Customer Name 3\"},{\"customerId\":104,\"name\":\"Customer Name 4\"}]}}");
5351
}
5452

5553

graphiql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/graphiql/boot/ReactiveGraphiQLControllerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package graphql.kickstart.graphiql.boot;
22

3-
import org.junit.Test;
4-
import org.junit.runner.RunWith;
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.boot.SpringBootConfiguration;
77
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
88
import org.springframework.context.annotation.Import;
99
import org.springframework.http.MediaType;
1010
import org.springframework.test.context.TestPropertySource;
11-
import org.springframework.test.context.junit4.SpringRunner;
11+
import org.springframework.test.context.junit.jupiter.SpringExtension;
1212
import org.springframework.test.web.reactive.server.WebTestClient;
1313

14-
@RunWith(SpringRunner.class)
14+
@ExtendWith(SpringExtension.class)
1515
@WebFluxTest
1616
public class ReactiveGraphiQLControllerTest {
1717

graphiql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/graphiql/boot/ServletGraphiQLControllerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package graphql.kickstart.graphiql.boot;
22

3-
import org.junit.Test;
4-
import org.junit.runner.RunWith;
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.boot.SpringBootConfiguration;
77
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
88
import org.springframework.context.annotation.Import;
99
import org.springframework.test.context.TestPropertySource;
10-
import org.springframework.test.context.junit4.SpringRunner;
10+
import org.springframework.test.context.junit.jupiter.SpringExtension;
1111
import org.springframework.test.web.servlet.MockMvc;
1212

1313
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1414
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
1515
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1616

17-
@RunWith(SpringRunner.class)
17+
@ExtendWith(SpringExtension.class)
1818
@WebMvcTest
1919
public class ServletGraphiQLControllerTest {
2020

graphiql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/graphiql/boot/test/AbstractAutoConfigurationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package graphql.kickstart.graphiql.boot.test;
22

3-
import org.junit.After;
3+
import org.junit.jupiter.api.AfterEach;
44
import org.springframework.boot.test.util.TestPropertyValues;
55
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
66
import org.springframework.context.annotation.AnnotationConfigRegistry;
@@ -26,7 +26,7 @@ protected AbstractAutoConfigurationTest(Class<? extends AbstractApplicationConte
2626
this.autoConfiguration = autoConfiguration;
2727
}
2828

29-
@After
29+
@AfterEach
3030
public void tearDown() {
3131
if (this.context != null) {
3232
this.context.close();

graphiql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/graphiql/boot/test/GraphiQLControllerTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
import graphql.kickstart.graphiql.boot.GraphiQLAutoConfiguration;
44
import graphql.kickstart.graphiql.boot.GraphiQLController;
5-
import org.junit.Assert;
6-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
76
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
87
import org.springframework.context.annotation.Bean;
98
import org.springframework.context.annotation.Configuration;
109
import org.springframework.context.annotation.PropertySource;
1110
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
1211
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
1312

13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
15+
1416
/**
1517
* @author Andrew Potter
1618
*/
@@ -42,13 +44,14 @@ public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderCon
4244
public void graphiqlLoads() {
4345
load(EnabledConfiguration.class);
4446

45-
Assert.assertNotNull(this.getContext().getBean(GraphiQLController.class));
47+
assertThat(this.getContext().getBean(GraphiQLController.class)).isNotNull();
4648
}
4749

48-
@Test(expected = NoSuchBeanDefinitionException.class)
50+
@Test
4951
public void graphiqlDoesNotLoad() {
5052
load(DisabledConfiguration.class);
5153

52-
this.getContext().getBean(GraphiQLController.class);
54+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
55+
.isThrownBy(() -> this.getContext().getBean(GraphiQLController.class));
5356
}
5457
}

0 commit comments

Comments
 (0)