Skip to content

Commit

Permalink
Migrate unit tests to JUnit 5 (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst authored Oct 31, 2023
1 parent 0d0b935 commit 1e5953e
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 389 deletions.
20 changes: 5 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mockito.version>1.10.19</mockito.version>
<junit.version>4.13.2</junit.version>
<github.global.server>github</github.global.server>
<github.global.oauth2Token>${env.GH_TOKEN}</github.global.oauth2Token>
<java.level>11</java.level>
Expand Down Expand Up @@ -226,13 +224,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
<configuration>
<includes>
<include>**/*.java</include>
Expand Down Expand Up @@ -295,16 +286,15 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>

Expand Down
272 changes: 130 additions & 142 deletions src/test/java/com/saucelabs/ci/BrowserFactoryTest.java

Large diffs are not rendered by default.

49 changes: 23 additions & 26 deletions src/test/java/com/saucelabs/ci/BuildInformationTest.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
package com.saucelabs.ci;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/** Created by gavinmogan on 2016-02-10. */
public class BuildInformationTest {
class BuildInformationTest {
private BuildInformation build;
private JSONObject jobs;

@Before
public void setUp() throws Exception {
@BeforeEach
void beforeEach() throws Exception {
JSONObject obj =
new JSONObject(
IOUtils.toString(getClass().getResourceAsStream("/build_info.json"), "UTF-8"));
IOUtils.toString(getClass().getResourceAsStream("/build_info.json"), StandardCharsets.UTF_8));
build = new BuildInformation("1234");
build.populateFromJson(obj);
}

@Test
public void testBuildId() {
void testBuildId() {
assertEquals("1234", build.getBuildId());
}

@Test
public void testStatus() {
void testStatus() {
assertEquals("failed", build.getStatus());

build.clearChanges();
Expand All @@ -50,10 +53,10 @@ public void testStatus() {
}

@Test
public void testPopulateFromJson() {
void testPopulateFromJson() {
assertEquals("test-name", build.getName());

jobs = new JSONObject();
JSONObject jobs = new JSONObject();
jobs.put("finished", 7);
jobs.put("passed", 5);
jobs.put("failed", 2);
Expand All @@ -70,7 +73,7 @@ public void testPopulateFromJson() {
json.put("name", (String) null);
build = new BuildInformation("1234");
build.populateFromJson(json);
assertEquals(null, build.getName());
assertNull(build.getName());

json.put("name", "Something");
build = new BuildInformation("1234");
Expand All @@ -81,7 +84,7 @@ public void testPopulateFromJson() {
json.put("status", (String) null);
build = new BuildInformation("1234");
build.populateFromJson(json);
assertEquals(null, build.getStatus());
assertNull(build.getStatus());

json.put("status", "success");
build = new BuildInformation("1234");
Expand Down Expand Up @@ -127,10 +130,7 @@ public void testPopulateFromJson() {
}

@Test
public void testBuildName() {
HashMap<String, Object> updates = new HashMap<>();
updates.put("name", "Gavin's first build");

void testBuildName() {
assertEquals("test-name", build.getName());
assertFalse(build.hasChanges());
build.setName(null);
Expand All @@ -142,19 +142,16 @@ public void testBuildName() {
build.setName("Gavin's first build");
assertTrue(build.hasBuildName());
assertTrue(build.hasChanges());
assertEquals(updates, build.getChanges());
assertEquals(Map.of("name", "Gavin's first build"), build.getChanges());
}

@Test
public void testGetChange() {
HashMap<String, Object> updates = new HashMap<>();
updates.put("name", "name-name-name");

void testGetChange() {
assertEquals("test-name", build.getName());
assertFalse(build.hasChanges());
build.setName("name-name-name");
assertTrue(build.hasChanges());
assertEquals(updates, build.getChanges());
assertEquals(Map.of("name", "name-name-name"), build.getChanges());
}

/* TODO - figure out how to test equals */
Expand Down
50 changes: 24 additions & 26 deletions src/test/java/com/saucelabs/ci/BuildJobInformationTest.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package com.saucelabs.ci;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.EnumSet;
import java.util.HashMap;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Map;

public class BuildJobInformationTest {
class BuildJobInformationTest {
private BuildJobInformation job;

@Before
public void setUp() throws Exception {
@BeforeEach
void beforeEach() throws Exception {
JSONObject obj =
new JSONObject(
IOUtils.toString(getClass().getResourceAsStream("/build_job.json"), "UTF-8"));
IOUtils.toString(getClass().getResourceAsStream("/build_job.json"), StandardCharsets.UTF_8));
job = new BuildJobInformation(obj);
}

@Test
public void testBuildJobInformation_ConstructorFromJSON() throws Exception {
void testBuildJobInformation_ConstructorFromJSON() throws Exception {
JSONObject obj =
new JSONObject(
IOUtils.toString(getClass().getResourceAsStream("/build_job.json"), "UTF-8"));
IOUtils.toString(getClass().getResourceAsStream("/build_job.json"), StandardCharsets.UTF_8));
job = new BuildJobInformation(obj);

assertEquals(1641976754, job.getCreationTime());
Expand All @@ -40,7 +41,7 @@ public void testBuildJobInformation_ConstructorFromJSON() throws Exception {
}

@Test
public void testBuildJobInformation_ConstructorFromId() {
void testBuildJobInformation_ConstructorFromId() {
job = new BuildJobInformation("1234");

assertEquals("1234", job.getJobId());
Expand All @@ -51,33 +52,33 @@ public void testBuildJobInformation_ConstructorFromId() {
}

@Test
public void testJobId() {
void testJobId() {
assertEquals("1375ba7db500432a97fbd0d99ce2bff2", job.getJobId());
}

@Test
public void testSetGetCreationTime() {
void testSetGetCreationTime() {
job.setCreationTime(25);

assertEquals(25, job.getCreationTime());
}

@Test
public void testSetGetModificationTime() {
void testSetGetModificationTime() {
job.setModificationTime(26);

assertEquals(26, job.getModificationTime());
}

@Test
public void testSetGetDeletionTime() {
void testSetGetDeletionTime() {
job.setDeletionTime(26);

assertEquals(26, job.getDeletionTime());
}

@Test
public void testSetStatusFlag() {
void testSetStatusFlag() {
assertFalse(job.getStatus().contains(BuildJobInformation.JobStatusFlag.PUBLIC));
assertFalse(job.hasChanges());

Expand All @@ -88,7 +89,7 @@ public void testSetStatusFlag() {
}

@Test
public void testClearStatusFlag() {
void testClearStatusFlag() {
job.setStatusFlag(BuildJobInformation.JobStatusFlag.PUBLIC);
job.clearChanges();
assertTrue(job.getStatus().contains(BuildJobInformation.JobStatusFlag.PUBLIC));
Expand All @@ -101,7 +102,7 @@ public void testClearStatusFlag() {
}

@Test
public void testPopulateFromJson() {
void testPopulateFromJson() {
JSONObject states = new JSONObject();
states.put("completed", true);
states.put("errored", true);
Expand Down Expand Up @@ -130,13 +131,10 @@ public void testPopulateFromJson() {
}

@Test
public void testGetChange() {
HashMap<String, Object> updates = new HashMap<>();
updates.put("creationTime", (long) 1);

void testGetChange() {
assertFalse(job.hasChanges());
job.setCreationTime(1);
assertTrue(job.hasChanges());
assertEquals(updates, job.getChanges());
assertEquals(Map.of("creationTime", (long) 1), job.getChanges());
}
}
Loading

0 comments on commit 1e5953e

Please sign in to comment.