Add support for JUnit 5
This commit adds support for JUnit 5 and its Jupiter programming model. A new Jupiter Extension implementation, RestDocumentationExtension, is provided. This extension can be applied to a test class to allow it to use Spring REST Docs to document a RESTful API. Closes gh-296
This commit is contained in:
22
samples/junit5/src/docs/asciidoc/index.adoc
Normal file
22
samples/junit5/src/docs/asciidoc/index.adoc
Normal file
@@ -0,0 +1,22 @@
|
||||
= Spring REST Docs TestNG Sample
|
||||
Andy Wilkinson;
|
||||
:doctype: book
|
||||
:icons: font
|
||||
:source-highlighter: highlightjs
|
||||
|
||||
Sample application demonstrating how to use Spring REST Docs with TestNG.
|
||||
|
||||
`SampleTestNgApplicationTests` makes a call to a very simple service and produces three
|
||||
documentation snippets.
|
||||
|
||||
One showing how to make a request using cURL:
|
||||
|
||||
include::{snippets}/sample/curl-request.adoc[]
|
||||
|
||||
One showing the HTTP request:
|
||||
|
||||
include::{snippets}/sample/http-request.adoc[]
|
||||
|
||||
And one showing the HTTP response:
|
||||
|
||||
include::{snippets}/sample/http-response.adoc[]
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.junit5;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleJUnit5Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplication(SampleJUnit5Application.class).run(args);
|
||||
}
|
||||
|
||||
@RestController
|
||||
private static class SampleController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public String index() {
|
||||
return "Hello, World";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.junit5;
|
||||
|
||||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
|
||||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.restdocs.RestDocumentationExtension;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@SpringBootTest
|
||||
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
|
||||
public class SampleJUnit5ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp(RestDocumentationExtension restDocumentation) {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(context)
|
||||
.apply(documentationConfiguration(restDocumentation)).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sample() throws Exception {
|
||||
this.mockMvc.perform(get("/"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(document("sample"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user