scala JUnitSuite for unit test

import org.junit.{Before, BeforeClass, Test}
import org.scalatest.junit.JUnitSuite

import scala.util.control.{Breaks, ControlThrowable}
import scala.util.control.Breaks._

private class BreakControl extends ControlThrowable

object BreaksTest {
@BeforeClass
def _init : Unit = {

}
}

class BreaksTest extends JUnitSuite {

private val breakException = new BreakControl

def init : Unit = BreaksTest._init

@Before
def inittest() : Unit = {}

@Test
def test1() : Unit = {
var i: Int = 0
breakable {
while (i < 100) {
i = i + 1
if (i == 5) {
break // break()
}
}
}
println(i)
assert (5 == i, "i must eq 5")
}

@Test
def test2() : Unit = {
val loop = new Breaks

var i: Int = 0
loop.breakable {
while (i < 100) {
i = i + 1
if (i == 5) {
loop.break
}
}
}
println(i)
assert (5 == i, "i must eq 5")
}

@Test
def test3() : Unit = {
var i: Int = 0
try {
while (i < 100) {
i = i + 1
if (i == 5) {
throw breakException
}
}
} catch {
case ex: BreakControl =>
if (ex ne breakException) throw ex
}
println(i)
assert (5 == i, "i must eq 5")
}


}


import java.util
import java.util.Map

import com.shenma.kafka.MessageListener
import org.junit.{Before, BeforeClass, Test}
import org.scalatest.junit.JUnitSuite

object KafkaConsumerTransportTest {

private var consumerProperties : Map[String, Object] = null

@BeforeClass
def _init : Unit = {
consumerProperties = new util.HashMap[String, Object]
consumerProperties.put("bootstrap.servers", "192.16.10.44:9092")
consumerProperties.put("group.id", "test")
consumerProperties.put("enable.auto.commit", "false")
consumerProperties.put("retries", "10")
consumerProperties.put("auto.commit.interval.ms", "1000")
consumerProperties.put("session.timeout.ms", "15000")
consumerProperties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
consumerProperties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
}
}

class KafkaConsumerTransportTest extends JUnitSuite {

def init : Unit = KafkaConsumerTransportTest._init

@Before
def inittest() : Unit = {}

@Test
def test() : Unit = {
var messageListener : MessageListener[String, String] = new TestMagentCatMessageListener()
var transport : KafkaConsumerTransport[String, String] =
new KafkaConsumerTransport[String, String](KafkaConsumerTransportTest.consumerProperties, util.Arrays.asList("topic-2018-0925"), messageListener)
transport run()

// for test
try {
this.synchronized {
wait()
}
} catch {
case e: InterruptedException => {
println(e)
e.printStackTrace()
}
}
}
}

你可能感兴趣的:(编程语言-Scala)