scala01.idea+junit 搭建scala开发环境

1.idea安装scala插件

QQ截图20181119212842.png

2.idea创建Maven项目

QQ截图20181119210336.png

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,修改项目骨架

QQ截图20181119213755.png

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容