Skip to content

Commit 0c39241

Browse files
committed
add junit test
1 parent 04a3492 commit 0c39241

File tree

30 files changed

+1139
-31
lines changed

30 files changed

+1139
-31
lines changed

content/post/java-tutorial-2025-day-01.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Java Tutorial 2025 Day 01"
2+
title: "Java Tutorial 2025 Day 01: Hello CLI"
33
type: "post"
44
date: 2025-06-28T15:19:22+07:00
55
description: "Java Tutorial 2025 Day 01"
@@ -9,6 +9,10 @@ tags: ["java"]
99
image: /articles/java-tutorial-2025-day-01/001.png
1010
---
1111

12+
### SourceCode
13+
14+
- [Source code](https://github.com/RefactorEveryThing/ret_java_cli_001)
15+
1216
**Create Main App**
1317

1418
![Create main app](/articles/java-tutorial-2025-day-01/002.png)
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
---
2+
title: "Java Tutorial 2025 Day 02 - Write Test with JUnit5"
3+
type: "post"
4+
date: 2025-07-02T13:49:22+07:00
5+
description: "Java Tutorial 2025 Day 02"
6+
keywords: ["Java Tutorial 2025 Day 02"]
7+
categories: ["java"]
8+
tags: ["java"]
9+
image: /articles/java-tutorial-2025-day-02/001.png
10+
---
11+
12+
### Source Code
13+
14+
- [Source code](https://github.com/RefactorEveryThing/ret_java_cli_001)
15+
16+
Add package to **pom.xml**
17+
18+
- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
19+
20+
```xml
21+
<dependencies>
22+
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
23+
<dependency>
24+
<groupId>org.junit.jupiter</groupId>
25+
<artifactId>junit-jupiter-api</artifactId>
26+
<version>5.13.2</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
```
31+
32+
```java
33+
package com.nextjsvietnam;
34+
35+
import java.time.LocalDate;
36+
37+
public class DateUtils {
38+
public long dateDiff(LocalDate date1, LocalDate date2) {
39+
if (date1.isBefore(date2)){
40+
return date1.datesUntil(date2).count();
41+
}
42+
return date2.datesUntil(date1).count();
43+
}
44+
45+
public long currentWeek(LocalDate d) {
46+
// Week Number = floor((days since January 1st + starting weekday offset) / 7) + 1
47+
LocalDate firstDayOfYear = LocalDate.of(d.getYear(), 1, 1);
48+
var daysSinceFirstDayOfYear = firstDayOfYear.datesUntil(d).count();
49+
var startWeekDayOffset = (long)firstDayOfYear.getDayOfWeek().getValue();
50+
return (long)Math.floor((double)(daysSinceFirstDayOfYear + startWeekDayOffset)/7) + 1;
51+
}
52+
}
53+
```
54+
55+
Write tests
56+
57+
```java
58+
// src/test/java/com/nextjsvietnam/DateUtilsTest.java
59+
package com.nextjsvietnam;
60+
import java.time.LocalDate;
61+
import org.junit.jupiter.api.Test;
62+
63+
import static org.junit.jupiter.api.Assertions.*;
64+
65+
class DateUtilsTest {
66+
@Test
67+
void SameDateTestCase() {
68+
var dateUtils = new DateUtils();
69+
var d1 = LocalDate.of(2025, 1, 1);
70+
var d2 = LocalDate.of(2025, 1, 1);
71+
assertEquals(0, dateUtils.dateDiff(d1, d2));
72+
}
73+
74+
@Test
75+
void firstDateLessThanSecondDateTestCase() {
76+
var dateUtils = new DateUtils();
77+
var d1 = LocalDate.of(2024, 12, 31);
78+
var d2 = LocalDate.of(2025, 1, 1);
79+
assertEquals(1, dateUtils.dateDiff(d1, d2));
80+
}
81+
82+
@Test
83+
void firstDateGreaterThanSecondDateTestCase() {
84+
var dateUtils = new DateUtils();
85+
var d1 = LocalDate.of(2025, 1, 31);
86+
var d2 = LocalDate.of(2025, 1, 1);
87+
assertEquals(30, dateUtils.dateDiff(d1, d2));
88+
}
89+
90+
@Test
91+
void week27Test() {
92+
var dateUtils = new DateUtils();
93+
var today = LocalDate.now();
94+
assertEquals(27, dateUtils.currentWeek(today));
95+
}
96+
}
97+
```
98+
99+
### Run test
100+
101+
```sh
102+
mvn clean test
103+
104+
mvn -Dtest=DateUtilsTest test
105+
106+
mvn -Dtest=DateUtilsTest#week27Test test
107+
```
108+
109+
### Test report
110+
111+
- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-report-plugin
112+
- https://maven.apache.org/surefire/maven-surefire-report-plugin/usage.html
113+
114+
```xml
115+
<dependency>
116+
<groupId>org.junit.jupiter</groupId>
117+
<artifactId>junit-jupiter-api</artifactId>
118+
<version>5.13.2</version>
119+
<scope>test</scope>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.junit.jupiter</groupId>
123+
<artifactId>junit-jupiter-engine</artifactId>
124+
<version>5.13.2</version>
125+
<scope>test</scope>
126+
</dependency>
127+
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-report-plugin -->
128+
<dependency>
129+
<groupId>org.apache.maven.plugins</groupId>
130+
<artifactId>maven-surefire-report-plugin</artifactId>
131+
<version>3.5.3</version>
132+
</dependency>
133+
```
134+
135+
```sh
136+
mvn site
137+
mvn surefire-report:report
138+
```
139+
140+
![Report overview](/articles/java-tutorial-2025-day-02/002.png)
141+
142+
Add environment
143+
144+
![env](/articles/java-tutorial-2025-day-02/003.png)
145+
146+
### Repeat Tests
147+
148+
```java
149+
@DisplayName("TC1: Generate a number from specific range")
150+
@RepeatedTest(
151+
value = 50,
152+
name = "Repeating {displayName} {currentRepetition}/{totalRepetitions}"
153+
)
154+
155+
void generateNumberTest() {
156+
int start = 50, end = 100;
157+
Random random = new Random();
158+
var randomNumber = random.nextInt(50, end + 1);
159+
assertTrue( randomNumber >= start && randomNumber <= end);
160+
}
161+
```
162+
163+
### Parameterized Tests
164+
165+
```xml
166+
<dependency>
167+
<groupId>org.junit.jupiter</groupId>
168+
<artifactId>junit-jupiter-params</artifactId>
169+
<version>5.13.2</version>
170+
<scope>test</scope>
171+
</dependency>
172+
```
173+
174+
```java
175+
@DisplayName("Parameter Tests")
176+
@ParameterizedTest(name = "{displayName} with str={arguments}")
177+
@ValueSource(strings = {"a", "b", "c"})
178+
void parametersTest(String str) {
179+
assertFalse(str.isBlank());
180+
}
181+
182+
@DisplayName("MethodSource")
183+
@ParameterizedTest()
184+
@MethodSource("phoneNumberList")
185+
void methodSourceTest(String str) {
186+
assertFalse(str.isEmpty());
187+
}
188+
189+
private static List<String> phoneNumberList(){
190+
return Arrays.asList("012345678", "123456789");
191+
}
192+
193+
@DisplayName("CsvFileSource")
194+
@ParameterizedTest()
195+
@CsvFileSource(resources = "/examples.csv", numLinesToSkip = 1)
196+
void csvFileSourceTest(String input, String expected) {
197+
String newValue = input.toUpperCase();
198+
assertEquals(newValue, expected);
199+
}
200+
```
201+
202+
![test resource folder](/articles/java-tutorial-2025-day-02/004.png)
203+
204+
### Nested Tests
205+
206+
```java
207+
package com.nextjsvietnam;
208+
import java.lang.reflect.Array;
209+
import java.time.LocalDate;
210+
import java.util.Arrays;
211+
import java.util.List;
212+
import java.util.Random;
213+
214+
import org.junit.jupiter.api.*;
215+
import org.junit.jupiter.params.ParameterizedTest;
216+
import org.junit.jupiter.params.provider.CsvFileSource;
217+
import org.junit.jupiter.params.provider.MethodSource;
218+
import org.junit.jupiter.params.provider.ValueSource;
219+
import org.junit.jupiter.params.provider.ValueSources;
220+
221+
import static org.junit.jupiter.api.Assertions.*;
222+
223+
class DateUtilsTest {
224+
@Test
225+
@DisplayName("TC1: Same Date")
226+
void SameDateTestCase() {
227+
var dateUtils = new DateUtils();
228+
var d1 = LocalDate.of(2025, 1, 1);
229+
var d2 = LocalDate.of(2025, 1, 1);
230+
assertEquals(0, dateUtils.dateDiff(d1, d2));
231+
}
232+
233+
@Test
234+
@DisplayName("TC2: 1st date less than 2nd date")
235+
void firstDateLessThanSecondDateTestCase() {
236+
var dateUtils = new DateUtils();
237+
var d1 = LocalDate.of(2024, 12, 31);
238+
var d2 = LocalDate.of(2025, 1, 1);
239+
assertEquals(1, dateUtils.dateDiff(d1, d2));
240+
}
241+
242+
@Test
243+
@DisplayName("TC3: 1st date greater than 2nd date")
244+
void firstDateGreaterThanSecondDateTestCase() {
245+
var dateUtils = new DateUtils();
246+
var d1 = LocalDate.of(2025, 1, 31);
247+
var d2 = LocalDate.of(2025, 1, 1);
248+
assertEquals(30, dateUtils.dateDiff(d1, d2));
249+
}
250+
251+
@Test
252+
@DisplayName("TC1: Week27")
253+
void week27Test() {
254+
var dateUtils = new DateUtils();
255+
var today = LocalDate.of(2025, 7, 2);
256+
assertEquals(27, dateUtils.currentWeek(today));
257+
}
258+
259+
@Test
260+
@DisplayName("TC1: Assumption Sample")
261+
void assumptionSampleTest() {
262+
System.out.println(System.getProperty("ENV"));
263+
Assumptions.assumeTrue("DEV".equals(System.getProperty("ENV")));
264+
assertEquals("DEV", System.getProperty("ENV"));
265+
}
266+
267+
@DisplayName("TC1: Generate a number from specific range")
268+
@RepeatedTest(
269+
value = 50,
270+
name = "Repeating {displayName} {currentRepetition}/{totalRepetitions}"
271+
)
272+
273+
void generateNumberTest() {
274+
int start = 50, end = 100;
275+
Random random = new Random();
276+
var randomNumber = random.nextInt(50, end + 1);
277+
assertTrue( randomNumber >= start && randomNumber <= end);
278+
}
279+
280+
281+
@Nested
282+
class ExampleNestedTestOrGroupTests {
283+
@DisplayName("Parameter Tests")
284+
@ParameterizedTest(name = "{displayName} with str={arguments}")
285+
@ValueSource(strings = {"a", "b", "c"})
286+
void parametersTest(String str) {
287+
assertFalse(str.isBlank());
288+
}
289+
290+
@DisplayName("MethodSource")
291+
@ParameterizedTest()
292+
@MethodSource("phoneNumberList")
293+
void methodSourceTest(String str) {
294+
assertFalse(str.isEmpty());
295+
}
296+
297+
private static List<String> phoneNumberList(){
298+
return Arrays.asList("012345678", "123456789");
299+
}
300+
301+
@DisplayName("CsvFileSource")
302+
@ParameterizedTest()
303+
@CsvFileSource(resources = "/examples.csv", numLinesToSkip = 1)
304+
void csvFileSourceTest(String input, String expected) {
305+
String newValue = input.toUpperCase();
306+
assertEquals(newValue, expected);
307+
}
308+
}
309+
}
310+
```
259 KB
Loading
259 KB
Loading
518 KB
Loading
305 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
![alt text](image.png)
2+
![alt text](image.png)
3+
![alt text](image.png)![alt text](image.png)
305 KB
Loading

public/categories/index.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
<description>Recent content in Categories on Useful NodeJS Tricks, JavaScript Tips, Tricks and Best Practices</description>
77
<generator>Hugo</generator>
88
<language>en-us</language>
9-
<lastBuildDate>Sat, 28 Jun 2025 15:19:22 +0700</lastBuildDate>
9+
<lastBuildDate>Wed, 02 Jul 2025 13:49:22 +0700</lastBuildDate>
1010
<atom:link href="http://localhost:1313/categories/index.xml" rel="self" type="application/rss+xml" />
1111
<item>
1212
<title>Java</title>
1313
<link>http://localhost:1313/categories/java/</link>
14-
<pubDate>Sat, 28 Jun 2025 15:19:22 +0700</pubDate>
14+
<pubDate>Wed, 02 Jul 2025 13:49:22 +0700</pubDate>
1515
<guid>http://localhost:1313/categories/java/</guid>
1616
<description></description>
1717
</item>

public/categories/java/index.html

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,39 @@ <h1 class="hero-section-heading">
251251
</section>
252252
<section class="container section grid md:grid-cols-4 gap-8">
253253

254+
<div class="card card-post-default">
255+
<a href="http://localhost:1313/post/java-tutorial-2025-day-02/"
256+
><img
257+
class="card-post-image"
258+
src="http://localhost:1313/articles/java-tutorial-2025-day-02/001.png"
259+
alt="Java Tutorial 2025 Day 02 - Write Test with JUnit5"
260+
/></a>
261+
<div class="px-6 py-4">
262+
<h2 class="card-post-title"><a href="http://localhost:1313/post/java-tutorial-2025-day-02/">Java Tutorial 2025 Day 02 - Write Test with JUnit5</a></h2>
263+
<div>
264+
265+
<a class="category-tag category-java" href="http://localhost:1313/categories/java">java</a>
266+
267+
</div>
268+
<div>
269+
<span class="card-post-date"
270+
>02 Jul, 2025</span
271+
>
272+
</div>
273+
<div class="card-post-description">Java Tutorial 2025 Day 02</div>
274+
</div>
275+
</div>
276+
277+
254278
<div class="card card-post-default">
255279
<a href="http://localhost:1313/post/java-tutorial-2025-day-01/"
256280
><img
257281
class="card-post-image"
258282
src="http://localhost:1313/articles/java-tutorial-2025-day-01/001.png"
259-
alt="Java Tutorial 2025 Day 01"
283+
alt="Java Tutorial 2025 Day 01: Hello CLI"
260284
/></a>
261285
<div class="px-6 py-4">
262-
<h2 class="card-post-title"><a href="http://localhost:1313/post/java-tutorial-2025-day-01/">Java Tutorial 2025 Day 01</a></h2>
286+
<h2 class="card-post-title"><a href="http://localhost:1313/post/java-tutorial-2025-day-01/">Java Tutorial 2025 Day 01: Hello CLI</a></h2>
263287
<div>
264288

265289
<a class="category-tag category-java" href="http://localhost:1313/categories/java">java</a>
@@ -488,7 +512,7 @@ <h4 class="footer-heading">Cheatsheet</h4>
488512
Powered by <a class="font-bold text-primary" href="https://gohugo.io/">Go Hugo</a> - Made
489513
with love from <a class="font-bold text-primary" href="http://localhost:1313/">NextJSVietNam</a> -
490514
Build
491-
Version 2025-06-28 15:19:22 &#43;0700 &#43;07
515+
Version 2025-07-02 13:49:22 &#43;0700 &#43;07
492516
</div>
493517
</div>
494518
</footer>

0 commit comments

Comments
 (0)