如何使用junit 4运行scala测试?

时间:2010-12-28 12:50:41

标签: unit-testing scala scala-2.8 junit4

我无法使用IDEA运行以下代码

@Test
class CompanyURLTest extends Assert {
  @Test
  def test = assert(false);

}

它运行,但J-Unit表示没有运行测试

4 个答案:

答案 0 :(得分:5)

我通常将ScalaTest与Junit4运行程序结合使用,以便Maven查看并执行我的测试。我喜欢用于组织测试的Spec / FlatSpec / WordSpec语义。我正在尝试使用ShouldMatchers,但我已经使用了JUnit这么长时间,断言对我来说似乎更自然一些。

以下是一个例子:

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers

@RunWith(classOf[JUnitRunner])
class BlogFeedHandlerTest extends FlatSpec with ShouldMatchers with Logging {
    "the thingy" should "do what I expect it to do" in {
        val someValue = false;

        assert(someValue === false)
    }
}

ScalaTest文档位于http://www.scalatest.org/

答案 1 :(得分:2)

以下为我工作

import org.junit._
import Assert._

class MyTest {

    @Test
    def test = assert(false)
}

答案 2 :(得分:2)

@ org.junit.Test注释仅适用于方法:@Target({ElementType.METHOD})。 还要记住,测试方法必须返回Unit。

import org.junit.Assert._
import org.junit.Test

class CompanyURLTest {
  @Test def test = assertFalse(false)
}

答案 3 :(得分:0)

尽管已经很晚了-Gradle网站令人惊讶地有很多很好的教程,展示了如何支持Scala测试。

https://guides.gradle.org/building-scala-libraries/#review_the_generated_project_files