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.
Scala Unit Test Demo -- Scala 的单测框架和示例(JUnit4 & ScalaTest)
import org.junit.Assert._
import org.junit._
// junit 4
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)
}
}
// 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)
}
}
import org.scalatest._
// scala test 3.0.1
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)
}
}
// 验证抛出的异常
// 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")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment