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

发布于 2024-10-09 21:30:09 字数 167 浏览 0 评论 0原文

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

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

}

它可以运行,但 J-Unit 说没有可以运行的测试

I can't run following code with IDEA

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

}

It runs, but J-Unit says that there are not test to run

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

—━☆沉默づ 2024-10-16 21:30:09

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

I generally use ScalaTest in combination with the Junit4 runner so that Maven sees and executes my tests. I like the Spec/FlatSpec/WordSpec semantics for organizing tests. I'm experimenting with the ShouldMatchers but I have used JUnit for so long that asserts just seem a bit more natural to me.

Here's an example:

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)
    }
}

The ScalaTest docs are at http://www.scalatest.org/

世俗缘 2024-10-16 21:30:09

以下对我有用

import org.junit._
import Assert._

class MyTest {

    @Test
    def test = assert(false)
}

The following works for me

import org.junit._
import Assert._

class MyTest {

    @Test
    def test = assert(false)
}
诗酒趁年少 2024-10-16 21:30:09

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

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

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

The @org.junit.Test annotation is only applicable for methods: @Target({ElementType.METHOD}).
Also keep in mind that test methods must return Unit.

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

class CompanyURLTest {
  @Test def test = assertFalse(false)
}
醉南桥 2024-10-16 21:30:09

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

https://guides.gradle.org/building-scala-libraries/#review_the_ generated_project_files< /a>

Even though it's late - Gradle site surprisingly has a lot of good tutorials showing how to support Scala tests.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文