package junit
- Alphabetic
- Public
- Protected
Type Members
- trait AssertionsForJUnit extends VersionSpecificAssertionsForJUnit
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 throwingTestFailedExceptionthey 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.AssertionFailedErrors 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,
AssertionFailedErrorwas 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 thisAssertionsForJUnittrait instead of plain-old ScalaTestAssertions.To use this trait in a JUnit 3
TestCase, you can mix it into yourTestCaseclass, 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
AssertionsForJUnitcompanion object. For the details on theAssertionsForJUnitsyntax, see the Scaladoc documentation fororg.scalatest.Assertions - class JUnit3Suite extends TestCase with Suite with AssertionsForJUnit
A
Suitethat is also ajunit.framework.TestCase.A
Suitethat is also ajunit.framework.TestCase.A
JUnit3Suitemay 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 aUnitreturn type. You manage fixtures with methodssetUpandtearDown. 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 theUnitresult type, like this:def testGoodIdea(): Unit = { // result type will be Unit // ... }
Instead of this:
def testBadIdea() = { // result type will be inferred // ... }
If the
testBadIdeamethod ends in an expression that has a result type other thanUnit, the Scala compiler will infer a result type to thetestBadIdeamethod to be the same non-Unittype. As a "result," JUnit 3 will not discover or run thetestBadIdeamethod at all. - final class JUnitRunner extends Runner
A JUnit
Runnerthat knows how to run any ScalaTestSuite.A JUnit
Runnerthat knows how to run any ScalaTestSuite. This enables you to provide a JUnitRunWithannotation 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
RunWithannotation will enable theMySuiteclass 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 withBeforeandAfter. 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
JUnitSuites with ScalaTest'sRunner, you must include JUnit's jar file on the class path or runpath. This version ofJUnitSuitewas 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.JUnitSuiteis 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 ofJUnitSuiteinto some other class, you can use this trait instead, because classJUnitSuitedoes 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
TestFailedExceptionin 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.AssertionFailedErrors 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,AssertionFailedErrorwas 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
NullArgumentExceptionif eithermessageorcauseisnull, 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.
- trait VersionSpecificAssertionsForJUnit extends Assertions
Value Members
- object AssertionsForJUnit extends AssertionsForJUnit
Companion object that facilitates the importing of
AssertionsForJUnitmembers as an alternative to mixing it in.Companion object that facilitates the importing of
AssertionsForJUnitmembers as an alternative to mixing it in. One use case is to importAssertionsForJUnitmembers 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