Skip to content

Commit

Permalink
refactor(junit): Migrate tests from JUnit4 to JUnit5 (#1712)
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-elimu authored Jul 12, 2024
2 parents eb36baa + aa87a08 commit 251bb72
Show file tree
Hide file tree
Showing 53 changed files with 931 additions and 1,109 deletions.
493 changes: 250 additions & 243 deletions pom.xml

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions src/test/java/ai/elimu/dao/ApplicationDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package ai.elimu.dao;

import ai.elimu.model.admin.Application;
import org.junit.jupiter.api.Test;

import org.apache.logging.log4j.Logger;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
@SpringJUnitConfig(locations = {
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
})
Expand All @@ -28,6 +26,6 @@ public class ApplicationDaoTest {
@Test
public void testReadAll() {
List<Application> applications = applicationDao.readAll();
assertThat(applications.isEmpty(), is(true));
assertTrue(applications.isEmpty());
}
}
19 changes: 8 additions & 11 deletions src/test/java/ai/elimu/dao/ApplicationVersionDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import ai.elimu.model.admin.Application;
import ai.elimu.model.admin.ApplicationVersion;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringJUnitConfig(locations = {
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
})
Expand All @@ -35,7 +32,7 @@ public void testReadAll() {
applicationDao.create(application);

List<ApplicationVersion> applicationVersions = applicationVersionDao.readAll(application);
assertThat(applicationVersions.isEmpty(), is(true));
assertTrue(applicationVersions.isEmpty());

applicationDao.delete(application);
}
Expand Down
16 changes: 6 additions & 10 deletions src/test/java/ai/elimu/dao/BaseDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package ai.elimu.dao;

import ai.elimu.dao.ImageDao;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import ai.elimu.model.content.multimedia.Image;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
@SpringJUnitConfig(locations = {
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
})
Expand Down
27 changes: 12 additions & 15 deletions src/test/java/ai/elimu/dao/ContributorDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package ai.elimu.dao;

import org.apache.logging.log4j.Logger;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.hibernate.exception.ConstraintViolationException;
import org.junit.Ignore;

import org.junit.Test;
import org.junit.runner.RunWith;
import ai.elimu.model.contributor.Contributor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.exception.ConstraintViolationException;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
@SpringJUnitConfig(locations = {
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
})
Expand All @@ -26,12 +23,12 @@ public class ContributorDaoTest {
@Autowired
private ContributorDao contributorDao;

@Ignore
@Test(expected=ConstraintViolationException.class)
@Disabled
@Test
public void testConstraintViolation() {
Contributor contributor = new Contributor();
contributorDao.create(contributor);
logger.info("contributor: " + contributor);
assertThat(contributor, nullValue());
assertNull(contributor);
}
}
23 changes: 11 additions & 12 deletions src/test/java/ai/elimu/dao/DeviceDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package ai.elimu.dao;

import ai.elimu.model.Device;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

@SpringJUnitConfig(locations = {
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
})
public class DeviceDaoTest extends TestCase {
public class DeviceDaoTest {

@Autowired
private DeviceDao deviceDao;
Expand All @@ -23,8 +22,8 @@ public void testRead() {
deviceDao.create(getDevice("22"));
deviceDao.create(getDevice("44"));

assertTrue("22".equals(deviceDao.read("22").getDeviceId()));
assertTrue("44".equals(deviceDao.read("44").getDeviceId()));
assertEquals("22", deviceDao.read("22").getDeviceId());
assertEquals("44", deviceDao.read("44").getDeviceId());
assertNull(deviceDao.read("33"));
}

Expand Down
40 changes: 24 additions & 16 deletions src/test/java/ai/elimu/dao/EmojiDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
package ai.elimu.dao;

import ai.elimu.model.content.Emoji;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.MethodName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringJUnitConfig(locations = {
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
})
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class EmojiDaoTest extends TestCase {
@TestMethodOrder(MethodName.class)
public class EmojiDaoTest {

@Autowired
private EmojiDao emojiDao;

@BeforeEach
void cleanupDao() {
var emojiList = emojiDao.readAll();
for (var emoji : emojiList) {
emojiDao.delete(emoji);
}
}

@Test
public void testReadAllOrdered() {
List<Emoji> expectedEmojis = new ArrayList<>();
expectedEmojis.add(getEmoji("A1F6"));
Expand All @@ -41,7 +49,7 @@ public void testReadAllOrdered() {

List<Emoji> actualEmojis = emojiDao.readAllOrdered();

Assert.assertArrayEquals(
assertArrayEquals(
expectedEmojis.stream().map(Emoji::getGlyph).toArray(),
actualEmojis.stream().map(Emoji::getGlyph).toArray()
);
Expand All @@ -50,7 +58,7 @@ public void testReadAllOrdered() {
@Test
public void testReadByGlyph() {
emojiDao.create(getEmoji("F1F6"));
assertTrue("F1F6".equals(emojiDao.readByGlyph("F1F6").getGlyph()));
assertEquals("F1F6", emojiDao.readByGlyph("F1F6").getGlyph());
}

private Emoji getEmoji(String glyph) {
Expand Down
36 changes: 17 additions & 19 deletions src/test/java/ai/elimu/dao/ImageDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package ai.elimu.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.logging.log4j.Logger;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import ai.elimu.model.content.Word;
import ai.elimu.model.content.multimedia.Image;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
@SpringJUnitConfig(locations = {
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
})
Expand All @@ -42,7 +40,7 @@ public void testReadAllLabeled() {
wordDao.create(wordCat);

List<Image> images = imageDao.readAllLabeled(wordCat);
assertThat(images.size(), is(0));
assertTrue(images.isEmpty());

Set<Word> words = new HashSet<>();
words.add(wordCat);
Expand All @@ -53,18 +51,18 @@ public void testReadAllLabeled() {
imageDao.create(image);

images = imageDao.readAllLabeled(wordDog);
assertThat(images.size(), is(0));
assertTrue(images.isEmpty());

images = imageDao.readAllLabeled(wordCat);
assertThat(images.size(), is(1));
assertThat(images.get(0).getWords().size(), is(1));
assertEquals(1, images.size());
assertEquals(1, images.get(0).getWords().size());

words.add(wordDog);
image.setWords(words);
imageDao.update(image);

images = imageDao.readAllLabeled(wordCat);
assertThat(images.size(), is(1));
assertThat(images.get(0).getWords().size(), is(2));
assertEquals(1, images.size());
assertEquals(2, images.get(0).getWords().size());
}
}
31 changes: 15 additions & 16 deletions src/test/java/ai/elimu/dao/LetterDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package ai.elimu.dao;

import ai.elimu.model.content.Letter;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.junit.jupiter.api.MethodOrderer.MethodName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringJUnitConfig(locations = {
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
})
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class LetterDaoTest extends TestCase {
@TestMethodOrder(MethodName.class)
public class LetterDaoTest {

@Autowired
private LetterDao letterDao;
Expand All @@ -40,7 +39,7 @@ public void testReadAllOrdered() {

List<Letter> lettersActual = letterDao.readAllOrdered();

Assert.assertArrayEquals(
assertArrayEquals(
lettersExpected.stream().map(Letter::getText).toArray(),
lettersActual.stream().map(Letter::getText).toArray()
);
Expand All @@ -61,7 +60,7 @@ public void testReadAllOrderedByUsage() {

List<Letter> lettersActual = letterDao.readAllOrderedByUsage();

Assert.assertArrayEquals(
assertArrayEquals(
lettersExpected.stream().map(Letter::getText).toArray(),
lettersActual.stream().filter(i -> i.getUsageCount() != 0).map(Letter::getText).toArray()
);
Expand Down
Loading

0 comments on commit 251bb72

Please sign in to comment.