Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
003fa3f
Clarified an instruction
UgmaDevelopment Dec 11, 2020
aeec146
Added missing instruction. Perhaps in a previous version, the instruc…
UgmaDevelopment Dec 11, 2020
aaba7b2
Fixes maven and gradle restdocs dependency code snippets.
UgmaDevelopment Dec 11, 2020
e12240d
Main method, not fuction
UgmaDevelopment Dec 14, 2020
1cf291b
Uses Initializr Rest Docs maven configuration.
UgmaDevelopment Dec 14, 2020
a1b270d
Uses Initializr Rest Docs gradle configuration.
UgmaDevelopment Dec 14, 2020
68e98de
Adds instructions for the maven and gradle wrappers.
UgmaDevelopment Dec 14, 2020
a3b3714
Replaced "dynamic parts of that testing" with "dynamic parts of that …
UgmaDevelopment Dec 14, 2020
31df795
Removes the instruction to add testing dependencies as those dependen…
UgmaDevelopment Dec 30, 2020
38a5431
Removes `@RunWith` annotation, as `@WebMvcTest` includes the JUnit 5 …
UgmaDevelopment Dec 30, 2020
c424e97
Adds links for `@WebMvcTest` and `MockMvc`.
UgmaDevelopment Dec 30, 2020
0ccacb7
Adds tip about dedicated web layer testing tutorial.
UgmaDevelopment Dec 30, 2020
8f7156a
Clarifies that most (but not this) APIs return dynamic content; also …
UgmaDevelopment Dec 30, 2020
4cbac31
Adds link to `@AutoConfigureRestDocs` documentation.
UgmaDevelopment Dec 30, 2020
c160edb
Adds link to `MockMvcRestDocumentation.document` documentation and cl…
UgmaDevelopment Dec 30, 2020
4d1ce0b
Adds request and response snippets for clarity.
UgmaDevelopment Dec 30, 2020
9707b09
Adds links and request snippets for curl and httpie.
UgmaDevelopment Dec 30, 2020
8776435
Adds missing parenthesis to `responseFields` example
UgmaDevelopment Dec 30, 2020
a8c9dfb
Clarifies phrase about using snippets and relocates link outside of p…
UgmaDevelopment Dec 30, 2020
881f86d
Adds newer build systems and dependencies to complete folder as they …
UgmaDevelopment Dec 30, 2020
06ce9d5
Adjusted `@AutoConfigureRestDocs` back to default where the plugin wi…
UgmaDevelopment Dec 30, 2020
9af112a
Adds line to (new) Gradle configuration to have ascii docs in the sam…
UgmaDevelopment Dec 30, 2020
5c6e93b
Added a screenshot of the generated HTML documentation.
UgmaDevelopment Dec 30, 2020
1c7230e
Lets gradle know how to resolve `snippets` attribute in `index.adoc`,…
UgmaDevelopment Dec 31, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/

For all Spring applications, you should start with the https://start.spring.io[Spring
Initializr]. The Initializr offers a fast way to pull in all the dependencies you need for
an application and does a lot of the setup for you. This example needs only the Spring Web
dependency.
an application and does a lot of the setup for you. This example needs only the Spring Web
and Spring REST Docs dependencies.

The following listing shows the `pom.xml` file that is created when you choose Maven:

