4
4
.
.
3
3
.
.
2
2
T
T
e
e
s
s
t
t
-
-
R
R
e
e
q
q
u
u
e
e
s
s
t
t
-
-
U
U
R
R
L
L
M
M
a
a
p
p
p
p
i
i
n
n
g
g
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to test if Endpoint was called
for specific URL 404 Page Not Found
with required HTTP Request Method: GET, POST, ... 405 Method Not Allowed
Application Schema [Result]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
Syntax
MockHttpServletRequestBuilder request = get("/Hello"); //CREATE REQUEST
mockMvc.perform(request).andExpect(status().isOk()); //PERFORM REQUEST. CHECK STATUS.
http://localhost:8080/Hello?name=John
Tomcat
hello()
MyController
MyControllerTest
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_test_mockmvc_responsestatus (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside controllers package)
Create Test Class: MyControllerTest.java
MyController.java
package com.ivoronline.springboot_test_mockmvc_responsestatus.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@GetMapping("/Hello")
public String hello() {
return "Hello from Controller";
}
}
MyControllerTest.java
package com.ivoronline.springboot_test_mockmvc_responsestatus.controllers;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
class MyControllerTest {
@Autowired MockMvc mockMvc;
@Autowired MyController myController;
@Test
void hello() throws Exception {
//CREATE REQUEST
MockHttpServletRequestBuilder request = get("/Hello");
//PERFORM REQUEST
mockMvc.perform(request).andExpect(status().isOk());
}
}
R
R
e
e
s
s
u
u
l
l
t
t
http://localhost:8080/Hello
Wrong URL get("/Hello1?name=John")
MockHttpServletRequest:
HTTP Method = GET
Request URI = /Hello1
Parameters = {name1=[John]}
MockHttpServletResponse:
Status = 404
Error message = null
java.lang.AssertionError: Status expected:<200> but was:<404>
Expected :200
Actual :404 Page Not Found
Wrong Method post("/Hello?name=John")
MockHttpServletRequest:
HTTP Method = POST
Request URI = /Hello
Parameters = {name1=[John]}
MockHttpServletResponse:
Status = 405
Error message = Request method 'POST' not supported
Headers = [Allow:"GET"]
java.lang.AssertionError: Status expected:<200> but was:<405>
Expected :200
Actual :405 Method Not Allowed
Run Test Class: MyControllerTest.java
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>