|
3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper;
|
4 | 4 | import org.junit.Test;
|
5 | 5 | import org.junit.runner.RunWith;
|
| 6 | +import org.skyscreamer.jsonassert.JSONAssert; |
6 | 7 | import org.springdoc.core.Constants;
|
7 | 8 | import org.springframework.beans.factory.annotation.Autowired;
|
8 |
| -import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; |
| 9 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 10 | +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; |
| 11 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.context.annotation.Configuration; |
9 | 14 | import org.springframework.test.context.ActiveProfiles;
|
10 | 15 | import org.springframework.test.context.junit4.SpringRunner;
|
11 |
| -import org.springframework.test.web.reactive.server.EntityExchangeResult; |
12 |
| -import org.springframework.test.web.reactive.server.WebTestClient; |
| 16 | +import org.springframework.test.web.servlet.MockMvc; |
| 17 | +import org.springframework.test.web.servlet.MvcResult; |
13 | 18 |
|
14 | 19 | import java.nio.file.Files;
|
15 | 20 | import java.nio.file.Path;
|
16 | 21 | import java.nio.file.Paths;
|
17 | 22 |
|
18 |
| -import static org.junit.Assert.assertEquals; |
| 23 | +import static org.hamcrest.Matchers.is; |
| 24 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 25 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 26 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
19 | 27 |
|
20 | 28 | @RunWith(SpringRunner.class)
|
21 |
| -@WebFluxTest |
| 29 | +@SpringBootTest |
22 | 30 | @ActiveProfiles("test")
|
| 31 | +@AutoConfigureMockMvc |
23 | 32 | public abstract class AbstractSpringDocTest {
|
24 | 33 |
|
| 34 | + public static String className; |
| 35 | + |
25 | 36 | @Autowired
|
26 |
| - private WebTestClient webTestClient; |
| 37 | + protected MockMvc mockMvc; |
27 | 38 |
|
28 | 39 | @Autowired
|
29 | 40 | private ObjectMapper objectMapper;
|
30 | 41 |
|
| 42 | + @Configuration |
| 43 | + @EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class}) |
| 44 | + static class ContextConfiguration { } |
| 45 | + |
31 | 46 | @Test
|
32 | 47 | public void testApp() throws Exception {
|
33 |
| - EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL).exchange() |
34 |
| - .expectStatus().isOk().expectBody().returnResult(); |
35 |
| - |
36 |
| - String result = new String(getResult.getResponseBody()); |
37 |
| - String className = getClass().getSimpleName(); |
| 48 | + className = getClass().getSimpleName(); |
38 | 49 | String testNumber = className.replaceAll("[^0-9]", "");
|
39 |
| - |
| 50 | + MvcResult mockMvcResult = mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk()) |
| 51 | + .andExpect(jsonPath("$.openapi", is("3.0.1"))).andReturn(); |
| 52 | + String result = mockMvcResult.getResponse().getContentAsString(); |
40 | 53 | Path path = Paths.get(getClass().getClassLoader().getResource("results/app" + testNumber + ".json").toURI());
|
41 | 54 | byte[] fileBytes = Files.readAllBytes(path);
|
42 | 55 | String expected = new String(fileBytes);
|
43 |
| - |
44 |
| - assertEquals(objectMapper.readTree(expected), objectMapper.readTree(result)); |
| 56 | + JSONAssert.assertEquals(expected, result, true); |
45 | 57 | }
|
46 | 58 |
|
| 59 | + |
47 | 60 | }
|
0 commit comments