4
4
.
.
1
1
.
.
7
7
@
@
D
D
i
i
s
s
a
a
b
b
l
l
e
e
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to use @Disable Annotation to skip specific Test Methods which is useful when you
want to concentrate on other Test Methods (and don't want to wait for disabled Test Methods to execute)
know that Test Method would fail because it is testing functionality that is still under development or being debugged
In this example we will create two Test Methods to test each Controller's Endpoint.
But one Test Method will be @Disable and therefore will not get executed.
Application Schema [Result]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
http://localhost:8080/TestMe
Tomcat
testMe()
MyController
MyControllerTest
http://localhost:8080/DisableMe
disableMe()
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_junit_test_controller (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_ignore.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("/TestMe")
public String testMe() {
return "Hello from TestMe";
}
@ResponseBody
@RequestMapping("/DisableMe")
public String disableMe() {
return "Hello from DisableMe";
}
}
MyControllerTest.java
package com.ivoronline.springboot_junit_ignore.controllers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
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;
@Test
void testMe() {
String result = myController.testMe();
assertEquals("Hello from TestMe", result);
}
@Disabled
void disableMe() {
String result = myController.disableMe();
assertEquals("Hello from DisableMe", result);
}
}
R
R
e
e
s
s
u
u
l
l
t
t
http://localhost:8080/TestMe http://localhost:8080/DisableMe
Run Test Class: MyControllerTest.java
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>