Skip to content

Commit 803b654

Browse files
committed
Optimised, refactored and improved tests and achieved 100% class and method test coverage; Added jupiter test suite for controller test execution order; Updated junit-jupiter version to 5.11.0
1 parent 1bad055 commit 803b654

14 files changed

+505
-248
lines changed

pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<spring-version>3.3.2</spring-version>
4141
<spring-integration-version>6.3.2</spring-integration-version>
4242
<jmockit-version>1.49.2</jmockit-version>
43-
<junit-jupiter-version>5.10.2</junit-jupiter-version>
43+
<junit-jupiter-version>5.11.0</junit-jupiter-version>
4444
<application.version>${project.version}</application.version>
4545
</properties>
4646

@@ -167,6 +167,18 @@
167167
<version>${junit-jupiter-version}</version>
168168
<scope>test</scope>
169169
</dependency>
170+
<dependency>
171+
<groupId>org.junit.jupiter</groupId>
172+
<artifactId>junit-jupiter-engine</artifactId>
173+
<version>${junit-jupiter-version}</version>
174+
<scope>test</scope>
175+
</dependency>
176+
<dependency>
177+
<groupId>org.junit.platform</groupId>
178+
<artifactId>junit-platform-suite-api</artifactId>
179+
<version>1.11.0</version>
180+
<scope>test</scope>
181+
</dependency>
170182
<dependency>
171183
<groupId>org.mockito</groupId>
172184
<artifactId>mockito-junit-jupiter</artifactId>
@@ -249,6 +261,11 @@
249261
<goals>
250262
<goal>report</goal>
251263
</goals>
264+
<configuration>
265+
<excludes>
266+
<exclude>dev/markodojkic/softwaredevelopmentsimulation/SoftwareDevelopmentSimulationApp.class</exclude>
267+
</excludes>
268+
</configuration>
252269
</execution>
253270
</executions>
254271
</plugin>
@@ -259,6 +276,7 @@
259276
<configuration>
260277
<argLine>
261278
${argLine} <!-- Needed for jacoco to run -->
279+
-Dspring.profiles.active=test
262280
-Xshare:off
263281
-XX:+EnableDynamicAgentLoading
264282
-javaagent:${user.home}/.m2/repository/com/github/hazendaz/jmockit/jmockit/${jmockit-version}/jmockit-${jmockit-version}.jar

src/main/java/dev/markodojkic/softwaredevelopmentsimulation/SoftwareDevelopmentSimulationApp.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ public static void main(String[] args) {
3232
} catch (IOException e) {
3333
getIGateways().sendToInfo(String.format("Welcome to Software development simulator™ Developed by Ⓒ Marko Dojkić 2024%nSize occupied by predefined data is: %.2f KB%nI hope you will enjoy using my spring integration web-based application", 0.00));
3434
}
35+
36+
//TODO: Implement swagger, explain controllers and write readme
3537
}
3638
}

src/test/java/dev/markodojkic/softwaredevelopmentsimulation/test/BaseTaskTest.java

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44
import dev.markodojkic.softwaredevelopmentsimulation.enums.DeveloperType;
55
import dev.markodojkic.softwaredevelopmentsimulation.model.BaseTask;
66
import dev.markodojkic.softwaredevelopmentsimulation.model.Developer;
7-
import org.junit.jupiter.api.BeforeAll;
7+
import dev.markodojkic.softwaredevelopmentsimulation.test.Config.GlobalSetupExtension;
88
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.springframework.boot.test.context.SpringBootTest;
911

1012
import java.time.ZonedDateTime;
1113

12-
import static dev.markodojkic.softwaredevelopmentsimulation.util.DataProvider.setupDataProvider;
1314
import static org.junit.jupiter.api.Assertions.*;
1415
import static org.mockito.Mockito.mock;
1516
import static org.mockito.Mockito.when;
1617

