Trait that contains ScalaTest's basic assertion methods, suitable for use with JUnit 5.
A suite of tests that can be run with either JUnit or ScalaTest.
A suite of tests that can be run with either JUnit or ScalaTest. This class allows you to write JUnit 5 tests
with ScalaTest's more concise assertion syntax as well as JUnit's assertions (assertEquals
, etc.).
You create tests by defining methods that are annotated with Test
, and can create fixtures with
methods annotated with Before
and After
. For example:
import org.scalatest.junit5.JUnitSuite import scala.collection.mutable.ListBuffer import _root_.org.junit.Test import _root_.org.junit.Before class TwoSuite extends JUnitSuite { var sb: StringBuilder = _ var lb: ListBuffer[String] = _ @Before def initialize() { sb = new StringBuilder("ScalaTest is ") lb = new ListBuffer[String] } @Test def verifyEasy() { sb.append("easy!") assert(sb.toString === "ScalaTest is easy!") assert(lb.isEmpty) lb += "sweet" } @Test def verifyFun() { sb.append("fun!") assert(sb.toString === "ScalaTest is fun!") assert(lb.isEmpty) } }
To execute JUnitSuite
s with ScalaTest's Runner
, you must include JUnit 5's jar file on the class path or runpath.
This version of JUnitSuite
was tested with JUnit version 5.9.
Instances of this class are not thread safe.
Implementation trait for class JUnitSuite
, which represents
a suite of tests that can be run with either JUnit 5 or ScalaTest.
Implementation trait for class JUnitSuite
, which represents
a suite of tests that can be run with either JUnit 5 or ScalaTest.
JUnitSuite
is a class, not a
trait, to minimize compile time given there is a slight compiler overhead to
mixing in traits compared to extending classes. If you need to mix the
behavior of JUnitSuite
into some other class, you can use this
trait instead, because class JUnitSuite
does nothing more than
extend this trait.
See the documentation of the class for a detailed
overview of JUnitSuite
.
Exception that indicates a test failed in JUnit 5.
Exception that indicates a test failed in JUnit 5.
The purpose of this exception is to encapsulate the same stack depth information provided by
TestFailedException
, which is used
when running with ScalaTest, but be reported as
a failure not an error when running with JUnit.
The stack depth information indicates which line of test code failed, so that when running
with ScalaTest information can be presented to
the user that makes it quick to find the failing line of test code. (In other words, when
running with ScalaTest the user need not scan through the stack trace to find the correct filename
and line number of the failing test.)
JUnit distinguishes between failures and errors.
If a test fails because of a failed assertion, that is considered a failure in JUnit. If a test
fails for any other reason, either the test code or the application being tested threw an unexpected
exception, that is considered an error in JUnit. This class differs from
TestFailedException
in that it extends
org.opentest4j.AssertionFailedError
. Instances of this class are thrown by the
assertions provided by AssertionsForJUnit
.
The way JUnit 3 (JUnit 3.8 and earlier releases) decided whether an exception represented a failure or error
is that only thrown junit.framework.AssertionFailedError
s were considered failures. Any other
exception type was considered an error. The exception type thrown by the JUnit 3 assertion methods declared
in junit.framework.Assert
(such as assertEquals
, assertTrue
,
and fail
) was, therefore, AssertionFailedError
. In JUnit 4, junit.framework.AssertionFailedError
was made to extend java.lang.AssertionError
, and the distinction between failures and errors
was essentially dropped. In JUnit 5, org.opentest4j.AssertionFailedError
is used instead as common
base class for test-related AssertionErrors.
NullArgumentException
if either message
or cause
is null
, or Some(null)
.
TestDescriptor
for ScalaTest suite.
TestDescriptor
for ScalaTest suite.
TestDescriptor
for a test in ScalaTest suite.
TestDescriptor
for a test in ScalaTest suite.
ScalaTest implementation for JUnit 5 Test Engine.
Companion object that facilitates the importing of AssertionsForJUnit
members as
an alternative to mixing it in.
Companion object that facilitates the importing of AssertionsForJUnit
members as
an alternative to mixing it in. One use case is to import AssertionsForJUnit
members so you can use
them in the Scala interpreter:
sbt:junit-5.9> console [info] Starting scala interpreter... Welcome to Scala 2.13.10 (OpenJDK 64-Bit Server VM, Java 1.8.0_352). Type in expressions for evaluation. Or try :help. scala> import org.scalatestplus.junit5.AssertionsForJUnit._ import org.scalatestplus.junit5.AssertionsForJUnit._ scala> assert(1 === 2) org.scalatestplus.junit5.JUnitTestFailedError: 1 did not equal 2 at org.scalatestplus.junit5.AssertionsForJUnit.newAssertionFailedException(AssertionsForJUnit.scala:64) at org.scalatestplus.junit5.AssertionsForJUnit.newAssertionFailedException$(AssertionsForJUnit.scala:63) at org.scalatestplus.junit5.AssertionsForJUnit$.newAssertionFailedException(AssertionsForJUnit.scala:111) at org.scalatestplus.junit5.AssertionsForJUnit$AssertionsHelper.macroAssert(AssertionsForJUnit.scala:148) ... 35 elided scala> val caught = intercept[StringIndexOutOfBoundsException] { "hi".charAt(-1) } val caught: StringIndexOutOfBoundsException = java.lang.StringIndexOutOfBoundsException: String index out of range: -1
ScalaTestClassDescriptor
companion object.
ScalaTestClassDescriptor
companion object.
Trait that contains ScalaTest's basic assertion methods, suitable for use with JUnit 5.
The assertion methods provided in this trait look and behave exactly like the ones in
Assertions
, except instead of throwingTestFailedException
they throwJUnitTestFailedError
, which extendsorg.opentest4j.AssertionFailedError
.JUnit 3 (release 3.8 and earlier) distinguishes between failures and errors. If a test fails because of a failed assertion, that is considered a failure. If a test fails for any other reason, either the test code or the application being tested threw an unexpected exception, that is considered an error. The way JUnit 3 decides whether an exception represents a failure or error is that only thrown
junit.framework.AssertionFailedError
s are considered failures. Any other exception type is considered an error. The exception type thrown by the JUnit 3 assertion methods declared injunit.framework.Assert
(such asassertEquals
,assertTrue
, andfail
) is, therefore,AssertionFailedError
.In JUnit 4,
junit.framework.AssertionFailedError
was made to extendjava.lang.AssertionError
, and the distinction between failures and errors was essentially dropped. However, some tools that integrate with JUnit carry on this distinction, so even if you are using JUnit 4 you may want to use thisAssertionsForJUnit
trait instead of plain-old ScalaTestAssertions
.In JUnit 5,
org.opentest4j.AssertionFailedError
is used as test-related AssertionError instead.