Use assertThrows instead of ExpectedException

pull/1773/head
Quentin Hibon 2 years ago
parent 12649141b1
commit 8e4274d923

@ -62,6 +62,7 @@ kotlin {
implementation("org.hamcrest:hamcrest:2.2")
implementation("org.apache.commons:commons-io:1.3.2")
implementation("org.mockito.kotlin:mockito-kotlin:2.2.11")
implementation("org.junit.jupiter:junit-jupiter:5.8.1")
}
}
}

@ -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<Habit>
@get:Rule
var thrown = ExpectedException.none()!!
@Before
@Throws(Exception::class)
override fun setUp() {

@ -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"))
}
}

@ -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<Habit>
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)
}
}
}

@ -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() {

@ -31,17 +31,14 @@ 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 org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import java.util.ArrayList
import kotlin.test.assertNull
class SQLiteHabitListTest : BaseUnitTest() {
@get:Rule
var exception = ExpectedException.none()!!
private lateinit var repository: Repository<HabitRecord>
private var listener: ModelObservable.Listener = mock()
private lateinit var habitsArray: ArrayList<Habit>
@ -90,8 +87,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

Loading…
Cancel
Save