【问题标题】:How to run scala tests with junit 4?如何使用 junit 4 运行 scala 测试?
【发布时间】:2011-05-31 13:51:10
【问题描述】:

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

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

}

它运行了,但是 J-Unit 说没有要运行的测试

【问题讨论】:

  • 您能否详细介绍一下您正在使用的测试框架?是普通的 JUnit 还是其他的东西?

标签: unit-testing scala scala-2.8 junit4


【解决方案1】:

我通常将 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/

【讨论】:

    【解决方案2】:

    以下对我有用

    import org.junit._
    import Assert._
    
    class MyTest {
    
        @Test
        def test = assert(false)
    }
    

    【讨论】:

      【解决方案3】:

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

      import org.junit.Assert._
      import org.junit.Test
      
      class CompanyURLTest {
        @Test def test = assertFalse(false)
      }
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-09
          • 2019-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多