4
4
.
.
1
1
.
.
1
1
0
0
a
a
s
s
s
s
e
e
r
r
t
t
T
T
h
h
r
r
o
o
w
w
s
s
(
(
)
)
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to use different assert() Methods which are used to assert/evaluate/compare provided values.
If values are don't match then Test ends in Failure at the first assert that didn't pass.
Application Schema [Result]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
http://localhost:8080/Hello
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_junit (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_junit_exception.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/Hello")
public String hello() throws Exception {
if (1 == 1) { throw new IllegalArgumentException("Argument is missing"); }
return "Hello from Controller";
}
}
MyControllerTest.java
package com.ivoronline.springboot_junit_exception.controllers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class MyControllerTest {
@Autowired MyController myController;
//===============================================================
// assertThrows() - Lambda
//===============================================================
@Test
void hello1() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> myController.hello() ); //Check
Type
assertEquals("Argument is missing", exception.getMessage()); //Check
Message
}
//===============================================================
// assertThrows() - Executable
//===============================================================
@Test
void hello2() {
//CHECK TYPE
Exception exception = assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() throws Throwable {
myController.hello();
}
});
//CHECK MESSAGE
assertEquals("Argument is missing", exception.getMessage());
}
//===============================================================
// TRY - CATCH
//===============================================================
@Test
void hello3() {
try {
myController.hello();
fail(); //Fail if no Exception was thrown
}
catch (IllegalArgumentException exception) { //Catch Excpetions of expected Type
assertEquals("Argument is missing", exception.getMessage()); //Fail if Message is wrong
}
catch(Exception exception) {
fail(); //Fail if Exception is of wrong Type
}
}
}
R
R
e
e
s
s
u
u
l
l
t
t
http://localhost:8080/Hello
Run Test Class: PersonTest
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>