18+
@SpringBootTest
19+
@ExtendWith(GlobalSetupExtension.class)
1720
class BaseTaskTest {
18-
@BeforeAll
19-
public static void preSetup(){
20-
setupDataProvider(true);
21-
}
22-
2321
@Test
2422
void when_noArgsConstructorIsCalled_correctValuesAreSetAsDefault() {
2523
BaseTask task = new BaseTask();
@@ -34,10 +32,11 @@ void when_noArgsConstructorIsCalled_correctValuesAreSetAsDefault() {
3432
}
3533

3634
@Test
37-
void when_allArgsConstructorIsCalled_correctValuesAreSet() {
35+
void when_allArgsConstructorIsCalled_correctValuesAreSetAndToStringIsAdequate() {
3836
Priority priority = Priority.NORMAL;
39-
Developer assignee = mock(Developer.class);
40-
Developer reporter = mock(Developer.class);
37+
Developer reporter = new Developer("John Doe", "1234567858123", DeveloperType.SENIOR_DEVELOPER, false, 2L);
38+
Developer assignee = new Developer("Alice Johnson", "9876543210987", DeveloperType.INTERN_DEVELOPER, true, 1L);
39+
4140
ZonedDateTime createdOn = ZonedDateTime.now();
4241

4342
BaseTask task = new BaseTask("1", "Task Name", "Task Description", priority, assignee, reporter, createdOn);
@@ -49,6 +48,19 @@ void when_allArgsConstructorIsCalled_correctValuesAreSet() {
4948
assertEquals(assignee, task.getAssignee());
5049
assertEquals(reporter, task.getReporter());
5150
assertEquals(createdOn, task.getCreatedOn());
51+
52+
// Assertion for toString method
53+
String expectedToString = "BaseTask{" +
54+
"id='1'," +
55+
" name='Task Name'," +
56+
" description='Task Description'," +
57+
" priority=NORMAL," +
58+
" assignee='" + assignee.getDisplayName() +
59+
"', reporter='" + reporter.getDisplayName() +
60+
"', createdOn=" + createdOn +
61+
'}';
62+
63+
assertEquals(expectedToString, task.toString());
5264
}
5365

5466
@Test
@@ -104,34 +116,6 @@ void when_equalsOrHashCodeIsCalled_onEqualObjectAreSame_onNonEqualObjectsAreDiff
104116
// Test with null
105117
assertNotNull(task1);
106118
}
107-
108-
@Test
109-
void testToString() {
110-
Priority priority = Priority.NORMAL;
111-
Developer assignee = mock(Developer.class);
112-
Developer reporter = mock(Developer.class);
113-
ZonedDateTime createdOn = ZonedDateTime.now();
114-
115-
BaseTask task = new BaseTask("1", "Task Name", "Task Description", priority, null, null, createdOn);
116-
117-
String expectedToString = "BaseTask{id='1', name='Task Name', description='Task Description', priority=" + priority +
118-
", assignee='UNASSIGNED', reporter='UNASSIGNED', createdOn=" + createdOn + '}';
119-
120-
assertEquals(expectedToString, task.toString());
121-
122-
task.setAssignee(assignee);
123-
task.setReporter(reporter);
124-
125-
expectedToString = "BaseTask{id='1', name='Task Name', description='Task Description', priority=" + priority +
126-
", assignee='" + assignee.getDisplayName() + "', reporter='" + reporter.getDisplayName() +
127-
"', createdOn=" + createdOn + '}';
128-
129-
assertEquals(expectedToString, task.toString());
130-
131-
task.setReporter(null);
132-
133-
assertEquals(expectedToString, task.toString());
134-
}
135119

136120
@Test
137121
void when_gettersAndSettersAreCalled_valuesAreCorrectlyRetrievedOrSet() {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dev.markodojkic.softwaredevelopmentsimulation.test.Config;
2+
3+
import dev.markodojkic.softwaredevelopmentsimulation.util.Utilities;
4+
import org.apache.commons.io.FileUtils;
5+
import org.junit.jupiter.api.extension.AfterAllCallback;
6+
import org.junit.jupiter.api.extension.BeforeAllCallback;
7+
import org.junit.jupiter.api.extension.ExtensionContext;
8+
9+
import static dev.markodojkic.softwaredevelopmentsimulation.util.DataProvider.setupDataProvider;
10+
11+
public class GlobalSetupExtension implements BeforeAllCallback, AfterAllCallback {
12+
@Override
13+
public void beforeAll(ExtensionContext extensionContext) {
14+
setupDataProvider();
15+
}
16+
17+
@Override
18+
public void afterAll(ExtensionContext context) throws Exception {
19+
FileUtils.deleteDirectory(Utilities.getCurrentApplicationDataPath().toAbsolutePath().toFile());
20+
}
21+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package dev.markodojkic.softwaredevelopmentsimulation.test.Config;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import dev.markodojkic.softwaredevelopmentsimulation.DeveloperImpl;
5+
import dev.markodojkic.softwaredevelopmentsimulation.ProjectManagerImpl;
6+
import dev.markodojkic.softwaredevelopmentsimulation.config.MiscellaneousConfig;
7+
import dev.markodojkic.softwaredevelopmentsimulation.config.SpringIntegrationMessageChannelsConfig;
8+
import dev.markodojkic.softwaredevelopmentsimulation.flow.FileHandlingFlow;
9+
import dev.markodojkic.softwaredevelopmentsimulation.flow.MQTTFlow;
10+
import dev.markodojkic.softwaredevelopmentsimulation.flow.PrintoutFlow;
11+
import dev.markodojkic.softwaredevelopmentsimulation.interfaces.IGateways;
12+
import dev.markodojkic.softwaredevelopmentsimulation.model.DevelopmentTeamCreationParameters;
13+
import dev.markodojkic.softwaredevelopmentsimulation.transformer.PrinterTransformer;
14+
import dev.markodojkic.softwaredevelopmentsimulation.util.Utilities;
15+
import dev.markodojkic.softwaredevelopmentsimulation.web.DevelopersPageController;
16+
import dev.markodojkic.softwaredevelopmentsimulation.web.MainController;
17+
import io.moquette.broker.Server;
18+
import io.moquette.broker.config.MemoryConfig;
19+
import org.junit.jupiter.api.AfterAll;
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
import org.mockito.junit.jupiter.MockitoExtension;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.beans.factory.annotation.Qualifier;
26+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
27+
import org.springframework.boot.test.context.SpringBootTest;
28+
import org.springframework.test.context.ContextConfiguration;
29+
30+
import java.io.IOException;
31+
import java.nio.file.Files;
32+
import java.util.Properties;
33+
34+
import static dev.markodojkic.softwaredevelopmentsimulation.util.DataProvider.setupDataProvider;
35+
import static dev.markodojkic.softwaredevelopmentsimulation.util.DataProvider.updateDevelopmentTeamsSetup;
36+
import static org.junit.jupiter.api.Assertions.assertNotNull;
37+
38+
@SpringBootTest
39+
@AutoConfigureMockMvc // Enables MockMvc with full context
40+
//@ComponentScan(basePackages = "dev.markodojkic.softwaredevelopmentsimulation")
41+
@ContextConfiguration(classes = { MiscellaneousConfig.class, TestConfig.class, SpringIntegrationMessageChannelsConfig.class, MQTTFlow.class, PrintoutFlow.class, FileHandlingFlow.class, PrinterTransformer.class, DeveloperImpl.class, ProjectManagerImpl.class, MainController.class, DevelopersPageController.class })
42+
@ExtendWith(MockitoExtension.class)
43+
@ExtendWith(GlobalSetupExtension.class)
44+
public abstract class SoftwareDevelopmentSimulationAppBaseTest {
45+
private static Server mqttServer;
46+
47+
@Autowired
48+
@Qualifier("IGateways")
49+
private IGateways iGateways;
50+
51+
@Autowired
52+
private ObjectMapper objectMapper;
53+
54+
@BeforeAll
55+
public static void preSetup() throws Exception {
56+
Properties properties = new Properties();
57+
properties.setProperty("port", "21681");
58+
properties.setProperty("host", "0.0.0.0");
59+
properties.setProperty("password_file", ""); //No password
60+
properties.setProperty("allow_anonymous", "true");
61+
properties.setProperty("netty.mqtt.message_size", "102400");
62+
63+
MemoryConfig memoryConfig = new MemoryConfig(properties);
64+
mqttServer = new Server();
65+
mqttServer.startServer(memoryConfig); //In memory MQTT server
66+
}
67+
68+
@AfterAll
69+
public static void tearDown() {
70+
mqttServer.stopServer();
71+
}
72+
73+
@BeforeEach
74+
public void setup() throws IOException {
75+
assertNotNull(iGateways);
76+
Utilities.setIGateways(iGateways);
77+
assertNotNull(Utilities.getIGateways());
78+
Utilities.setObjectMapper(objectMapper);
79+
setupDataProvider();
80+
updateDevelopmentTeamsSetup(new DevelopmentTeamCreationParameters());
81+
82+
Files.createDirectories(Utilities.getCurrentApplicationDataPath());
83+
Files.createDirectories(Utilities.getCurrentApplicationLogsPath());
84+
}
85+
86+
@BeforeEach
87+
public void setUp() {
88+
// Common setup code here
89+
// e.g., initializing beans, setting up data
90+
setupDataProvider();
91+
updateDevelopmentTeamsSetup(new DevelopmentTeamCreationParameters());
92+
}
93+
}

src/test/java/dev/markodojkic/softwaredevelopmentsimulation/test/DataProviderTest.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,16 @@
33
import dev.markodojkic.softwaredevelopmentsimulation.enums.DeveloperType;
44
import dev.markodojkic.softwaredevelopmentsimulation.model.DevelopmentTeamCreationParameters;
55
import dev.markodojkic.softwaredevelopmentsimulation.model.Developer;
6+
import dev.markodojkic.softwaredevelopmentsimulation.test.Config.SoftwareDevelopmentSimulationAppBaseTest;
67
import org.apache.logging.log4j.util.Strings;
7-
import org.junit.jupiter.api.BeforeAll;
88
import org.junit.jupiter.api.Test;
9-
import org.springframework.boot.test.context.SpringBootTest;
109

1110
import java.util.List;
1211

1312
import static dev.markodojkic.softwaredevelopmentsimulation.util.DataProvider.*;
1413
import static org.junit.jupiter.api.Assertions.*;
1514

16-
@SpringBootTest
17-
class DataProviderTest {
18-
@BeforeAll
19-
public static void preSetup(){
20-
setupDataProvider(true);
21-
}
22-
15+
class DataProviderTest extends SoftwareDevelopmentSimulationAppBaseTest {
2316
@Test
2417
void when_updateDevelopmentTeamsSetup_currentDevelopmentTeamsSetupIsPopulated_withReplacementOfOldValues() {
2518
DevelopmentTeamCreationParameters parameters = new DevelopmentTeamCreationParameters();

src/test/java/dev/markodojkic/softwaredevelopmentsimulation/test/DeveloperTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@
22

33
import dev.markodojkic.softwaredevelopmentsimulation.enums.DeveloperType;
44
import dev.markodojkic.softwaredevelopmentsimulation.model.Developer;
5-
import org.junit.jupiter.api.BeforeAll;
5+
import dev.markodojkic.softwaredevelopmentsimulation.test.Config.SoftwareDevelopmentSimulationAppBaseTest;
66
import org.junit.jupiter.api.BeforeEach;
77
import org.junit.jupiter.api.Test;
88

9-
import static dev.markodojkic.softwaredevelopmentsimulation.util.DataProvider.setupDataProvider;
109
import static org.junit.jupiter.api.Assertions.*;
1110

12-
class DeveloperTest {
11+
class DeveloperTest extends SoftwareDevelopmentSimulationAppBaseTest {
1312
private Developer developer;
1413

15-
@BeforeAll
16-
public static void preSetup(){
17-
setupDataProvider(true);
18-
}
19-
2014
@BeforeEach
2115
public void setup() {
2216
developer = new Developer();
@@ -31,6 +25,7 @@ void when_noArgsConstructorIsCalled_correctValuesAreSetAsDefault() {
3125
assertNull(developer.getYugoslavianUMCN());
3226
assertNull(developer.getPlaceOfBirth());
3327
assertEquals(DeveloperType.INTERN_DEVELOPER, developer.getDeveloperType());
28+
assertEquals("Intern developer", developer.getDeveloperType().getDisplayName());
3429
assertEquals(0L, developer.getExperienceCoefficient());
3530
}
3631

@@ -65,14 +60,16 @@ void testToString() {
6560

6661
@Test
6762
void when_equalsOrHashCodeIsCalled_onEqualObjectAreSame_onNonEqualObjectsAreDifferent() {
68-
Developer developer1 = new Developer("John Doe", "1234567890123", DeveloperType.SENIOR_DEVELOPER, false, 2L);
69-
Developer developer2 = new Developer("John Doe", "1234567890123", DeveloperType.SENIOR_DEVELOPER, false, 2L);
63+
Developer developer1 = new Developer("John Doe", "1234567858123", DeveloperType.SENIOR_DEVELOPER, false, 2L);
64+
Developer developer2 = new Developer("John Doe", "1234567858123", DeveloperType.SENIOR_DEVELOPER, false, 2L);
7065
Developer developer3 = new Developer("Alice Johnson", "9876543210987", DeveloperType.INTERN_DEVELOPER, true, 1L);
7166

7267
assertEquals(developer1, developer2);
7368
assertEquals(developer1.hashCode(), developer2.hashCode());
7469

7570
assertNotEquals(developer1, developer3);
7671
assertNotEquals(developer1.hashCode(), developer3.hashCode());
72+
73+
assertNotEquals("John Doe", developer1);
7774
}
7875
}

0 commit comments

Comments
 (0)