diff --git a/uhabits-core/build.gradle.kts b/uhabits-core/build.gradle.kts index c0854f067..d8153f586 100644 --- a/uhabits-core/build.gradle.kts +++ b/uhabits-core/build.gradle.kts @@ -62,6 +62,7 @@ kotlin { implementation("org.hamcrest:hamcrest:2.2") implementation("org.apache.commons:commons-io:1.3.2") implementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0") + implementation("org.junit.jupiter:junit-jupiter:5.8.1") } } } diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/commands/DeleteHabitsCommandTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/commands/DeleteHabitsCommandTest.kt index b44328035..15f7f6717 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/commands/DeleteHabitsCommandTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/commands/DeleteHabitsCommandTest.kt @@ -23,18 +23,13 @@ import org.hamcrest.MatcherAssert.assertThat import org.isoron.uhabits.core.BaseUnitTest import org.isoron.uhabits.core.models.Habit import org.junit.Before -import org.junit.Rule import org.junit.Test -import org.junit.rules.ExpectedException -import java.util.LinkedList +import java.util.* class DeleteHabitsCommandTest : BaseUnitTest() { private lateinit var command: DeleteHabitsCommand private lateinit var selected: LinkedList - @get:Rule - var thrown = ExpectedException.none()!! - @Before @Throws(Exception::class) override fun setUp() { diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/database/migrations/Version22Test.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/database/migrations/Version22Test.kt index 9a9927cc1..a98f28215 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/database/migrations/Version22Test.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/database/migrations/Version22Test.kt @@ -27,13 +27,10 @@ import org.isoron.uhabits.core.database.Database import org.isoron.uhabits.core.database.MigrationHelper import org.isoron.uhabits.core.models.sqlite.SQLModelFactory import org.isoron.uhabits.core.test.HabitFixtures -import org.junit.Rule import org.junit.Test -import org.junit.rules.ExpectedException +import org.junit.jupiter.api.Assertions.assertThrows class Version22Test : BaseUnitTest() { - @get:Rule - var exception = ExpectedException.none()!! private lateinit var db: Database private lateinit var helper: MigrationHelper @@ -76,8 +73,8 @@ class Version22Test : BaseUnitTest() { @Throws(Exception::class) fun testDisallowNewRepsWithInvalidRef() { helper.migrateTo(22) - exception.expectMessage(Matchers.containsString("SQLITE_CONSTRAINT")) - db.execute("insert into Repetitions(habit, timestamp, value) values (99999, 100, 2)") + val exception = assertThrows(java.lang.RuntimeException::class.java) { db.execute("insert into Repetitions(habit, timestamp, value) values (99999, 100, 2)") } + assertThat(exception.message, Matchers.containsString("SQLITE_CONSTRAINT")) } @Test @@ -97,8 +94,12 @@ class Version22Test : BaseUnitTest() { @Throws(Exception::class) fun testDisallowNullTimestamp() { helper.migrateTo(22) - exception.expectMessage(Matchers.containsString("SQLITE_CONSTRAINT")) - db.execute("insert into Repetitions(habit, value) " + "values (0, 2)") + + val exception = assertThrows(java.lang.RuntimeException::class.java) { + db.execute("insert into Repetitions(habit, value) " + "values (0, 2)") + } + + assertThat(exception.message, Matchers.containsString("SQLITE_CONSTRAINT")) } @Test @@ -118,8 +119,12 @@ class Version22Test : BaseUnitTest() { @Throws(Exception::class) fun testDisallowNullHabit() { helper.migrateTo(22) - exception.expectMessage(Matchers.containsString("SQLITE_CONSTRAINT")) - db.execute("insert into Repetitions(timestamp, value) " + "values (5, 2)") + + val exception = assertThrows(java.lang.RuntimeException::class.java) { + db.execute("insert into Repetitions(timestamp, value) " + "values (5, 2)") + } + + assertThat(exception.message, Matchers.containsString("SQLITE_CONSTRAINT")) } @Test @@ -142,7 +147,11 @@ class Version22Test : BaseUnitTest() { fun testDisallowNewDuplicateTimestamps() { helper.migrateTo(22) db.execute("insert into repetitions(habit, timestamp, value)values (0, 100, 2)") - exception.expectMessage(Matchers.containsString("SQLITE_CONSTRAINT")) - db.execute("insert into repetitions(habit, timestamp, value)values (0, 100, 5)") + + val exception = assertThrows(java.lang.RuntimeException::class.java) { + db.execute("insert into repetitions(habit, timestamp, value)values (0, 100, 5)") + } + + assertThat(exception.message, Matchers.containsString("SQLITE_CONSTRAINT")) } } diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitListTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitListTest.kt index 82f8d2c24..f4cecda6f 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitListTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitListTest.kt @@ -22,19 +22,15 @@ import org.hamcrest.CoreMatchers.equalTo import org.hamcrest.CoreMatchers.not import org.hamcrest.MatcherAssert.assertThat import org.isoron.uhabits.core.BaseUnitTest -import org.junit.Rule +import org.junit.Assert.assertThrows import org.junit.Test -import org.junit.rules.ExpectedException import java.io.IOException import java.io.StringWriter -import java.util.ArrayList import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNull class HabitListTest : BaseUnitTest() { - @get:Rule - var thrown = ExpectedException.none()!! private lateinit var habitsArray: ArrayList private lateinit var activeHabits: HabitList private lateinit var reminderHabits: HabitList @@ -173,8 +169,9 @@ class HabitListTest : BaseUnitTest() { fun testReorder_withInvalidArguments() { val h1 = habitsArray[0] val h2 = fixtures.createEmptyHabit() - thrown.expect(IllegalArgumentException::class.java) - habitList.reorder(h1, h2) + assertThrows(IllegalArgumentException::class.java) { + habitList.reorder(h1, h2) + } } @Test @@ -235,15 +232,17 @@ class HabitListTest : BaseUnitTest() { @Test @Throws(Exception::class) fun testAdd_withFilteredList() { - thrown.expect(IllegalStateException::class.java) - activeHabits.add(fixtures.createEmptyHabit()) + assertThrows(IllegalStateException::class.java) { + activeHabits.add(fixtures.createEmptyHabit()) + } } @Test @Throws(Exception::class) fun testRemove_onFilteredList() { - thrown.expect(IllegalStateException::class.java) - activeHabits.remove(fixtures.createEmptyHabit()) + assertThrows(IllegalStateException::class.java) { + activeHabits.remove(fixtures.createEmptyHabit()) + } } @Test @@ -251,8 +250,9 @@ class HabitListTest : BaseUnitTest() { fun testReorder_onFilteredList() { val h1 = fixtures.createEmptyHabit() val h2 = fixtures.createEmptyHabit() - thrown.expect(IllegalStateException::class.java) - activeHabits.reorder(h1, h2) + assertThrows(IllegalStateException::class.java) { + activeHabits.reorder(h1, h2) + } } @Test @@ -261,7 +261,8 @@ class HabitListTest : BaseUnitTest() { habitList.primaryOrder = HabitList.Order.BY_SCORE_DESC val h1 = habitsArray[1] val h2 = habitsArray[2] - thrown.expect(IllegalStateException::class.java) - habitList.reorder(h1, h2) + assertThrows(IllegalStateException::class.java) { + habitList.reorder(h1, h2) + } } } diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitTest.kt index df06d3d2c..64b498ed3 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/HabitTest.kt @@ -24,16 +24,12 @@ import org.hamcrest.core.IsEqual.equalTo import org.isoron.uhabits.core.BaseUnitTest import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday import org.junit.Assert.assertNotEquals -import org.junit.Rule import org.junit.Test -import org.junit.rules.ExpectedException import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue class HabitTest : BaseUnitTest() { - @get:Rule - val exception = ExpectedException.none()!! @Throws(Exception::class) override fun setUp() { diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.kt index 2351af4ee..acf418955 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.kt @@ -33,15 +33,11 @@ import org.isoron.uhabits.core.models.Reminder import org.isoron.uhabits.core.models.WeekdayList import org.isoron.uhabits.core.models.sqlite.records.HabitRecord import org.isoron.uhabits.core.test.HabitFixtures -import org.junit.Rule +import org.junit.Assert.assertThrows import org.junit.Test -import org.junit.rules.ExpectedException -import java.util.ArrayList import kotlin.test.assertNull class SQLiteHabitListTest : BaseUnitTest() { - @get:Rule - var exception = ExpectedException.none()!! private lateinit var repository: Repository private var listener: ModelObservable.Listener = mock() private lateinit var habitsArray: ArrayList @@ -90,8 +86,9 @@ class SQLiteHabitListTest : BaseUnitTest() { val habit = modelFactory.buildHabit() habitList.add(habit) verify(listener).onModelChange() - exception.expect(IllegalArgumentException::class.java) - habitList.add(habit) + assertThrows(IllegalArgumentException::class.java) { + habitList.add(habit) + } } @Test