Skip to content

Commit df6483e

Browse files
Adding junit 5 features sample code
1 parent ba34900 commit df6483e

9 files changed

Lines changed: 254 additions & 6 deletions

File tree

junit5-maven-sample/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<maven.compiler.source>17</maven.compiler.source>
1515
<maven.compiler.target>17</maven.compiler.target>
1616
<junit.version>5.9.2</junit.version>
17+
<assertj-core.version>3.24.2</assertj-core.version>
1718
</properties>
1819

1920
<dependencyManagement>
@@ -34,6 +35,12 @@
3435
<artifactId>junit-jupiter</artifactId>
3536
<scope>test</scope>
3637
</dependency>
38+
<dependency>
39+
<groupId>org.assertj</groupId>
40+
<artifactId>assertj-core</artifactId>
41+
<version>${assertj-core.version}</version>
42+
<scope>test</scope>
43+
</dependency>
3744
</dependencies>
3845

3946
<build>

junit5-maven-sample/src/main/java/com/sivalabs/jtme/PersonRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ public void deleteById(Long id) {
4545
}
4646
PERSON_DB.remove(id);
4747
}
48+
49+
public void deleteAll() {
50+
PERSON_DB.clear();
51+
}
4852
}

junit5-maven-sample/src/main/java/com/sivalabs/jtme/PersonService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ private void checkNotEmpty(String value, String message) {
4848
throw new RuntimeException(message);
4949
}
5050
}
51+
52+
public void deleteAll() {
53+
repo.deleteAll();
54+
}
5155
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.sivalabs.jtme;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.condition.EnabledOnJre;
5+
import org.junit.jupiter.api.condition.EnabledOnOs;
6+
import org.junit.jupiter.api.condition.JRE;
7+
import org.junit.jupiter.api.condition.OS;
8+
9+
class ConditionalTestExecutionDemoTest {
10+
11+
@Test
12+
@EnabledOnOs(OS.MAC)
13+
void shouldRunOnlyOnMacOS() {
14+
System.out.println("This is a test running on MacOS");
15+
}
16+
17+
@Test
18+
@EnabledOnOs(OS.WINDOWS)
19+
void shouldRunOnlyOnWindows() {
20+
System.out.println("This is a test running on Windows");
21+
}
22+
23+
@Test
24+
@EnabledOnJre(JRE.JAVA_17)
25+
void shouldRunOnlyOnJre17() {
26+
System.out.println("This is a test running on Java 17");
27+
}
28+
29+
@Test
30+
@EnabledOnJre(JRE.JAVA_21)
31+
void shouldRunOnlyOnJre21() {
32+
System.out.println("This is a test running on Java 21");
33+
}
34+
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.sivalabs.jtme;
2+
3+
import org.junit.jupiter.api.AfterAll;
4+
import org.junit.jupiter.api.AfterEach;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
class LifecycleCallbacksDemoTest {
10+
11+
@BeforeAll
12+
static void beforeAll() {
13+
System.out.println("--------beforeAll()---------");
14+
}
15+
16+
@AfterAll
17+
static void afterAll() {
18+
System.out.println("--------afterAll()---------");
19+
}
20+
21+
@BeforeEach
22+
void setUp() {
23+
System.out.println("--------setUp()---------");
24+
}
25+
26+
@AfterEach
27+
void tearDown() {
28+
System.out.println("--------tearDown()---------");
29+
}
30+
31+
@Test
32+
void test1() {
33+
System.out.println("------------test1------------");
34+
}
35+
36+
@Test
37+
void test2() {
38+
System.out.println("------------test2------------");
39+
}
40+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.sivalabs.jtme;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
import java.util.stream.Stream;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNotNull;
13+
import static org.junit.jupiter.params.provider.Arguments.arguments;
14+
15+
class PersonServiceParameterizedTests {
16+
PersonService personService;
17+
18+
@BeforeEach
19+
void setUp() {
20+
PersonRepository repo = new PersonRepository();
21+
personService = new PersonService(repo);
22+
}
23+
24+
@ParameterizedTest
25+
@CsvSource({
26+
"Siva1,siva1@gmail.com",
27+
"Siva2,siva2@gmail.com",
28+
"Siva3,siva3@gmail.com",
29+
})
30+
void shouldCreatePersonUsingCSVSuccessfully(String name, String email) {
31+
Person person = personService.create(new Person(null, name, email));
32+
assertNotNull(person.getId());
33+
assertEquals(name, person.getName());
34+
assertEquals(email, person.getEmail());
35+
}
36+
37+
@ParameterizedTest
38+
@MethodSource("personPropsProvider")
39+
void shouldCreatePersonSuccessfully(String name, String email) {
40+
Person person = personService.create(new Person(null, name, email));
41+
assertNotNull(person.getId());
42+
assertEquals(name, person.getName());
43+
assertEquals(email, person.getEmail());
44+
}
45+
46+
static Stream<Arguments> personPropsProvider() {
47+
return Stream.of(
48+
arguments("Siva", "siva@gmail.com"),
49+
arguments("Prasad", "prasad@gmail.com")
50+
);
51+
}
52+
53+
@ParameterizedTest
54+
@MethodSource("personObjectsProvider")
55+
void shouldCreatePersonWithObjectInputSuccessfully(Person personInput) {
56+
Person person = personService.create(personInput);
57+
assertNotNull(person.getId());
58+
assertEquals(personInput.getName(), person.getName());
59+
assertEquals(personInput.getEmail(), person.getEmail());
60+
}
61+
62+
static Stream<Arguments> personObjectsProvider() {
63+
return Stream.of(
64+
arguments(new Person(null, "Neha", "neha@gmail.com")),
65+
arguments(new Person(null, "Yuvaan", "yuvaan@gmail.com"))
66+
);
67+
}
68+
}
Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package com.sivalabs.jtme;
22

3+
import org.junit.jupiter.api.AfterEach;
34
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Nested;
46
import org.junit.jupiter.api.Test;
57

8+
import java.util.Optional;
9+
import java.util.UUID;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
613
import static org.junit.jupiter.api.Assertions.*;
714

815
class PersonServiceTest {
@@ -15,11 +22,52 @@ void setUp() {
1522
personService = new PersonService(repo);
1623
}
1724

18-
@Test
19-
void shouldCreatePerson() {
20-
Person person = personService.create(new Person(null, "Siva", "siva@gmail.com"));
21-
assertNotNull(person.getId());
22-
assertEquals("Siva", person.getName());
23-
assertEquals("siva@gmail.com", person.getEmail());
25+
@AfterEach
26+
void tearDown() {
27+
personService.deleteAll();
28+
}
29+
30+
@Nested
31+
class CreatePersonTests {
32+
@Test
33+
void shouldCreatePersonSuccessfully() {
34+
Person person = personService.create(new Person(null, "Siva", "siva@gmail.com"));
35+
assertNotNull(person.getId());
36+
assertEquals("Siva", person.getName());
37+
assertEquals("siva@gmail.com", person.getEmail());
38+
}
39+
40+
@Test
41+
void shouldFailToCreatePersonWithExistingEmail() {
42+
String email = UUID.randomUUID() +"@gmail.com";
43+
personService.create(new Person(null, "Siva", email));
44+
45+
assertThatThrownBy(()-> personService.create(new Person(null, "Siva", email)))
46+
.isInstanceOf(RuntimeException.class)
47+
.hasMessage("Person with email '"+email+"' already exists");
48+
}
49+
}
50+
51+
@Nested
52+
class FindPersonByEmailTests {
53+
String email;
54+
55+
@BeforeEach
56+
void setUp() {
57+
email = UUID.randomUUID() +"@gmail.com";
58+
personService.create(new Person(null, "Siva", email));
59+
}
60+
61+
@Test
62+
void shouldGetPersonByEmailWhenExists() {
63+
Optional<Person> optionalPerson = personService.findByEmail(email);
64+
assertThat(optionalPerson).isPresent();
65+
}
66+
67+
@Test
68+
void shouldGetEmptyWhenPersonByEmailNotExists() {
69+
Optional<Person> optionalPerson = personService.findByEmail("random@mail.com");
70+
assertThat(optionalPerson).isEmpty();
71+
}
2472
}
2573
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.sivalabs.jtme;
2+
3+
import org.junit.jupiter.api.Disabled;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Tag;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.TestInstance;
8+
import org.junit.jupiter.api.Timeout;
9+
10+
import java.util.concurrent.TimeUnit;
11+
12+
@TestInstance(value = TestInstance.Lifecycle.PER_CLASS)
13+
class TestInstanceLifecycleDemoTest {
14+
15+
@Test
16+
@DisplayName("Given a person to save with existing email then ti should throw Exception")
17+
@Tag("unit")
18+
@Timeout(value = 5, unit = TimeUnit.SECONDS)
19+
void test1() throws InterruptedException {
20+
System.out.println("test1:"+this.hashCode());
21+
Thread.sleep(3000);
22+
}
23+
24+
@Test
25+
@Tag("integration")
26+
@Disabled
27+
void test2() {
28+
System.out.println("test2:"+this.hashCode());
29+
}
30+
31+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#junit.jupiter.testinstance.lifecycle.default=per_class
2+
3+
# To execute top-level classes in parallel but methods in same thread
4+
#junit.jupiter.execution.parallel.enabled = true
5+
#junit.jupiter.execution.parallel.mode.default = same_thread
6+
#junit.jupiter.execution.parallel.mode.classes.default = concurrent
7+
8+
# To execute top-level classes sequentially but their methods in parallel
9+
#junit.jupiter.execution.parallel.enabled = true
10+
#junit.jupiter.execution.parallel.mode.default = concurrent
11+
#junit.jupiter.execution.parallel.mode.classes.default = same_thread

0 commit comments

Comments
 (0)