问题
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. test
s:
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