Package

org.scalatest

junit

Permalink

package junit

Visibility
  1. Public
  2. All

Type Members

  1. trait AssertionsForJUnit extends Assertions

    Permalink

    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 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.AssertionFailedErrors 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 AssertionsForJUnit trait instead of plain-old ScalaTest Assertions.

    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.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 the AssertionsForJUnit syntax, see the Scaladoc documentation for org.scalatest.Assertions

  2. class JUnit3Suite extends TestCase with Suite with AssertionsForJUnit

    Permalink

    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.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 from AssertionsForJUnit.

    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 the Unit 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 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.

  3. final class JUnitRunner extends Runner

    Permalink

    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.FunSuite
    
    @RunWith(classOf[JUnitRunner])
    class MySuite extends FunSuite {
      // ...
    }
    

    This RunWith annotation will enable the MySuite class to be run by JUnit 4.

  4. class JUnitSuite extends JUnitSuiteLike

    Permalink

    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.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'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.

  5. trait JUnitSuiteLike extends Suite with AssertionsForJUnit

    Permalink

    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.

  6. class JUnitTestFailedError extends AssertionFailedError with StackDepth with ModifiableMessage[JUnitTestFailedError] with PayloadField with ModifiablePayload[JUnitTestFailedError]

    Permalink

    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.

    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 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 AssertionsForJUnit.

    Exceptions thrown

    NullArgumentException if either message or cause is null, or Some(null).

  7. class JUnitWrapperSuite extends Suite

    Permalink

    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

  1. object AssertionsForJUnit extends AssertionsForJUnit

    Permalink

    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
    

Ungrouped