Expand Down Expand Up @@ -63,8 +63,8 @@ include::complete/src/main/java/com/example/testingrestdocs/HomeController.java[

== Run the Application

The Spring Initializr creates a `main` class that you can use to launch the application.
The following listing (from
The Spring Initializr creates a `{YourProjectName}Application` class with a `main` method
that is used to launch the application. The following listing (from
`src/main/java/com/example/testingrestdocs/TestingRestdocsApplication.java`) shows the
application class that the Spring Initializr created:

Expand All @@ -75,6 +75,9 @@ include::complete/src/main/java/com/example/testingrestdocs/TestingRestdocsAppli
----
====


=== Demistifying the annotations

`@SpringBootApplication` is a convenience annotation that adds all of the following:

- `@Configuration`: Tags the class as a source of bean definitions for the application
Expand All @@ -93,25 +96,43 @@ application. Did you notice that there is not a single line of XML? There is no
file, either. This web application is 100% pure Java and you did not have to deal with
configuring any plumbing or infrastructure. Spring Boot handles all of that for you.

Logging output is displayed. The service should be up and running within a few seconds.
=== Getting up and going with wrappers
The easiest way to get started is to use the *Maven or Gradle wrapper*. It will download
all the necessary dependencies and start the Spring Boot app (by running the `main()`
method) for you.

==== Running using the Maven Wrapper
Start the application by running `./mvnw spring-boot:run` on the command line in the root of the project.

==== Running using the Gradle Wrapper
Start the application by running `./gradlew bootRun` on the command line in the root of the project.


Once you start the application, logging output will be displayed and the service should be up
and running within a few seconds. You'll know it's ready once you see a line that reads
something like `Started TestingRestdocsApplication in 1.492 seconds`.


== Test the Application

Now that the application is running, you can test it. You can load the home page at
`http://localhost:8080`. However, to give yourself more confidence that the application
works when you make changes, you want to automate the testing. You also want to publish
documentation for the HTTP endpoint. You can generate the dynamic parts of that testing as
documentation for the HTTP endpoint. You can generate the dynamic parts of that documentation as
part of the tests by using Spring REST Docs.

The first thing you can do is write a simple sanity check test that fails if the
application context cannot start. To do so, add Spring Test and Spring REST Docs as
dependencies to your project, in the test scope. The following listing shows what to add
if you use Maven:
application context cannot start.

=== Maven

If you use Maven, the following listing shows what to add to the `<dependencies>`
section of `pom.xml`:

====
[source,xml]
----
include::complete/pom.xml[tag=test]
include::complete/pom.xml[tag=restdocs-dependency]
----
====

Copy link
Author

@UgmaDevelopment UgmaDevelopment Dec 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section about adding dependencies to Maven/Gradle would go away, because Initializr would've already added them—unless I'm mistaken.

Let us not forget to

  • remove the asciidoctor tags regarding adding a dependency

Expand All @@ -124,12 +145,14 @@ include::complete/pom.xml[]
----
====

The following example shows what to add if you use Gradle:
=== Gradle

If you use Gradle, add the following line to the `dependencies` section of your `build.gradle` file.

====
[source,groovy]
----
include::complete/build.gradle[tag=test]
include::complete/build.gradle[tag=restdocs-dependency]
----
====

Expand All @@ -142,7 +165,7 @@ include::complete/build.gradle[]
----
====

NOTE: You can ignore the comments in the build files. They are there to let us pick up
NOTE: You can ignore the `tag` comments in the build files. They are there to let us pick up
parts of the files for inclusion in this guide.

NOTE: You have included the `mockmvc` flavor of REST Docs, which uses Spring MockMvc to
Expand Down
2 changes: 2 additions & 0 deletions complete/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
// tag::restdocs-dependency[]
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
// end::restdocs-dependency[]
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
Expand Down
8 changes: 3 additions & 5 deletions complete/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- tag::restdocs-dependency[] -->
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
<!-- end::restdocs-dependency[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!-- We'll be using JUnit 5. Vintage is for JUnit 4 so we exclude it. -->
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
Expand Down
44 changes: 44 additions & 0 deletions initial/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
6 changes: 3 additions & 3 deletions initial/.mvn/wrapper/MavenWrapperDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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
* https://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,
Expand All @@ -20,12 +20,12 @@

public class MavenWrapperDownloader {

private static final String WRAPPER_VERSION = "0.5.2";
private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + " .jar";
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";

/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
Expand Down
Binary file modified initial/.mvn/wrapper/maven-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion initial/.mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.2/maven-wrapper-0.5.2.tar.gz
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
20 changes: 15 additions & 5 deletions initial/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'org.springframework.boot' version '2.3.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'org.springframework.boot' version '2.4.1'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'org.asciidoctor.convert' version '1.5.8'
id 'java'
}

Expand All @@ -12,13 +13,22 @@ repositories {
mavenCentral()
}

ext {
set('snippetsDir', file("build/generated-snippets"))
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
}

test {
outputs.dir snippetsDir
useJUnitPlatform()
}

asciidoctor {
inputs.dir snippetsDir
dependsOn test
}
Binary file modified initial/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion initial/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
31 changes: 14 additions & 17 deletions initial/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ esac

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar


# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
Expand Down Expand Up @@ -129,6 +130,7 @@ fi
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`

JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
Expand All @@ -154,19 +156,19 @@ if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
i=`expr $i + 1`
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
0) set -- ;;
1) set -- "$args0" ;;
2) set -- "$args0" "$args1" ;;
3) set -- "$args0" "$args1" "$args2" ;;
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi

Expand All @@ -175,14 +177,9 @@ save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
APP_ARGS=`save "$@"`

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "$@"
25 changes: 7 additions & 18 deletions initial/gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

Expand All @@ -37,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
if "%ERRORLEVEL%" == "0" goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Expand All @@ -51,7 +54,7 @@ goto fail
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init
if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
Expand All @@ -61,28 +64,14 @@ echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
Expand Down
Loading