Skip to content

Commit 02a8bc5

Browse files
committed
chore: more readable tests
1 parent 43efad0 commit 02a8bc5

9 files changed

+69
-84
lines changed

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocApp1Test.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,33 @@
1919
package test.org.springdoc.ui.app1;
2020

2121
import org.junit.jupiter.api.Test;
22-
import test.org.springdoc.ui.AbstractSpringDocTest;
23-
2422
import org.springframework.boot.autoconfigure.SpringBootApplication;
25-
import org.springframework.test.web.servlet.MvcResult;
23+
import test.org.springdoc.ui.AbstractSpringDocTest;
2624

27-
import static org.junit.jupiter.api.Assertions.assertEquals;
28-
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
import static org.hamcrest.Matchers.containsString;
2926
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3028
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3129

3230

3331
public class SpringDocApp1Test extends AbstractSpringDocTest {
3432

35-
@Test
36-
public void shouldDisplaySwaggerUiPage() throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui/index.html")).andExpect(status().isOk()).andReturn();
38-
String contentAsString = mvcResult.getResponse().getContentAsString();
39-
assertTrue(contentAsString.contains("Swagger UI"));
40-
}
41-
42-
@Test
43-
public void originalIndex() throws Exception {
44-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui/index.html")).andExpect(status().isOk()).andReturn();
45-
String transformedIndex = mvcResult.getResponse().getContentAsString();
46-
assertTrue(transformedIndex.contains("Swagger UI"));
47-
assertEquals(getExpectedResult(), transformedIndex);
48-
}
49-
50-
@SpringBootApplication
51-
static class SpringDocTestApp {}
33+
@Test
34+
public void shouldDisplaySwaggerUiPage() throws Exception {
35+
mockMvc.perform(get("/swagger-ui/index.html"))
36+
.andExpect(status().isOk())
37+
.andExpect(content().string(containsString("Swagger UI")));
38+
}
39+
40+
@Test
41+
public void originalIndex() throws Exception {
42+
mockMvc.perform(get("/swagger-ui/index.html"))
43+
.andExpect(status().isOk())
44+
.andExpect(content().string(containsString("Swagger UI")))
45+
.andExpect(content().string(getExpectedResult()));
46+
}
47+
48+
@SpringBootApplication
49+
static class SpringDocTestApp {
50+
}
5251
}

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocConfigPathsTest.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,32 @@
1919
package test.org.springdoc.ui.app1;
2020

2121
import org.junit.jupiter.api.Test;
22-
import test.org.springdoc.ui.AbstractSpringDocTest;
23-
2422
import org.springframework.boot.autoconfigure.SpringBootApplication;
2523
import org.springframework.test.context.TestPropertySource;
26-
import org.springframework.test.web.servlet.MvcResult;
24+
import test.org.springdoc.ui.AbstractSpringDocTest;
2725

28-
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
import static org.hamcrest.Matchers.containsString;
2927
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3029
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3130

3231
@TestPropertySource(properties = {
33-
"springdoc.swagger-ui.path=/test/swagger.html",
34-
"server.servlet.context-path=/context-path",
35-
"spring.mvc.servlet.path=/servlet-path"
32+
"springdoc.swagger-ui.path=/test/swagger.html",
33+
"server.servlet.context-path=/context-path",
34+
"spring.mvc.servlet.path=/servlet-path"
3635
})
3736
public class SpringDocConfigPathsTest extends AbstractSpringDocTest {
3837

39-
@Test
40-
public void should_display_swaggerui_page() throws Exception {
41-
mockMvc.perform(get("/context-path/servlet-path/test/swagger.html").contextPath("/context-path").servletPath("/servlet-path")).andExpect(status().isFound()).andReturn();
42-
MvcResult mvcResult = mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/index.html").contextPath("/context-path").servletPath("/servlet-path")).andExpect(status().isOk()).andReturn();
43-
String contentAsString = mvcResult.getResponse().getContentAsString();
44-
assertTrue(contentAsString.contains("Swagger UI"));
45-
}
38+
@Test
39+
public void should_display_swaggerui_page() throws Exception {
40+
mockMvc.perform(get("/context-path/servlet-path/test/swagger.html").contextPath("/context-path").servletPath("/servlet-path"))
41+
.andExpect(status().isFound());
42+
mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/index.html").contextPath("/context-path").servletPath("/servlet-path"))
43+
.andExpect(status().isOk())
44+
.andExpect(content().string(containsString("Swagger UI")));
45+
}
4646

47-
@SpringBootApplication
48-
static class SpringDocTestApp {}
47+
@SpringBootApplication
48+
static class SpringDocTestApp {
49+
}
4950
}

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectConfigUrlTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3031
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3132

