Skip to content

Instantly share code, notes, and snippets.

@icejoywoo
Last active September 30, 2018 07:19
Show Gist options
  • Save icejoywoo/2c7b6960e4cbbacf901a8f57f951333b to your computer and use it in GitHub Desktop.
Save icejoywoo/2c7b6960e4cbbacf901a8f57f951333b to your computer and use it in GitHub Desktop.

Revisions

  1. icejoywoo revised this gist Sep 30, 2018. 2 changed files with 35 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions UsingJUnit.scala
    Original file line number Diff line number Diff line change
    @@ -35,3 +35,26 @@ class UsingJUnit {
    assertNotEquals(List("b", "a"), list)
    }
    }

    // junit check exception
    class JunitCheckException {

    val _thrown = rules.ExpectedException.none

    @Rule
    def thrown = _thrown

    @Test(expected = classOf[IndexOutOfBoundsException])
    def testStringIndexOutOfBounds(): Unit = {
    val s = "test string"
    s.charAt(-1)
    }

    @Test
    def testStringIndexOutOfBoundsExceptionMessage(): Unit = {
    val s = "test string"
    thrown.expect(classOf[IndexOutOfBoundsException])
    thrown.expectMessage("String index out of range: -1")
    s.charAt(-1)
    }
    }
    12 changes: 12 additions & 0 deletions UsingScalaTest.scala
    Original file line number Diff line number Diff line change
    @@ -98,3 +98,15 @@ class UsingGivenWhenThen extends FlatSpec with GivenWhenThen with Matchers {
    list.size should be(2)
    }
    }

    // 验证抛出的异常
    // check the exception thrown by method
    class ScalaTestCheckException extends FlatSpec with Matchers {
    "string" should "throw IndexOutOfBoundsException when index is illegal" in {
    val s = "test string"
    an [IndexOutOfBoundsException] should be thrownBy s.charAt(-1)

    val thrown = the [IndexOutOfBoundsException] thrownBy s.charAt(-1)
    thrown.getMessage should equal ("String index out of range: -1")
    }
    }
  2. icejoywoo revised this gist Sep 30, 2018. 2 changed files with 2 additions and 1 deletion.
    1 change: 1 addition & 0 deletions UsingJUnit.scala
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    import org.junit.Assert._
    import org.junit._

    // junit 4
    object UsingJUnit {
    @BeforeClass
    def beforeClass(): Unit = {
    2 changes: 1 addition & 1 deletion UsingScalaTest.scala
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import org.scalatest._

    //
    // scala test 3.0.1
    class UsingScalaTest extends FlatSpec with Matchers {
    trait EmptyArrayList {
    val list = new java.util.ArrayList[String]
  3. icejoywoo created this gist Sep 30, 2018.
    36 changes: 36 additions & 0 deletions UsingJUnit.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    import org.junit.Assert._
    import org.junit._

    object UsingJUnit {
    @BeforeClass
    def beforeClass(): Unit = {
    println("before class")
    }

    @AfterClass
    def afterClass(): Unit = {
    println("after class")
    }
    }

    class UsingJUnit {

    @Before
    def before(): Unit = {
    println("before test")
    }

    @After
    def after(): Unit = {
    println("after test")
    }

    @Test
    def testList(): Unit = {
    println("testList")
    val list = List("a", "b")

    assertEquals(List("a", "b"), list)
    assertNotEquals(List("b", "a"), list)
    }
    }
    100 changes: 100 additions & 0 deletions UsingScalaTest.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    import org.scalatest._

    //
    class UsingScalaTest extends FlatSpec with Matchers {
    trait EmptyArrayList {
    val list = new java.util.ArrayList[String]
    }

    "a list" should "be empty on create" in new EmptyArrayList {
    list.size shouldBe 0
    }

    it should "increase in size upon add" in new EmptyArrayList {
    list.add("Milk")
    list add "Sugar"

    list.size should be(2)
    }
    }

    class UsingBeforeAndAfter extends FlatSpec with BeforeAndAfter with Matchers {

    var list: java.util.List[String] = _

    before {
    println("before")
    list = new java.util.ArrayList[String]
    }

    after {
    println("after")
    if (list != null) {
    list = null
    }
    }

    "a list" should "be empty on create" in {
    list.size shouldBe 0
    }

    it should "increase in size upon add" in {
    list.add("Milk")
    list add "Sugar"

    list.size should be(2)
    }
    }

    class UsingBeforeAndAfterAll extends FlatSpec with BeforeAndAfterAll with Matchers {

    var list: java.util.List[String] = _

    override def beforeAll(): Unit = {
    println("before all...")
    list = new java.util.ArrayList[String]
    super.beforeAll()
    }

    override def afterAll(): Unit = {
    println("after all...")
    if (list != null) {
    list = null
    }
    super.afterAll()
    }

    "a list" should "be empty on create" in {
    list.size shouldBe 0
    }

    it should "increase in size upon add" in {
    list.add("Milk")
    list add "Sugar"

    list.size should be(2)
    }
    }

    class UsingGivenWhenThen extends FlatSpec with GivenWhenThen with Matchers {
    trait EmptyArrayList {
    val list = new java.util.ArrayList[String]
    }

    "a list" should "be empty on create" in new EmptyArrayList {
    Given("a empty list")
    Then("list size should be 0")
    list.size shouldBe 0
    }

    it should "increase in size upon add" in new EmptyArrayList {
    Given("a empty list")

    When("add 2 elements")
    list.add("Milk")
    list add "Sugar"

    Then("list size should be 2")
    list.size should be(2)
    }
    }