├── .github └── FUNDING.yml ├── .gitignore ├── .mvn └── wrapper │ └── maven-wrapper.properties ├── images ├── tests_mockmvc_with_context_wm.png ├── tests_mockmvc_wm.png └── tests_springboot_wm.png ├── mvnw ├── mvnw.cmd ├── pom.xml ├── readme.md └── src ├── main ├── java │ └── io │ │ └── tpd │ │ └── superheroes │ │ ├── MvcTestsApplication.java │ │ ├── controller │ │ ├── SuperHeroController.java │ │ ├── SuperHeroExceptionHandler.java │ │ └── SuperHeroFilter.java │ │ ├── domain │ │ └── SuperHero.java │ │ ├── exceptions │ │ └── NonExistingHeroException.java │ │ └── repository │ │ ├── SuperHeroRepository.java │ │ └── SuperHeroRepositoryImpl.java └── resources │ └── application.properties └── test └── java └── io └── tpd └── superheroes └── controller ├── SuperHeroControllerMockMvcStandaloneTest.java ├── SuperHeroControllerMockMvcWithContextTest.java ├── SuperHeroControllerSpringBootMockTest.java └── SuperHeroControllerSpringBootTest.java /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: mechero 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | wrapperVersion=3.3.2 18 | distributionType=only-script 19 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip 20 | -------------------------------------------------------------------------------- /images/tests_mockmvc_with_context_wm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mechero/spring-boot-testing-strategies/e06a2b4b8af04937d8438b9e3366af6bd3235d14/images/tests_mockmvc_with_context_wm.png -------------------------------------------------------------------------------- /images/tests_mockmvc_wm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mechero/spring-boot-testing-strategies/e06a2b4b8af04937d8438b9e3366af6bd3235d14/images/tests_mockmvc_wm.png -------------------------------------------------------------------------------- /images/tests_springboot_wm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mechero/spring-boot-testing-strategies/e06a2b4b8af04937d8438b9e3366af6bd3235d14/images/tests_springboot_wm.png -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Apache Maven Wrapper startup batch script, version 3.3.2 23 | # 24 | # Optional ENV vars 25 | # ----------------- 26 | # JAVA_HOME - location of a JDK home dir, required when download maven via java source 27 | # MVNW_REPOURL - repo url base for downloading maven distribution 28 | # MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 29 | # MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output 30 | # ---------------------------------------------------------------------------- 31 | 32 | set -euf 33 | [ "${MVNW_VERBOSE-}" != debug ] || set -x 34 | 35 | # OS specific support. 36 | native_path() { printf %s\\n "$1"; } 37 | case "$(uname)" in 38 | CYGWIN* | MINGW*) 39 | [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" 40 | native_path() { cygpath --path --windows "$1"; } 41 | ;; 42 | esac 43 | 44 | # set JAVACMD and JAVACCMD 45 | set_java_home() { 46 | # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched 47 | if [ -n "${JAVA_HOME-}" ]; then 48 | if [ -x "$JAVA_HOME/jre/sh/java" ]; then 49 | # IBM's JDK on AIX uses strange locations for the executables 50 | JAVACMD="$JAVA_HOME/jre/sh/java" 51 | JAVACCMD="$JAVA_HOME/jre/sh/javac" 52 | else 53 | JAVACMD="$JAVA_HOME/bin/java" 54 | JAVACCMD="$JAVA_HOME/bin/javac" 55 | 56 | if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then 57 | echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 58 | echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 59 | return 1 60 | fi 61 | fi 62 | else 63 | JAVACMD="$( 64 | 'set' +e 65 | 'unset' -f command 2>/dev/null 66 | 'command' -v java 67 | )" || : 68 | JAVACCMD="$( 69 | 'set' +e 70 | 'unset' -f command 2>/dev/null 71 | 'command' -v javac 72 | )" || : 73 | 74 | if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then 75 | echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 76 | return 1 77 | fi 78 | fi 79 | } 80 | 81 | # hash string like Java String::hashCode 82 | hash_string() { 83 | str="${1:-}" h=0 84 | while [ -n "$str" ]; do 85 | char="${str%"${str#?}"}" 86 | h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) 87 | str="${str#?}" 88 | done 89 | printf %x\\n $h 90 | } 91 | 92 | verbose() { :; } 93 | [ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } 94 | 95 | die() { 96 | printf %s\\n "$1" >&2 97 | exit 1 98 | } 99 | 100 | trim() { 101 | # MWRAPPER-139: 102 | # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. 103 | # Needed for removing poorly interpreted newline sequences when running in more 104 | # exotic environments such as mingw bash on Windows. 105 | printf "%s" "${1}" | tr -d '[:space:]' 106 | } 107 | 108 | # parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties 109 | while IFS="=" read -r key value; do 110 | case "${key-}" in 111 | distributionUrl) distributionUrl=$(trim "${value-}") ;; 112 | distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; 113 | esac 114 | done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" 115 | [ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" 116 | 117 | case "${distributionUrl##*/}" in 118 | maven-mvnd-*bin.*) 119 | MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ 120 | case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in 121 | *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; 122 | :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; 123 | :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; 124 | :Linux*x86_64*) distributionPlatform=linux-amd64 ;; 125 | *) 126 | echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 127 | distributionPlatform=linux-amd64 128 | ;; 129 | esac 130 | distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" 131 | ;; 132 | maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; 133 | *) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; 134 | esac 135 | 136 | # apply MVNW_REPOURL and calculate MAVEN_HOME 137 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 138 | [ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" 139 | distributionUrlName="${distributionUrl##*/}" 140 | distributionUrlNameMain="${distributionUrlName%.*}" 141 | distributionUrlNameMain="${distributionUrlNameMain%-bin}" 142 | MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" 143 | MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" 144 | 145 | exec_maven() { 146 | unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : 147 | exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" 148 | } 149 | 150 | if [ -d "$MAVEN_HOME" ]; then 151 | verbose "found existing MAVEN_HOME at $MAVEN_HOME" 152 | exec_maven "$@" 153 | fi 154 | 155 | case "${distributionUrl-}" in 156 | *?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; 157 | *) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; 158 | esac 159 | 160 | # prepare tmp dir 161 | if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then 162 | clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } 163 | trap clean HUP INT TERM EXIT 164 | else 165 | die "cannot create temp dir" 166 | fi 167 | 168 | mkdir -p -- "${MAVEN_HOME%/*}" 169 | 170 | # Download and Install Apache Maven 171 | verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 172 | verbose "Downloading from: $distributionUrl" 173 | verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 174 | 175 | # select .zip or .tar.gz 176 | if ! command -v unzip >/dev/null; then 177 | distributionUrl="${distributionUrl%.zip}.tar.gz" 178 | distributionUrlName="${distributionUrl##*/}" 179 | fi 180 | 181 | # verbose opt 182 | __MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' 183 | [ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v 184 | 185 | # normalize http auth 186 | case "${MVNW_PASSWORD:+has-password}" in 187 | '') MVNW_USERNAME='' MVNW_PASSWORD='' ;; 188 | has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; 189 | esac 190 | 191 | if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then 192 | verbose "Found wget ... using wget" 193 | wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" 194 | elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then 195 | verbose "Found curl ... using curl" 196 | curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" 197 | elif set_java_home; then 198 | verbose "Falling back to use Java to download" 199 | javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" 200 | targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" 201 | cat >"$javaSource" <<-END 202 | public class Downloader extends java.net.Authenticator 203 | { 204 | protected java.net.PasswordAuthentication getPasswordAuthentication() 205 | { 206 | return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); 207 | } 208 | public static void main( String[] args ) throws Exception 209 | { 210 | setDefault( new Downloader() ); 211 | java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); 212 | } 213 | } 214 | END 215 | # For Cygwin/MinGW, switch paths to Windows format before running javac and java 216 | verbose " - Compiling Downloader.java ..." 217 | "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" 218 | verbose " - Running Downloader.java ..." 219 | "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" 220 | fi 221 | 222 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 223 | if [ -n "${distributionSha256Sum-}" ]; then 224 | distributionSha256Result=false 225 | if [ "$MVN_CMD" = mvnd.sh ]; then 226 | echo "Checksum validation is not supported for maven-mvnd." >&2 227 | echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 228 | exit 1 229 | elif command -v sha256sum >/dev/null; then 230 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then 231 | distributionSha256Result=true 232 | fi 233 | elif command -v shasum >/dev/null; then 234 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then 235 | distributionSha256Result=true 236 | fi 237 | else 238 | echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 239 | echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 240 | exit 1 241 | fi 242 | if [ $distributionSha256Result = false ]; then 243 | echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 244 | echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 245 | exit 1 246 | fi 247 | fi 248 | 249 | # unzip and move 250 | if command -v unzip >/dev/null; then 251 | unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" 252 | else 253 | tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" 254 | fi 255 | printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" 256 | mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" 257 | 258 | clean || : 259 | exec_maven "$@" 260 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | <# : batch portion 2 | @REM ---------------------------------------------------------------------------- 3 | @REM Licensed to the Apache Software Foundation (ASF) under one 4 | @REM or more contributor license agreements. See the NOTICE file 5 | @REM distributed with this work for additional information 6 | @REM regarding copyright ownership. The ASF licenses this file 7 | @REM to you under the Apache License, Version 2.0 (the 8 | @REM "License"); you may not use this file except in compliance 9 | @REM with the License. You may obtain a copy of the License at 10 | @REM 11 | @REM http://www.apache.org/licenses/LICENSE-2.0 12 | @REM 13 | @REM Unless required by applicable law or agreed to in writing, 14 | @REM software distributed under the License is distributed on an 15 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | @REM KIND, either express or implied. See the License for the 17 | @REM specific language governing permissions and limitations 18 | @REM under the License. 19 | @REM ---------------------------------------------------------------------------- 20 | 21 | @REM ---------------------------------------------------------------------------- 22 | @REM Apache Maven Wrapper startup batch script, version 3.3.2 23 | @REM 24 | @REM Optional ENV vars 25 | @REM MVNW_REPOURL - repo url base for downloading maven distribution 26 | @REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 27 | @REM MVNW_VERBOSE - true: enable verbose log; others: silence the output 28 | @REM ---------------------------------------------------------------------------- 29 | 30 | @IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) 31 | @SET __MVNW_CMD__= 32 | @SET __MVNW_ERROR__= 33 | @SET __MVNW_PSMODULEP_SAVE=%PSModulePath% 34 | @SET PSModulePath= 35 | @FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( 36 | IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) 37 | ) 38 | @SET PSModulePath=%__MVNW_PSMODULEP_SAVE% 39 | @SET __MVNW_PSMODULEP_SAVE= 40 | @SET __MVNW_ARG0_NAME__= 41 | @SET MVNW_USERNAME= 42 | @SET MVNW_PASSWORD= 43 | @IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) 44 | @echo Cannot start maven from wrapper >&2 && exit /b 1 45 | @GOTO :EOF 46 | : end batch / begin powershell #> 47 | 48 | $ErrorActionPreference = "Stop" 49 | if ($env:MVNW_VERBOSE -eq "true") { 50 | $VerbosePreference = "Continue" 51 | } 52 | 53 | # calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties 54 | $distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl 55 | if (!$distributionUrl) { 56 | Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" 57 | } 58 | 59 | switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { 60 | "maven-mvnd-*" { 61 | $USE_MVND = $true 62 | $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" 63 | $MVN_CMD = "mvnd.cmd" 64 | break 65 | } 66 | default { 67 | $USE_MVND = $false 68 | $MVN_CMD = $script -replace '^mvnw','mvn' 69 | break 70 | } 71 | } 72 | 73 | # apply MVNW_REPOURL and calculate MAVEN_HOME 74 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 75 | if ($env:MVNW_REPOURL) { 76 | $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } 77 | $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" 78 | } 79 | $distributionUrlName = $distributionUrl -replace '^.*/','' 80 | $distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' 81 | $MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" 82 | if ($env:MAVEN_USER_HOME) { 83 | $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" 84 | } 85 | $MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' 86 | $MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" 87 | 88 | if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { 89 | Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" 90 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 91 | exit $? 92 | } 93 | 94 | if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { 95 | Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" 96 | } 97 | 98 | # prepare tmp dir 99 | $TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile 100 | $TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" 101 | $TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null 102 | trap { 103 | if ($TMP_DOWNLOAD_DIR.Exists) { 104 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 105 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 106 | } 107 | } 108 | 109 | New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null 110 | 111 | # Download and Install Apache Maven 112 | Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 113 | Write-Verbose "Downloading from: $distributionUrl" 114 | Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 115 | 116 | $webclient = New-Object System.Net.WebClient 117 | if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { 118 | $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) 119 | } 120 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 121 | $webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null 122 | 123 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 124 | $distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum 125 | if ($distributionSha256Sum) { 126 | if ($USE_MVND) { 127 | Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." 128 | } 129 | Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash 130 | if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { 131 | Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." 132 | } 133 | } 134 | 135 | # unzip and move 136 | Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null 137 | Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null 138 | try { 139 | Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null 140 | } catch { 141 | if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { 142 | Write-Error "fail to move MAVEN_HOME" 143 | } 144 | } finally { 145 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 146 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 147 | } 148 | 149 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 150 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.tpd 7 | mvc-tests 8 | 1.0-SNAPSHOT 9 | jar 10 | 11 | mvc-tests 12 | Sample project comparing MVC test strategies in Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 3.4.5 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 24 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | 35 | com.fasterxml.jackson.datatype 36 | jackson-datatype-jdk8 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | org.junit.vintage 46 | junit-vintage-engine 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Spring Boot Testing Strategies 2 | 3 | ## Introduction 4 | 5 | This sample application made with Spring Boot is intended to show the different approach for testing, from Unit Tests with MockMVC in Standalone mode to full `@SpringBootTest` as Integration tests between the modules. 6 | 7 | The complete guide is available on [The Practical Developer Blog](https://thepracticaldeveloper.com/guide-spring-boot-controller-tests/). 8 | 9 | ## The application 10 | 11 | The logic behind the application is simple: it's a repository of superheroes that you can access through a REST API. It allows to read the available ones (which are hardcoded when the application starts up) and also add new members to the crew. 12 | 13 | The architecture is simple: just the Controller layer (REST) and a `SuperHeroRepository`. To illustrate the differences when creating tests, there are two extra classes that work at a web layer level: 14 | 15 | * `SuperHeroExceptionHandler`. It's a `ControllerAdvice` that will transform a `NonExistingHeroException` into a `404 NOT_FOUND` HTTP error code. 16 | * `SuperHeroFilter`. This web filter adds a new header to the HTTP response. 17 | 18 | ## Testing strategies 19 | 20 | In the test sources you can find four different approaches to test the Controller. `SuperHeroControllerMockMvcStandaloneTest`. Uses a `MockitoJUnitRunner` and it's the most lightweight approach. 21 | 22 | ![MockMVC in Standalone mode](images/tests_mockmvc_wm.png) 23 | 24 | Then you can find two approaches using a Spring context, both use `MockMVC` and one of them already introduces the `@SpringBootTest` annotation. 25 | 26 | ![MockMVC using the context](images/tests_mockmvc_with_context_wm.png) 27 | 28 | Finally, `SuperHeroControllerSpringBootTest` shows how to write a `@SpringBootTest` based test mocking other layers but utilizing the web server with a `RestTemplate`. 29 | 30 | ![@SpringBootTest using context and web server](images/tests_springboot_wm.png) 31 | 32 | To check conclusion and more information please visit [the blog](https://thepracticaldeveloper.com/guide-spring-boot-controller-tests/). 33 | 34 | ## Changelog 35 | 36 | ### 2025/05 Update: Spring Boot 3.4.5 and JDK 24 37 | 38 | * The trailing slash in some tests makes them fail. See [this blog post](https://www.lucasjosino.com/blog/spring-boot-using-the-new-filter-for-trailing-slash-handling/) 39 | * Trailing slashes are now removed since they were irrelevant for this blog post 40 | * Moved from `javax.` packages to `jakarta.` where needed 41 | * Replaced `MockBean`'s deprecated annotation by its new version `MockitoBean` -------------------------------------------------------------------------------- /src/main/java/io/tpd/superheroes/MvcTestsApplication.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MvcTestsApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MvcTestsApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/superheroes/controller/SuperHeroController.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.controller; 2 | 3 | import io.tpd.superheroes.domain.SuperHero; 4 | import io.tpd.superheroes.repository.SuperHeroRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | import java.util.Optional; 10 | 11 | @RestController 12 | @RequestMapping("/superheroes") 13 | public final class SuperHeroController { 14 | 15 | private final SuperHeroRepository superHeroRepository; 16 | 17 | @Autowired 18 | public SuperHeroController(SuperHeroRepository superHeroRepository) { 19 | this.superHeroRepository = superHeroRepository; 20 | } 21 | 22 | @GetMapping("/{id}") 23 | public SuperHero getSuperHeroById(@PathVariable int id) { 24 | return superHeroRepository.getSuperHero(id); 25 | } 26 | 27 | @GetMapping 28 | public Optional getSuperHeroByHeroName(@RequestParam("name") String heroName) { 29 | return superHeroRepository.getSuperHero(heroName); 30 | } 31 | 32 | @PostMapping 33 | @ResponseStatus(HttpStatus.CREATED) 34 | public void addNewSuperHero(@RequestBody SuperHero superHero) { 35 | superHeroRepository.saveSuperHero(superHero); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/superheroes/controller/SuperHeroExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.controller; 2 | 3 | import io.tpd.superheroes.exceptions.NonExistingHeroException; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.web.bind.annotation.ExceptionHandler; 6 | import org.springframework.web.bind.annotation.ResponseStatus; 7 | import org.springframework.web.bind.annotation.RestControllerAdvice; 8 | 9 | /** 10 | * Maps exceptions to HTTP codes 11 | * @author moises.macero 12 | */ 13 | @RestControllerAdvice 14 | public class SuperHeroExceptionHandler { 15 | 16 | @ExceptionHandler(NonExistingHeroException.class) 17 | @ResponseStatus(HttpStatus.NOT_FOUND) 18 | public void handleNonExistingHero() { 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/superheroes/controller/SuperHeroFilter.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.controller; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import jakarta.servlet.*; 6 | import jakarta.servlet.http.HttpServletResponse; 7 | import java.io.IOException; 8 | 9 | /** 10 | * @author moises.macero 11 | */ 12 | @Component 13 | public class SuperHeroFilter implements Filter { 14 | 15 | @Override 16 | public void init(FilterConfig filterConfig) {} 17 | 18 | @Override 19 | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 20 | var httpServletResponse = (HttpServletResponse) servletResponse; 21 | httpServletResponse.setHeader("X-SUPERHERO-APP", "super-header"); 22 | filterChain.doFilter(servletRequest, servletResponse); 23 | } 24 | 25 | @Override 26 | public void destroy() {} 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/superheroes/domain/SuperHero.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.domain; 2 | 3 | public record SuperHero(String firstName, 4 | String lastName, 5 | String heroName) { 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/superheroes/exceptions/NonExistingHeroException.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.exceptions; 2 | 3 | /** 4 | * This exception is thrown when the SuperHero can't be found in the application if searching by ID. 5 | * @author moises.macero 6 | */ 7 | public class NonExistingHeroException extends RuntimeException { 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/superheroes/repository/SuperHeroRepository.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.repository; 2 | 3 | import io.tpd.superheroes.domain.SuperHero; 4 | 5 | import java.util.Optional; 6 | 7 | /** 8 | * Provides access to superheroes' data 9 | * @author moises.macero 10 | */ 11 | public interface SuperHeroRepository { 12 | 13 | /** 14 | * Retrieves a super hero by the id. 15 | * If the id does not exist, a {@link io.tpd.superheroes.exceptions.NonExistingHeroException} will be thrown. 16 | * 17 | * @param id the unique id of the super hero 18 | * @return the SuperHero details 19 | */ 20 | SuperHero getSuperHero(int id); 21 | 22 | /** 23 | * Retrieves a super hero given his super hero alias. 24 | * 25 | * @param heroName the super hero name 26 | * @return the SuperHero details 27 | */ 28 | Optional getSuperHero(String heroName); 29 | 30 | /** 31 | * Saves the super hero. 32 | * 33 | * @param superHero the details of the super hero 34 | */ 35 | void saveSuperHero(SuperHero superHero); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/superheroes/repository/SuperHeroRepositoryImpl.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.repository; 2 | 3 | import io.tpd.superheroes.domain.SuperHero; 4 | import io.tpd.superheroes.exceptions.NonExistingHeroException; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | import java.util.Optional; 10 | 11 | /** 12 | * Simple, In-memory implementation of SuperHero Repository. It comes with some predefined data. 13 | * 14 | * @author moises.macero 15 | */ 16 | @Component 17 | class SuperHeroRepositoryImpl implements SuperHeroRepository { 18 | 19 | private List superHeroList; 20 | 21 | public SuperHeroRepositoryImpl() { 22 | superHeroList = new ArrayList<>(); 23 | superHeroList.add(new SuperHero("Jean", "Grey", "Phoenix")); 24 | superHeroList.add(new SuperHero("Bruce", "Wayne", "Batman")); 25 | superHeroList.add(new SuperHero("Susan", "Storm", "Invisible woman")); 26 | superHeroList.add(new SuperHero("Peter", "Parker", "Spiderman")); 27 | } 28 | 29 | @Override 30 | public SuperHero getSuperHero(int id) { 31 | if (id > superHeroList.size()) throw new NonExistingHeroException(); 32 | return superHeroList.get(id - 1); 33 | } 34 | 35 | @Override 36 | public Optional getSuperHero(String heroName) { 37 | return superHeroList.stream().filter(h -> h.heroName().equals(heroName)).findAny(); 38 | } 39 | 40 | @Override 41 | public void saveSuperHero(SuperHero superHero) { 42 | superHeroList.add(superHero); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mechero/spring-boot-testing-strategies/e06a2b4b8af04937d8438b9e3366af6bd3235d14/src/main/resources/application.properties -------------------------------------------------------------------------------- /src/test/java/io/tpd/superheroes/controller/SuperHeroControllerMockMvcStandaloneTest.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.controller; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import io.tpd.superheroes.domain.SuperHero; 5 | import io.tpd.superheroes.exceptions.NonExistingHeroException; 6 | import io.tpd.superheroes.repository.SuperHeroRepository; 7 | import org.junit.jupiter.api.BeforeEach; 8 | import org.junit.jupiter.api.Test; 9 | import org.junit.jupiter.api.extension.ExtendWith; 10 | import org.mockito.InjectMocks; 11 | import org.mockito.Mock; 12 | import org.mockito.junit.jupiter.MockitoExtension; 13 | import org.springframework.boot.test.json.JacksonTester; 14 | import org.springframework.http.HttpStatus; 15 | import org.springframework.http.MediaType; 16 | import org.springframework.mock.web.MockHttpServletResponse; 17 | import org.springframework.test.web.servlet.MockMvc; 18 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 19 | 20 | import java.util.Optional; 21 | 22 | import static org.assertj.core.api.Assertions.assertThat; 23 | import static org.mockito.BDDMockito.given; 24 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 25 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 26 | 27 | /** 28 | * This class demonstrates how to test a controller using MockMVC with Standalone setup. 29 | * 30 | * @author moises.macero 31 | */ 32 | @ExtendWith(MockitoExtension.class) 33 | public class SuperHeroControllerMockMvcStandaloneTest { 34 | 35 | private MockMvc mvc; 36 | 37 | @Mock 38 | private SuperHeroRepository superHeroRepository; 39 | 40 | @InjectMocks 41 | private SuperHeroController superHeroController; 42 | 43 | // This object will be magically initialized by the initFields method below. 44 | private JacksonTester jsonSuperHero; 45 | 46 | @BeforeEach 47 | public void setup() { 48 | // We would need this line if we would not use the MockitoExtension 49 | // MockitoAnnotations.initMocks(this); 50 | // Here we can't use @AutoConfigureJsonTesters because there isn't a Spring context 51 | JacksonTester.initFields(this, new ObjectMapper()); 52 | // MockMvc standalone approach 53 | mvc = MockMvcBuilders.standaloneSetup(superHeroController) 54 | .setControllerAdvice(new SuperHeroExceptionHandler()) 55 | .addFilters(new SuperHeroFilter()) 56 | .build(); 57 | } 58 | 59 | @Test 60 | public void canRetrieveByIdWhenExists() throws Exception { 61 | // given 62 | given(superHeroRepository.getSuperHero(2)) 63 | .willReturn(new SuperHero("Rob", "Mannon", "RobotMan")); 64 | 65 | // when 66 | MockHttpServletResponse response = mvc.perform( 67 | get("/superheroes/2") 68 | .accept(MediaType.APPLICATION_JSON)) 69 | .andReturn().getResponse(); 70 | 71 | // then 72 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 73 | assertThat(response.getContentAsString()).isEqualTo( 74 | jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson() 75 | ); 76 | } 77 | 78 | @Test 79 | public void canRetrieveByIdWhenDoesNotExist() throws Exception { 80 | // given 81 | given(superHeroRepository.getSuperHero(2)) 82 | .willThrow(new NonExistingHeroException()); 83 | 84 | // when 85 | MockHttpServletResponse response = mvc.perform( 86 | get("/superheroes/2") 87 | .accept(MediaType.APPLICATION_JSON)) 88 | .andReturn().getResponse(); 89 | 90 | // then 91 | assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); 92 | assertThat(response.getContentAsString()).isEmpty(); 93 | } 94 | 95 | @Test 96 | public void canRetrieveByNameWhenExists() throws Exception { 97 | // given 98 | given(superHeroRepository.getSuperHero("RobotMan")) 99 | .willReturn(Optional.of(new SuperHero("Rob", "Mannon", "RobotMan"))); 100 | 101 | // when 102 | MockHttpServletResponse response = mvc.perform( 103 | get("/superheroes?name=RobotMan") 104 | .accept(MediaType.APPLICATION_JSON)) 105 | .andReturn().getResponse(); 106 | 107 | // then 108 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 109 | assertThat(response.getContentAsString()).isEqualTo( 110 | jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson() 111 | ); 112 | } 113 | 114 | @Test 115 | public void canRetrieveByNameWhenDoesNotExist() throws Exception { 116 | // given 117 | given(superHeroRepository.getSuperHero("RobotMan")) 118 | .willReturn(Optional.empty()); 119 | 120 | // when 121 | MockHttpServletResponse response = mvc.perform( 122 | get("/superheroes?name=RobotMan") 123 | .accept(MediaType.APPLICATION_JSON)) 124 | .andReturn().getResponse(); 125 | 126 | // then 127 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 128 | assertThat(response.getContentAsString()).isEqualTo("null"); 129 | } 130 | 131 | @Test 132 | public void canCreateANewSuperHero() throws Exception { 133 | // when 134 | MockHttpServletResponse response = mvc.perform( 135 | post("/superheroes").contentType(MediaType.APPLICATION_JSON).content( 136 | jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson() 137 | )).andReturn().getResponse(); 138 | 139 | // then 140 | assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value()); 141 | } 142 | 143 | @Test 144 | public void headerIsPresent() throws Exception { 145 | // when 146 | MockHttpServletResponse response = mvc.perform( 147 | get("/superheroes/2") 148 | .accept(MediaType.APPLICATION_JSON)) 149 | .andReturn().getResponse(); 150 | 151 | // then 152 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 153 | assertThat(response.getHeaders("X-SUPERHERO-APP")).containsOnly("super-header"); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/test/java/io/tpd/superheroes/controller/SuperHeroControllerMockMvcWithContextTest.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.controller; 2 | 3 | import io.tpd.superheroes.domain.SuperHero; 4 | import io.tpd.superheroes.exceptions.NonExistingHeroException; 5 | import io.tpd.superheroes.repository.SuperHeroRepository; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters; 9 | import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; 10 | import org.springframework.boot.test.json.JacksonTester; 11 | import org.springframework.http.HttpStatus; 12 | import org.springframework.http.MediaType; 13 | import org.springframework.mock.web.MockHttpServletResponse; 14 | import org.springframework.test.context.bean.override.mockito.MockitoBean; 15 | import org.springframework.test.web.servlet.MockMvc; 16 | 17 | import java.util.Optional; 18 | 19 | import static org.assertj.core.api.Assertions.assertThat; 20 | import static org.mockito.BDDMockito.given; 21 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 22 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 23 | 24 | /** 25 | * This class demonstrates how to test a controller using MockMVC loading a Test Context 26 | * 27 | * @author moises.macero 28 | */ 29 | @AutoConfigureJsonTesters 30 | @WebMvcTest(SuperHeroController.class) 31 | public class SuperHeroControllerMockMvcWithContextTest { 32 | 33 | @Autowired 34 | private MockMvc mvc; 35 | 36 | @MockitoBean 37 | private SuperHeroRepository superHeroRepository; 38 | 39 | // This object will be initialized thanks to @AutoConfigureJsonTesters 40 | @Autowired 41 | private JacksonTester jsonSuperHero; 42 | 43 | @Test 44 | public void canRetrieveByIdWhenExists() throws Exception { 45 | // given 46 | given(superHeroRepository.getSuperHero(2)) 47 | .willReturn(new SuperHero("Rob", "Mannon", "RobotMan")); 48 | 49 | // when 50 | MockHttpServletResponse response = mvc.perform( 51 | get("/superheroes/2") 52 | .accept(MediaType.APPLICATION_JSON)) 53 | .andReturn().getResponse(); 54 | 55 | // then 56 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 57 | assertThat(response.getContentAsString()).isEqualTo( 58 | jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson() 59 | ); 60 | } 61 | 62 | @Test 63 | public void canRetrieveByIdWhenDoesNotExist() throws Exception { 64 | // given 65 | given(superHeroRepository.getSuperHero(2)) 66 | .willThrow(new NonExistingHeroException()); 67 | 68 | // when 69 | MockHttpServletResponse response = mvc.perform( 70 | get("/superheroes/2") 71 | .accept(MediaType.APPLICATION_JSON)) 72 | .andReturn().getResponse(); 73 | 74 | // then 75 | assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); 76 | assertThat(response.getContentAsString()).isEmpty(); 77 | } 78 | 79 | @Test 80 | public void canRetrieveByNameWhenExists() throws Exception { 81 | // given 82 | given(superHeroRepository.getSuperHero("RobotMan")) 83 | .willReturn(Optional.of(new SuperHero("Rob", "Mannon", "RobotMan"))); 84 | 85 | // when 86 | MockHttpServletResponse response = mvc.perform( 87 | get("/superheroes?name=RobotMan") 88 | .accept(MediaType.APPLICATION_JSON)) 89 | .andReturn().getResponse(); 90 | 91 | // then 92 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 93 | assertThat(response.getContentAsString()).isEqualTo( 94 | jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson() 95 | ); 96 | } 97 | 98 | @Test 99 | public void canRetrieveByNameWhenDoesNotExist() throws Exception { 100 | // given 101 | given(superHeroRepository.getSuperHero("RobotMan")) 102 | .willReturn(Optional.empty()); 103 | 104 | // when 105 | MockHttpServletResponse response = mvc.perform( 106 | get("/superheroes?name=RobotMan") 107 | .accept(MediaType.APPLICATION_JSON)) 108 | .andReturn().getResponse(); 109 | 110 | // then 111 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 112 | assertThat(response.getContentAsString()).isEqualTo("null"); 113 | } 114 | 115 | @Test 116 | public void canCreateANewSuperHero() throws Exception { 117 | // when 118 | MockHttpServletResponse response = mvc.perform( 119 | post("/superheroes").contentType(MediaType.APPLICATION_JSON).content( 120 | jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson() 121 | )).andReturn().getResponse(); 122 | 123 | // then 124 | assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value()); 125 | } 126 | 127 | @Test 128 | public void headerIsPresent() throws Exception { 129 | // when 130 | MockHttpServletResponse response = mvc.perform( 131 | get("/superheroes/2") 132 | .accept(MediaType.APPLICATION_JSON)) 133 | .andReturn().getResponse(); 134 | 135 | // then 136 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 137 | assertThat(response.getHeaders("X-SUPERHERO-APP")).containsOnly("super-header"); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/test/java/io/tpd/superheroes/controller/SuperHeroControllerSpringBootMockTest.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.controller; 2 | 3 | import io.tpd.superheroes.domain.SuperHero; 4 | import io.tpd.superheroes.exceptions.NonExistingHeroException; 5 | import io.tpd.superheroes.repository.SuperHeroRepository; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters; 9 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | import org.springframework.boot.test.json.JacksonTester; 12 | import org.springframework.http.HttpStatus; 13 | import org.springframework.http.MediaType; 14 | import org.springframework.mock.web.MockHttpServletResponse; 15 | import org.springframework.test.context.bean.override.mockito.MockitoBean; 16 | import org.springframework.test.web.servlet.MockMvc; 17 | 18 | import java.util.Optional; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | import static org.mockito.BDDMockito.given; 22 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 23 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 24 | 25 | /** 26 | * This class demonstrates how to test a controller using Spring Boot Test 27 | * with a MOCK web environment, which makes it similar to just using @WebMvcTest 28 | * 29 | * @author moises.macero 30 | */ 31 | @AutoConfigureJsonTesters 32 | @SpringBootTest 33 | @AutoConfigureMockMvc 34 | public class SuperHeroControllerSpringBootMockTest { 35 | 36 | @Autowired 37 | private MockMvc mvc; 38 | 39 | @MockitoBean 40 | private SuperHeroRepository superHeroRepository; 41 | 42 | // This object will be initialized thanks to @AutoConfigureJsonTesters 43 | @Autowired 44 | private JacksonTester jsonSuperHero; 45 | 46 | @Test 47 | public void canRetrieveByIdWhenExists() throws Exception { 48 | // given 49 | given(superHeroRepository.getSuperHero(2)) 50 | .willReturn(new SuperHero("Rob", "Mannon", "RobotMan")); 51 | 52 | // when 53 | MockHttpServletResponse response = mvc.perform( 54 | get("/superheroes/2") 55 | .accept(MediaType.APPLICATION_JSON)) 56 | .andReturn().getResponse(); 57 | 58 | // then 59 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 60 | assertThat(response.getContentAsString()).isEqualTo( 61 | jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson() 62 | ); 63 | } 64 | 65 | @Test 66 | public void canRetrieveByIdWhenDoesNotExist() throws Exception { 67 | // given 68 | given(superHeroRepository.getSuperHero(2)) 69 | .willThrow(new NonExistingHeroException()); 70 | 71 | // when 72 | MockHttpServletResponse response = mvc.perform( 73 | get("/superheroes/2") 74 | .accept(MediaType.APPLICATION_JSON)) 75 | .andReturn().getResponse(); 76 | 77 | // then 78 | assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); 79 | assertThat(response.getContentAsString()).isEmpty(); 80 | } 81 | 82 | @Test 83 | public void canRetrieveByNameWhenExists() throws Exception { 84 | // given 85 | given(superHeroRepository.getSuperHero("RobotMan")) 86 | .willReturn(Optional.of(new SuperHero("Rob", "Mannon", "RobotMan"))); 87 | 88 | // when 89 | MockHttpServletResponse response = mvc.perform( 90 | get("/superheroes?name=RobotMan") 91 | .accept(MediaType.APPLICATION_JSON)) 92 | .andReturn().getResponse(); 93 | 94 | // then 95 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 96 | assertThat(response.getContentAsString()).isEqualTo( 97 | jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson() 98 | ); 99 | } 100 | 101 | @Test 102 | public void canRetrieveByNameWhenDoesNotExist() throws Exception { 103 | // given 104 | given(superHeroRepository.getSuperHero("RobotMan")) 105 | .willReturn(Optional.empty()); 106 | 107 | // when 108 | MockHttpServletResponse response = mvc.perform( 109 | get("/superheroes?name=RobotMan") 110 | .accept(MediaType.APPLICATION_JSON)) 111 | .andReturn().getResponse(); 112 | 113 | // then 114 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 115 | assertThat(response.getContentAsString()).isEqualTo("null"); 116 | } 117 | 118 | @Test 119 | public void canCreateANewSuperHero() throws Exception { 120 | // when 121 | MockHttpServletResponse response = mvc.perform( 122 | post("/superheroes").contentType(MediaType.APPLICATION_JSON).content( 123 | jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson() 124 | )).andReturn().getResponse(); 125 | 126 | // then 127 | assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value()); 128 | } 129 | 130 | @Test 131 | public void headerIsPresent() throws Exception { 132 | // when 133 | MockHttpServletResponse response = mvc.perform( 134 | get("/superheroes/2") 135 | .accept(MediaType.APPLICATION_JSON)) 136 | .andReturn().getResponse(); 137 | 138 | // then 139 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); 140 | assertThat(response.getHeaders("X-SUPERHERO-APP")).containsOnly("super-header"); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/test/java/io/tpd/superheroes/controller/SuperHeroControllerSpringBootTest.java: -------------------------------------------------------------------------------- 1 | package io.tpd.superheroes.controller; 2 | 3 | import io.tpd.superheroes.domain.SuperHero; 4 | import io.tpd.superheroes.exceptions.NonExistingHeroException; 5 | import io.tpd.superheroes.repository.SuperHeroRepository; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.boot.test.web.client.TestRestTemplate; 10 | import org.springframework.http.HttpStatus; 11 | import org.springframework.http.ResponseEntity; 12 | import org.springframework.test.context.bean.override.mockito.MockitoBean; 13 | 14 | import java.util.Optional; 15 | 16 | import static org.assertj.core.api.Assertions.assertThat; 17 | import static org.mockito.BDDMockito.given; 18 | import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 19 | 20 | /** 21 | * This class demonstrates how to test a controller using Spring Boot Test 22 | * (what makes it much closer to an Integration Test) 23 | * 24 | * @author moises.macero 25 | */ 26 | @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 27 | public class SuperHeroControllerSpringBootTest { 28 | 29 | @MockitoBean 30 | private SuperHeroRepository superHeroRepository; 31 | 32 | @Autowired 33 | private TestRestTemplate restTemplate; 34 | 35 | @Test 36 | public void canRetrieveByIdWhenExists() { 37 | // given 38 | given(superHeroRepository.getSuperHero(2)) 39 | .willReturn(new SuperHero("Rob", "Mannon", "RobotMan")); 40 | 41 | // when 42 | ResponseEntity superHeroResponse = restTemplate.getForEntity("/superheroes/2", SuperHero.class); 43 | 44 | // then 45 | assertThat(superHeroResponse.getStatusCode()).isEqualTo(HttpStatus.OK); 46 | assertThat(superHeroResponse.getBody().equals(new SuperHero("Rob", "Mannon", "RobotMan"))); 47 | } 48 | 49 | @Test 50 | public void canRetrieveByIdWhenDoesNotExist() { 51 | // given 52 | given(superHeroRepository.getSuperHero(2)) 53 | .willThrow(new NonExistingHeroException()); 54 | 55 | // when 56 | ResponseEntity superHeroResponse = restTemplate.getForEntity("/superheroes/2", SuperHero.class); 57 | 58 | // then 59 | assertThat(superHeroResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); 60 | assertThat(superHeroResponse.getBody()).isNull(); 61 | } 62 | 63 | @Test 64 | public void canRetrieveByNameWhenExists() { 65 | // given 66 | given(superHeroRepository.getSuperHero("RobotMan")) 67 | .willReturn(Optional.of(new SuperHero("Rob", "Mannon", "RobotMan"))); 68 | 69 | // when 70 | ResponseEntity superHeroResponse = restTemplate 71 | .getForEntity("/superheroes?name=RobotMan", SuperHero.class); 72 | 73 | // then 74 | assertThat(superHeroResponse.getStatusCode()).isEqualTo(HttpStatus.OK); 75 | assertThat(superHeroResponse.getBody()).isEqualTo(new SuperHero("Rob", "Mannon", "RobotMan")); 76 | } 77 | 78 | @Test 79 | public void canRetrieveByNameWhenDoesNotExist() { 80 | // given 81 | given(superHeroRepository.getSuperHero("RobotMan")) 82 | .willReturn(Optional.empty()); 83 | 84 | // when 85 | ResponseEntity superHeroResponse = restTemplate 86 | .getForEntity("/superheroes?name=RobotMan", SuperHero.class); 87 | 88 | // then 89 | assertThat(superHeroResponse.getStatusCode()).isEqualTo(HttpStatus.OK); 90 | assertThat(superHeroResponse.getBody()).isNull(); 91 | } 92 | 93 | @Test 94 | public void canCreateANewSuperHero() { 95 | // when 96 | ResponseEntity superHeroResponse = restTemplate.postForEntity("/superheroes", 97 | new SuperHero("Rob", "Mannon", "RobotMan"), SuperHero.class); 98 | 99 | // then 100 | assertThat(superHeroResponse.getStatusCode()).isEqualTo(HttpStatus.CREATED); 101 | } 102 | 103 | @Test 104 | public void headerIsPresent() throws Exception { 105 | // when 106 | ResponseEntity superHeroResponse = restTemplate.getForEntity("/superheroes/2", SuperHero.class); 107 | 108 | // then 109 | assertThat(superHeroResponse.getStatusCode()).isEqualTo(HttpStatus.OK); 110 | assertThat(superHeroResponse.getHeaders().get("X-SUPERHERO-APP")).containsOnly("super-header"); 111 | } 112 | 113 | } 114 | --------------------------------------------------------------------------------