Trait that contains ScalaTest's basic assertion methods, suitable for use with JUnit.
A Suite
that is also a junit.framework.TestCase
.
A Suite
that is also a junit.framework.TestCase
.
A JUnit3Suite
may be run by either JUnit 3 (such as JUnit 3.8) or ScalaTest's runner. You write it the way
you write a JUnit 3 TestCase
. Tests are methods that start with test
, take no parameters, and
have a Unit
return type. You manage fixtures with methods setUp
and tearDown
.
Here's an example:
import org.scalatest.junit.JUnit3Suite import scala.collection.mutable.ListBufferYou can use either JUnit's assertions, inherited from
class BlastFromThePastSuite extends JUnit3Suite {
var sb: StringBuilder = _ var lb: ListBuffer[String] = _
override def setUp() { sb = new StringBuilder("ScalaTest is ") lb = new ListBuffer[String] }
def testEasy() { // Uses JUnit-style assertions sb.append("easy!") assertEquals("ScalaTest is easy!", sb.toString) assertTrue(lb.isEmpty) lb += "sweet" }
def testFun() { // Uses ScalaTest assertions sb.append("fun!") assert(sb.toString === "ScalaTest is fun!") assert(lb.isEmpty) } }
TestCase
, or ScalaTest's, inherited from AssertionsForJUnit
.
You can also mix in ShouldMatchersForJUnit
or MustMatchersForJUnit
if you want to use ScalaTests's matchers DSL.
Here's an example:
import org.scalatest.junit.JUnit3Suite import org.scalatest.junit.MustMatchersForJUnit import scala.collection.mutable.ListBufferThe reason you would ordinarily want to mix in
class BlastFromThePastSuite extends JUnit3Suite with MustMatchersForJUnit {
var stringBuilder: StringBuilder = _ var listBuffer: ListBuffer[String] = _
override def setUp() { stringBuilder = new StringBuilder("ScalaTest is ") listBuffer = new ListBuffer[String] }
def testEasy() { stringBuilder.append("easy!") stringBuilder.toString must be ("ScalaTest is easy!") listBuffer must be ('empty) listBuffer += "sweet" }
def testFun() { stringBuilder.append("fun!") stringBuilder.toString must be ("ScalaTest is fun!") listBuffer must be ('empty) } }
MustMatchersForJUnit
or ShouldMatchersForJUnit
rather than MustMatchers
or ShouldMatchers
is that MustMatchersForJUnit
and ShouldMatchersForJUnit
throw
junit.framework.AssertionFailedError
s, which JUnit 3 will report as failures, not errors.
When writing JUnit 3 tests in Scala, you should keep in mind that JUnit 3 will not run tests that have a return type other than
Unit
. Thus it is best to leave off the equals sign before the curly braces of the body of the test, like this:
def testGoodIdea() { // result type will be Unit // ... }Instead of this:
def testBadIdea() = { // result type will be inferred // ... }If the
testBadIdea
method ends in an expression that has a result type other than Unit
, the Scala
compiler will infer a result type to the testBadIdea
method to be the same non-Unit
type. As a "result,"
JUnit 3 will not discover or run the testBadIdea
method at all.
A JUnit Runner
that knows how to run any ScalaTest Suite
.
A JUnit Runner
that knows how to run any ScalaTest Suite
.
This enables you to provide a JUnit RunWith
annotation on any
ScalaTest Suite
. Here's an example:
import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import org.scalatest.FunSuiteThis
@RunWith(classOf[JUnitRunner]) class MySuite extends FunSuite { // ... }
RunWith
annotation will enable the MySuite
class
to be run by JUnit 4.
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 4 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.junit.JUnitSuite import scala.collection.mutable.ListBuffer import _root_.org.junit.Test import _root_.org.junit.BeforeTo execute
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) } }
JUnitSuite
s with ScalaTest's Runner
, you must include JUnit's jar file on the class path or runpath.
This version of JUnitSuite
was tested with JUnit version 4.10.
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 or ScalaTest.
Implementation trait for class JUnitSuite
, which represents
a suite of tests that can be run with either JUnit 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.
Exception that indicates a test failed.
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
junit.framework.AssertionFailedError
. Instances of this class are thrown by the
assertions provided by AssertionsForJUnit
, and matcher
expressions provided by ShouldMatchersForJUnit
, and
MustMatchersForJUnit
.
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, AssertionFailedError
was made to extend java.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 the "ForJUnit
" of ScalaTest assertions and matchers.
if either message
or cause
is null
, or Some(null)
.
A wrapper to allow JUnit tests to be run by the ScalaTest runner.
A wrapper to allow JUnit tests to be run by the ScalaTest runner.
Instances of this trait are not thread safe.
Trait that makes ScalaTest's ShouldMatchers
DSL syntax available for use with JUnit.
Trait that makes ScalaTest's ShouldMatchers
DSL syntax available for use with JUnit.
The assertion methods provided in this trait look and behave exactly like the ones in
ShouldMatchers
, except instead of throwing
TestFailedException
they throw
JUnitTestFailedError
,
which extends junit.framework.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 in junit.framework.Assert
(such as assertEquals
,
assertTrue
, and fail
) is, therefore, AssertionFailedError
.
In JUnit 4, AssertionFailedError
was made to extend java.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 this
ShouldMatchersForJUnit
trait instead of plain-old ScalaTest
ShouldMatchers
.
To use this trait in a JUnit 3 TestCase
, you can mix it into your TestCase
class, like this:
import junit.framework.TestCase import org.scalatest.junit.ShouldMatchersForJUnitYou can alternatively import the methods defined in this trait.
class MyTestCase extends TestCase with ShouldMatchersForJUnit {
def testSomething() { "hello, world!" should startWith ("hello") }
// ... }
import junit.framework.TestCase import org.scalatest.junit.ShouldMatchersForJUnit._For details on the importing approach, see the documentation for the
class MyTestCase extends TestCase {
def testSomething() { "hello, world!" should startWith ("hello") }
// ... }
ShouldMatchersForJUnit
companion object.
For the details on the ShouldMatchersForJUnit
syntax, see the Scaladoc documentation for
org.scalatest.matchers.ShouldMatchers
Please use org.scalatest.MustMatchers with AssertionsForJUnit instead.
Trait that makes ScalaTest's ShouldMatchers
DSL syntax available for use with JUnit.
Trait that makes ScalaTest's ShouldMatchers
DSL syntax available for use with JUnit.
The assertion methods provided in this trait look and behave exactly like the ones in
ShouldMatchers
, except instead of throwing
TestFailedException
they throw
JUnitTestFailedError
,
which extends junit.framework.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 in junit.framework.Assert
(such as assertEquals
,
assertTrue
, and fail
) is, therefore, AssertionFailedError
.
In JUnit 4, AssertionFailedError
was made to extend java.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 this
ShouldMatchersForJUnit
trait instead of plain-old ScalaTest
ShouldMatchers
.
To use this trait in a JUnit 3 TestCase
, you can mix it into your TestCase
class, like this:
import junit.framework.TestCase import org.scalatest.junit.ShouldMatchersForJUnitYou can alternatively import the methods defined in this trait.
class MyTestCase extends TestCase with ShouldMatchersForJUnit {
def testSomething() { "hello, world!" should startWith ("hello") }
// ... }
import junit.framework.TestCase import org.scalatest.junit.ShouldMatchersForJUnit._For details on the importing approach, see the documentation for the
class MyTestCase extends TestCase {
def testSomething() { "hello, world!" should startWith ("hello") }
// ... }
ShouldMatchersForJUnit
companion object.
For the details on the ShouldMatchersForJUnit
syntax, see the Scaladoc documentation for
org.scalatest.matchers.ShouldMatchers
Please use org.scalatest.Matchers with AssertionsForJUnit instead.
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:
$ scala -cp junit3.8.2/junit.jar:../target/jar_contents Welcome to Scala version 2.7.5.final (Java HotSpot(TM) Client VM, Java 1.5.0_16). Type in expressions to have them evaluated. Type :help for more information. scala> import org.scalatest.junit.AssertionsForJUnit._ import org.scalatest.junit.AssertionsForJUnit._ scala> assert(1 === 2) junit.framework.AssertionFailedError: 1 did not equal 2 at org.scalatest.junit.AssertionsForJUnit$class.assert(AssertionsForJUnit.scala:353) at org.scalatest.junit.AssertionsForJUnit$.assert(AssertionsForJUnit.scala:672) at .( :7) at . ( ) at RequestResult$. ( :3) at RequestResult$. ( ) at RequestResult$result( expect(3) { 1 + 3 } junit.framework.AssertionFailedError: Expected 3, but got 4 at org.scalatest.junit.AssertionsForJUnit$class.expect(AssertionsForJUnit.scala:563) at org.scalatest.junit.AssertionsForJUnit$.expect(AssertionsForJUnit.scala:672) at . ( :7) at . ( ) at RequestResult$. ( :3) at RequestResult$. ( ) at RequestResult$result( val caught = intercept[StringIndexOutOfBoundsException] { "hi".charAt(-1) } caught: StringIndexOutOfBoundsException = java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Companion object that facilitates the importing of ShouldMatchersForJUnit
members as
an alternative to mixing it in.
Companion object that facilitates the importing of ShouldMatchersForJUnit
members as
an alternative to mixing it in. One use case is to import ShouldMatchersForJUnit
members so you can use
them in the Scala interpreter:
Macintosh-65:delus bv$ scala -cp .:../target/jar_contents:junit3.8.2/junit.jar Welcome to Scala version 2.7.5.final (Java HotSpot(TM) Client VM, Java 1.5.0_16). Type in expressions to have them evaluated. Type :help for more information. scala> import org.scalatest.junit.ShouldMatchersForJUnit._ import org.scalatest.junit.ShouldMatchersForJUnit._ scala> "hi" should have length (3) junit.framework.AssertionFailedError: "hi" had length 2 instead of expected length 3 at org.scalatest.junit.ShouldMatchersForJUnit$class.newTestFailedException(ShouldMatchersForJUnit.scala:22) at org.scalatest.junit.ShouldMatchersForJUnit$.newTestFailedException(ShouldMatchersForJUnit.scala:63) at org.scalatest.matchers.Matchers$ResultOfHaveWordForString.length(Matchers.scala:4102) at .( 1 should equal (2) junit.framework.AssertionFailedError: 1 did not equal 2 at org.scalatest.junit.ShouldMatchersForJUnit$class.newTestFailedException(ShouldMatchersForJUnit.scala:22) at org.scalatest.junit.ShouldMatchersForJUnit$.newTestFailedException(ShouldMatchersForJUnit.scala:63) at org.scalatest.matchers.ShouldMatchers$ShouldMethodHelper$.shouldMatcher(ShouldMatchers.scala:800) at org.scal... scala> "hello, world" should startWith ("hello") scala> 7 should (be >= (3) and not be <= (7)) junit.framework.AssertionFailedError: 7 was greater than or equal to 3, but 7 was less than or equal to 7 at org.scalatest.junit.ShouldMatchersForJUnit$class.newTestFailedException(ShouldMatchersForJUnit.scala:22) at org.scalatest.junit.ShouldMatchersForJUnit$.newTestFailedException(ShouldMatchersForJUnit.scala:63) at org.scalatest.matchers.ShouldMatchers$ShouldMethodHelper$.sh...
Please use org.scalatest.MustMatchers with AssertionsForJUnit instead.
Companion object that facilitates the importing of ShouldMatchersForJUnit
members as
an alternative to mixing it in.
Companion object that facilitates the importing of ShouldMatchersForJUnit
members as
an alternative to mixing it in. One use case is to import ShouldMatchersForJUnit
members so you can use
them in the Scala interpreter:
Macintosh-65:delus bv$ scala -cp .:../target/jar_contents:junit3.8.2/junit.jar Welcome to Scala version 2.7.5.final (Java HotSpot(TM) Client VM, Java 1.5.0_16). Type in expressions to have them evaluated. Type :help for more information. scala> import org.scalatest.junit.ShouldMatchersForJUnit._ import org.scalatest.junit.ShouldMatchersForJUnit._ scala> "hi" should have length (3) junit.framework.AssertionFailedError: "hi" had length 2 instead of expected length 3 at org.scalatest.junit.ShouldMatchersForJUnit$class.newTestFailedException(ShouldMatchersForJUnit.scala:22) at org.scalatest.junit.ShouldMatchersForJUnit$.newTestFailedException(ShouldMatchersForJUnit.scala:63) at org.scalatest.matchers.Matchers$ResultOfHaveWordForString.length(Matchers.scala:4102) at .( 1 should equal (2) junit.framework.AssertionFailedError: 1 did not equal 2 at org.scalatest.junit.ShouldMatchersForJUnit$class.newTestFailedException(ShouldMatchersForJUnit.scala:22) at org.scalatest.junit.ShouldMatchersForJUnit$.newTestFailedException(ShouldMatchersForJUnit.scala:63) at org.scalatest.matchers.ShouldMatchers$ShouldMethodHelper$.shouldMatcher(ShouldMatchers.scala:800) at org.scal... scala> "hello, world" should startWith ("hello") scala> 7 should (be >= (3) and not be <= (7)) junit.framework.AssertionFailedError: 7 was greater than or equal to 3, but 7 was less than or equal to 7 at org.scalatest.junit.ShouldMatchersForJUnit$class.newTestFailedException(ShouldMatchersForJUnit.scala:22) at org.scalatest.junit.ShouldMatchersForJUnit$.newTestFailedException(ShouldMatchersForJUnit.scala:63) at org.scalatest.matchers.ShouldMatchers$ShouldMethodHelper$.sh...
Please use org.scalatest.Matchers with AssertionsForJUnit instead.
Trait that contains ScalaTest's basic assertion methods, suitable for use with JUnit.
The assertion methods provided in this trait look and behave exactly like the ones in
Assertions
, except instead of throwingTestFailedException
they throwJUnitTestFailedError
, which extendsjunit.framework.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,
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
.To use this trait in a JUnit 3
You can alternatively import the methods defined in this trait. For details on the importing approach, see the documentation for theTestCase
, you can mix it into yourTestCase
class, like this:AssertionsForJUnit
companion object. For the details on theAssertionsForJUnit
syntax, see the Scaladoc documentation fororg.scalatest.Assertions