Skip to content

Commit

Permalink
Use assertThrows instead of ExpectedException
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqua committed Jul 25, 2023
1 parent 4127eb2 commit 5431687
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 44 deletions.
1 change: 1 addition & 0 deletions uhabits-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -235,24 +232,27 @@ 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
@Throws(Exception::class)
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
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HabitRecord>
private var listener: ModelObservable.Listener = mock()
private lateinit var habitsArray: ArrayList<Habit>
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5431687

Please sign in to comment.