package junit
- Alphabetic
- Public
- All
Type Members
-
trait
AssertionsForJUnit
extends Assertions
Trait that contains ScalaTest's basic assertion methods, suitable for use with JUnit.
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
TestCase
, you can mix it into yourTestCase
class, like this:import junit.framework.TestCase import org.scalatest.junit.AssertionsForJUnit class MyTestCase extends TestCase with AssertionsForJUnit { def testSomething() { assert("hi".charAt(1) === 'i') } // ... }
You can alternatively import the methods defined in this trait.
import junit.framework.TestCase import org.scalatest.junit.AssertionsForJUnit._ class MyTestCase extends TestCase { def testSomething() { assert("hi".charAt(1) === 'i') } // ... }
For details on the importing approach, see the documentation for the
AssertionsForJUnit
companion object. For the details on theAssertionsForJUnit
syntax, see the Scaladoc documentation fororg.scalatest.Assertions
-
class
JUnit3Suite
extends TestCase with Suite with AssertionsForJUnit
A
Suite
that is also ajunit.framework.TestCase
.A
Suite
that is also ajunit.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 3TestCase
. Tests are methods that start withtest
, take no parameters, and have aUnit
return type. You manage fixtures with methodssetUp
andtearDown
. Here's an example:import org.scalatest.junit.JUnit3Suite import scala.collection.mutable.ListBuffer class BlastFromThePastSuite extends JUnit3Suite { var sb: StringBuilder = _ var lb: ListBuffer[String] = _ override def setUp(): Unit = { sb = new StringBuilder("ScalaTest is ") lb = new ListBuffer[String] } def testEasy(): Unit = { // Uses JUnit-style assertions sb.append("easy!") assertEquals("ScalaTest is easy!", sb.toString) assertTrue(lb.isEmpty) lb += "sweet" } def testFun(): Unit = { // Uses ScalaTest assertions sb.append("fun!") assert(sb.toString === "ScalaTest is fun!") assert(lb.isEmpty) } }
You can use either JUnit's assertions, inherited from
TestCase
, or ScalaTest's, inherited fromAssertionsForJUnit
.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 explicitly state theUnit
result type, like this:def testGoodIdea(): Unit = { // 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 thanUnit
, the Scala compiler will infer a result type to thetestBadIdea
method to be the same non-Unit
type. As a "result," JUnit 3 will not discover or run thetestBadIdea
method at all. -
final
class
JUnitRunner
extends Runner
A JUnit
Runner
that knows how to run any ScalaTestSuite
.A JUnit
Runner
that knows how to run any ScalaTestSuite
. This enables you to provide a JUnitRunWith
annotation on any ScalaTestSuite
. Here's an example:import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import org.scalatest.FunSuite @RunWith(classOf[JUnitRunner]) class MySuite extends FunSuite { // ... }
This
RunWith
annotation will enable theMySuite
class to be run by JUnit 4. -
class
JUnitSuite
extends JUnitSuiteLike
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 withTest
, and can create fixtures with methods annotated withBefore
andAfter
. For example:import org.scalatest.junit.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'sRunner
, you must include JUnit's jar file on the class path or runpath. This version ofJUnitSuite
was tested with JUnit version 4.10.Instances of this class are not thread safe.
-
trait
JUnitSuiteLike
extends Suite with AssertionsForJUnit
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 ofJUnitSuite
into some other class, you can use this trait instead, because classJUnitSuite
does nothing more than extend this trait.See the documentation of the class for a detailed overview of
JUnitSuite
. -
class
JUnitTestFailedError
extends AssertionFailedError with StackDepth with ModifiableMessage[JUnitTestFailedError] with PayloadField with ModifiablePayload[JUnitTestFailedError]
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 extendsjunit.framework.AssertionFailedError
. Instances of this class are thrown by the assertions provided byAssertionsForJUnit
.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 injunit.framework.Assert
(such asassertEquals
,assertTrue
, andfail
) was, 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 useAssertionsForJUnit
.- Exceptions thrown
NullArgumentException
if eithermessage
orcause
isnull
, orSome(null)
.
-
class
JUnitWrapperSuite
extends Suite
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.
Value Members
-
object
AssertionsForJUnit
extends AssertionsForJUnit
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 importAssertionsForJUnit
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