Skip to content

Commit 3b10f1c

Browse files
committed
use junit 5 tests
1 parent 3172fc9 commit 3b10f1c

25 files changed

Lines changed: 268 additions & 1 deletion

File tree

JavaTMP-SpringBoot-Modules/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ include 'spring-boot-management:spring-boot-actuator'
3636
include 'spring-boot-miscellaneous:spring-boot-validation'
3737
include 'spring-boot-miscellaneous:spring-boot-itext'
3838
include 'spring-boot-miscellaneous:spring-boot-JasperReports'
39+
include 'spring-boot-miscellaneous:spring-boot-testing'
3940

4041
include 'spring-boot-cache:spring-boot-cache-simple'
4142

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
bootJar {
3+
enabled = true
4+
}
5+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Spring Boot Testing Miscellaneous Module
2+
3+
## A Guide to JUnit 5
4+
* JUnit is one of the most popular unit-testing frameworks in the Java ecosystem.
5+
* JUnit 5 Test can be found in class
6+
`com.javatmp.testing.JUnitTest`
7+
8+
9+
## References
10+
- [https://junit.org/junit5/](https://junit.org/junit5/)
11+
- [https://www.baeldung.com/junit-5](https://www.baeldung.com/junit-5)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javatmp.testing;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.CommandLineRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
9+
/**
10+
* Spring Boot Main Runner Class
11+
*/
12+
@SpringBootApplication
13+
@Slf4j
14+
public class DemoApplication {
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(DemoApplication.class, args);
18+
}
19+
20+
@Bean
21+
public CommandLineRunner springBootMain() throws Exception {
22+
return args -> {
23+
log.info("*** Start Spring Boot Project ***");
24+
};
25+
}
26+
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{ISO8601} %highlight{%-5level} [%t] %C{1.}: %msg%n%throwable"/>
6+
</Console>
7+
8+
<RollingFile name="RollingFile" bufferedIO="true"
9+
fileName="./logs/app-log.log"
10+
filePattern="./logs/$${date:yyyy-MM}/app-%d{dd-MMMM-yyyy}-%i.log.gz">
11+
<PatternLayout>
12+
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
13+
</PatternLayout>
14+
<Policies>
15+
<!-- rollover on startup, daily and when the file reaches 10 MegaBytes -->
16+
<OnStartupTriggeringPolicy/>
17+
<SizeBasedTriggeringPolicy size="250 MB"/>
18+
<TimeBasedTriggeringPolicy/>
19+
</Policies>
20+
<DefaultRolloverStrategy max="100"/>
21+
</RollingFile>
22+
</Appenders>
23+
24+
<Loggers>
25+
<!-- LOG everything at INFO level -->
26+
<Root level="debug">
27+
<AppenderRef ref="Console"/>
28+
<AppenderRef ref="RollingFile"/>
29+
</Root>
30+
<Logger name="org.hibernate" level="debug" additivity="false">
31+
<AppenderRef ref="Console"/>
32+
<AppenderRef ref="RollingFile"/>
33+
</Logger>
34+
<!-- org.hibernate.SQL-->
35+
<Logger name="org.springframework" level="debug" additivity="false">
36+
<AppenderRef ref="Console"/>
37+
<AppenderRef ref="RollingFile"/>
38+
</Logger>
39+
<Logger name="com.zaxxer.hikari" level="info" additivity="false">
40+
<AppenderRef ref="Console"/>
41+
<AppenderRef ref="RollingFile"/>
42+
</Logger>
43+
</Loggers>
44+
45+
</Configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.javatmp.testing;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
7+
@SpringBootTest
8+
@Slf4j
9+
class DemoApplicationTests {
10+
11+
@Test
12+
void welcomeLogTest1() {
13+
log.debug("*** SpringBoot Test Context Load 1 ***");
14+
}
15+
16+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.javatmp.testing;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.jupiter.api.*;
5+
6+
import java.util.stream.Stream;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
import static org.junit.jupiter.api.Assumptions.*;
10+
11+
@Slf4j
12+
class JUnitTest {
13+
@Test
14+
void test() {
15+
log.debug("*** Test Context Load 1 ***");
16+
}
17+
18+
/**
19+
* <code>@BeforeAll</code> denotes that the annotated method will be executed
20+
* before all test methods in the current class.
21+
* This annotation needs to be static
22+
*/
23+
@BeforeAll
24+
static void setup() {
25+
log.info("@BeforeAll - executes once before all test methods in this class");
26+
}
27+
28+
/**
29+
* <code>@BeforeEach</code> denotes that the annotated method will be executed
30+
* before each test method
31+
*/
32+
@BeforeEach
33+
void init() {
34+
log.info("@BeforeEach - executes before each test method in this class");
35+
}
36+
37+
/**
38+
* <code>@DisplayName</code> defines custom display name for a test class
39+
* or a test method
40+
*/
41+
@DisplayName("Display Name Annotation")
42+
@Test
43+
void testWithDisplayNameTest() {
44+
log.info("Test With custom display name using @DisplayName");
45+
}
46+
47+
/**
48+
* <code>@Disable</code> it is used to disable a test class or method
49+
*/
50+
@Test
51+
@Disabled("Not implemented yet")
52+
void tobeImplemented() {
53+
log.info("@Disabled it is used to disable a test class or method");
54+
}
55+
56+
/**
57+
* <code>@AfterEach</code> denotes that the annotated method will be executed
58+
* after each test method
59+
*/
60+
@AfterEach
61+
void tearDown() {
62+
log.info("@AfterEach executed after each test method.");
63+
}
64+
65+
/**
66+
* <code>@AfterAll</code> denotes that the annotated method will be executed
67+
* after all test methods in the current class
68+
*/
69+
@AfterAll
70+
static void done() {
71+
log.info("@AfterAll executed after all test methods.");
72+
}
73+
74+
@Test
75+
void assertionTest() {
76+
log.info("start assertTrue assertion");
77+
assertTrue(Stream.of(1, 2, 3)
78+
.mapToInt(i -> i)
79+
.sum() > 5, () -> "Sum should be greater than 5");
80+
}
81+
82+
@Test
83+
void groupAssertions() {
84+
int[] numbers = {0, 1, 2, 3, 4};
85+
assertAll("numbers",
86+
() -> assertEquals(numbers[0], 1),
87+
() -> assertEquals(numbers[3], 3),
88+
() -> assertEquals(numbers[4], 1)
89+
);
90+
}
91+
92+
@Test
93+
void trueAssumption() {
94+
assumeTrue(5 > 1);
95+
assertEquals(5 + 2, 7);
96+
}
97+
98+
@Test
99+
void falseAssumption() {
100+
assumeFalse(5 < 1);
101+
assertEquals(5 + 2, 7);
102+
}
103+
104+
@Test
105+
void assumptionThat() {
106+
String someString = "Just a string";
107+
assumingThat(
108+
someString.equals("Just a string"),
109+
() -> assertEquals(2 + 2, 4)
110+
);
111+
}
112+
113+
@Test
114+
void shouldThrowException() {
115+
Throwable exception = assertThrows(UnsupportedOperationException.class, () -> {
116+
throw new UnsupportedOperationException("Not supported");
117+
});
118+
assertEquals(exception.getMessage(), "Not supported");
119+
}
120+
121+
@Test
122+
void assertThrowsException() {
123+
String str = null;
124+
assertThrows(IllegalArgumentException.class, () -> {
125+
Integer.valueOf(str);
126+
});
127+
}
128+
129+
}

0 commit comments

Comments
 (0)