3233
@TestPropertySource(properties = {
@@ -37,11 +38,9 @@ public class SpringDocRedirectConfigUrlTest extends AbstractSpringDocTest {
3738

3839
@Test
3940
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
40-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
41-
.andExpect(status().isFound()).andReturn();
42-
43-
String locationHeader = mvcResult.getResponse().getHeader("Location");
44-
assertEquals("/swagger-ui/index.html?configUrl=/foo/bar", locationHeader);
41+
mockMvc.perform(get("/swagger-ui.html"))
42+
.andExpect(status().isFound())
43+
.andExpect(header().string("Location", "/swagger-ui/index.html?configUrl=/foo/bar"));
4544
}
4645

4746
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectDefaultTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@
2626

2727
import static org.junit.jupiter.api.Assertions.assertEquals;
2828
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
2930
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3031

3132
public class SpringDocRedirectDefaultTest extends AbstractSpringDocTest {
3233

3334
@Test
3435
public void shouldRedirectWithDefaultQueryParams() throws Exception {
35-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
36-
.andExpect(status().isFound()).andReturn();
37-
38-
String locationHeader = mvcResult.getResponse().getHeader("Location");
39-
assertEquals("/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config", locationHeader);
36+
mockMvc.perform(get("/swagger-ui.html"))
37+
.andExpect(status().isFound())
38+
.andExpect(header().string("Location", "/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config"));
4039
}
4140

4241
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectFilterTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3031
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3132

3233
@TestPropertySource(properties = "springdoc.swagger-ui.filter=false")
3334
public class SpringDocRedirectFilterTest extends AbstractSpringDocTest {
3435

3536
@Test
3637
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
38-
.andExpect(status().isFound()).andReturn();
39-
40-
String locationHeader = mvcResult.getResponse().getHeader("Location");
41-
assertEquals("/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&filter=false", locationHeader);
38+
mockMvc.perform(get("/swagger-ui.html"))
39+
.andExpect(status().isFound())
40+
.andExpect(header().string("Location", "/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&filter=false"));
4241
}
4342

4443
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectLayoutTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3031
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3132

3233
@TestPropertySource(properties = "springdoc.swagger-ui.layout=BaseLayout")
3334
public class SpringDocRedirectLayoutTest extends AbstractSpringDocTest {
3435

3536
@Test
3637
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
38-
.andExpect(status().isFound()).andReturn();
39-
40-
String locationHeader = mvcResult.getResponse().getHeader("Location");
41-
assertEquals("/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&layout=BaseLayout", locationHeader);
38+
mockMvc.perform(get("/swagger-ui.html"))
39+
.andExpect(status().isFound())
40+
.andExpect(header().string("Location", "/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&layout=BaseLayout"));
4241
}
4342

4443
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectQueryParams1Test.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3031
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3132

3233
@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params-without-oauth2=true")
3334
public class SpringDocRedirectQueryParams1Test extends AbstractSpringDocTest {
3435

3536
@Test
3637
public void shouldRedirectWithQueryParamsWithoutOauth2() throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
38-
.andExpect(status().isFound()).andReturn();
39-
40-
String locationHeader = mvcResult.getResponse().getHeader("Location");
41-
assertEquals("/swagger-ui/index.html?url=/v3/api-docs", locationHeader);
38+
mockMvc.perform(get("/swagger-ui.html"))
39+
.andExpect(status().isFound())
40+
.andExpect(header().string("Location", "/swagger-ui/index.html?url=/v3/api-docs"));
4241
}
4342

4443
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectQueryParams2Test.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,22 @@
1919
package test.org.springdoc.ui.app1;
2020

2121
import org.junit.jupiter.api.Test;
22-
import test.org.springdoc.ui.AbstractSpringDocTest;
23-
2422
import org.springframework.boot.autoconfigure.SpringBootApplication;
2523
import org.springframework.test.context.TestPropertySource;
26-
import org.springframework.test.web.servlet.MvcResult;
24+
import test.org.springdoc.ui.AbstractSpringDocTest;
2725

28-
import static org.junit.jupiter.api.Assertions.assertEquals;
2926
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3028
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3129

3230
@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params=true")
3331
public class SpringDocRedirectQueryParams2Test extends AbstractSpringDocTest {
3432

3533
@Test
3634
public void shouldRedirectWithQueryParams() throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
38-
.andExpect(status().isFound()).andReturn();
39-
40-
String locationHeader = mvcResult.getResponse().getHeader("Location");
41-
assertEquals("/swagger-ui/index.html?oauth2RedirectUrl=http://localhost/swagger-ui/oauth2-redirect.html&url=/v3/api-docs", locationHeader);
35+
mockMvc.perform(get("/swagger-ui.html"))
36+
.andExpect(status().isFound())
37+
.andExpect(header().string("Location", "/swagger-ui/index.html?oauth2RedirectUrl=http://localhost/swagger-ui/oauth2-redirect.html&url=/v3/api-docs"));
4238
}
4339

4440
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectWithConfigTest.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,13 @@
1919
package test.org.springdoc.ui.app1;
2020

2121
import org.junit.jupiter.api.Test;
22-
import test.org.springdoc.ui.AbstractSpringDocTest;
23-
2422
import org.springframework.boot.autoconfigure.SpringBootApplication;
2523
import org.springframework.test.context.TestPropertySource;
26-
import org.springframework.test.web.servlet.MvcResult;
24+
import test.org.springdoc.ui.AbstractSpringDocTest;
2725

2826
import static org.hamcrest.Matchers.is;
29-
import static org.junit.jupiter.api.Assertions.assertEquals;
3027
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
31-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
32-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
3329

3430
@TestPropertySource(properties = {
3531
"springdoc.swagger-ui.validatorUrl=/foo/validate",
@@ -39,14 +35,12 @@ public class SpringDocRedirectWithConfigTest extends AbstractSpringDocTest {
3935

4036
@Test
4137
public void shouldRedirectWithConfiguredParams() throws Exception {
42-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
43-
.andExpect(status().isFound()).andReturn();
44-
45-
String locationHeader = mvcResult.getResponse().getHeader("Location");
46-
assertEquals("/swagger-ui/index.html?configUrl=/baf/batz/swagger-config", locationHeader);
38+
mockMvc.perform(get("/swagger-ui.html"))
39+
.andExpect(status().isFound())
40+
.andExpect(header().string("Location", "/swagger-ui/index.html?configUrl=/baf/batz/swagger-config"));
4741

4842
mockMvc.perform(get("/baf/batz/swagger-config"))
49-
.andExpect(status().isOk()).andExpect(jsonPath("$.validatorUrl", is("/foo/validate"))).andReturn();
43+
.andExpect(status().isOk()).andExpect(jsonPath("$.validatorUrl", is("/foo/validate")));
5044

5145
}
5246

0 commit comments

Comments
 (0)