summaryrefslogtreecommitdiff
path: root/test/junit/scala/tools/testing/AssertThrowsTest.scala
blob: 76758f51d2be2420c3bc08deed877a296b2dd3cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package scala.tools
package testing

import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import AssertUtil.assertThrows

@RunWith(classOf[JUnit4])
class AssertThrowsTest {
  class Foo extends Exception
  class SubFoo extends Foo
  class Bar extends Exception

  @Test
  def catchFoo = assertThrows[Foo] { throw new Foo }

  @Test
  def catchSubclass = assertThrows[Foo] { throw new SubFoo }

  @Test
  def rethrowBar =
    assertTrue("exception wasn't rethrown", {
      try {
        assertThrows[Foo] { throw new Bar }
        false
      } catch {
        case bar: Bar => true
        case e: Throwable => fail(s"expected Bar but got $e"); false
      }
    })

  @Test
  def errorIfNoThrow: Unit = {
    try {
      assertThrows[Foo] { () }
    } catch {
      case e: AssertionError => return
    }
    fail("assertThrows should error if the tested expression does not throw anything")
  }
}