1.idea安装scala插件
2.idea创建Maven项目
3.修改pom.xml文件 整合单元测试框架
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.8</version>
<!--<scope>provided</scope>-->
</dependency>
<!--依赖-->
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>3.0.0</version>
<!--<scope>test</scope>-->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!--<scope>test</scope>-->
</dependency>
</dependencies>
<build>
<plugins>
<!--插件-->
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>WDF TestSuite.txt</filereports>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
4.添加package,修改项目骨架
5.添加HelloWorld,并测试
object HelloWorld {
def main(args: Array[String]): Unit = {
println("hello scala")
}
}
6.scala编写单元测试的方式1
import org.scalatest.FunSuite
class ScalaJunitTest extends FunSuite {
//差集
test("Test difference") {
val a = Set("a", "b", "a", "c")
val b = Set("b", "d")
println(1111111)
assert(a -- b === Set("a", "c"))
}
//交集
test("Test intersection") {
val a = Set("a", "b", "a", "c")
val b = Set("b", "d")
println(222222)
assert(a.intersect(b) === Set("b"))
}
//并集
test("Test union") {
val a = Set("a", "b", "a", "c")
val b = Set("b", "d")
println(333333)
assert(a ++ b === Set("a", "b", "c", "d"))
}
test("Test difference1111") {
val a = Set("a", "b", "a", "c")
val b = Set("b", "d")
//应该等于Set("a","b")
println(444444)
assert(a -- b === Set("b", "c"))
}
test("test123"){
val a =1;
val b =3;
println(12345)
assert(a==b)
}
}
7.scala编写单元测试的方式2
import org.junit.{Assert, Test}
@Test
class ScalaClassTest {
@Test
def hel:Unit = {
//scala整合junit进行单元测试 注意必须是class
//oject 报错
val someValue = true
print(111)
assert(someValue == true)
}
}
8 项目源码 github地址
https://github.com/code1990/scala.git