How to turn on assert statements in junit4 runned with sbt?

老子叫甜甜 提交于 2019-12-25 00:35:39

问题


I use sbt for development routines in my java project and junit-interface for running tests. But by default it ignores assert statements(assert for checking some invariant in underlying code). For example this test passes:

@Test
public void test() {
  // this is example; In reality assertion somewhere in code under test
  assert false; // not assertTrue(or whatever) from junit
}

My build.sbt file is fairly simple:

organization := "Kharandziuk"

name := "Some alg project"

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
    "junit" % "junit" % "4.8.1" % "test",
    "com.novocode" % "junit-interface" % "0.10" % "test"
)

autoScalaLibrary := false

testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-q", "-v")

fork in run := true

javaOptions in run += "-ea"

How I can enable assert statements in tests?


回答1:


You can set it for separate tasks, eg. tests:

javaOptions in test += "-ea"

or simply set it everywhere:

javaOptions += "-ea"

Regardless, @MPirious is right in that you typically don't want to cause assertion failures through unit testing. A unit test is supposed to sandbox your code's logic




回答2:


Your use of "assert" is a keyword in java. They need to be enabled by by supplying JVM option "-ea".

I think, you want to use the static methods of org.junit.Assert.

@Test
public void test() {
  org.junit.Assert.assertTrue(SOME CONDITION);
}


来源:https://stackoverflow.com/questions/24637692/how-to-turn-on-assert-statements-in-junit4-runned-with-sbt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!