Last active
September 30, 2018 07:19
-
-
Save icejoywoo/2c7b6960e4cbbacf901a8f57f951333b to your computer and use it in GitHub Desktop.
Scala Unit Test Demo -- Scala 的单测框架和示例(JUnit4 & ScalaTest)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment