org.scalatest.junit

trait ShouldMatchersForJUnit

[source: org/scalatest/junit/ShouldMatchersForJUnit.scala]

trait ShouldMatchersForJUnit
extends ShouldMatchers with AssertionsForJUnit
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.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 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.ShouldMatchersForJUnit

 class MyTestCase extends TestCase with ShouldMatchersForJUnit {

   def testSomething() {
     "hello, world!" should startWith ("hello")
   }

   // ...
 }
 

You can alternatively import the methods defined in this trait.

 import junit.framework.TestCase
 import org.scalatest.junit.ShouldMatchersForJUnit._

 class MyTestCase extends TestCase {

   def testSomething() {
     "hello, world!" should startWith ("hello")
   }

   // ...
 }
 

For details on the importing approach, see the documentation for the ShouldMatchersForJUnit companion object. For the details on the ShouldMatchersForJUnit syntax, see the Scaladoc documentation for org.scalatest.matchers.ShouldMatchers

Author
Bill Venners
Direct Known Subclasses:
ShouldMatchersForJUnit

Values and Variables inherited from Matchers
not, be, have, contain, include, fullyMatch, startWith, endWith, length, size, key, value, a, an, theSameInstanceAs, regex
Methods inherited from ShouldMatchers
convertToEvaluatingApplicationShouldWrapper, convertToAnyShouldWrapper, convertToDoubleShouldWrapper, convertToFloatShouldWrapper, convertToLongShouldWrapper, convertToIntShouldWrapper, convertToShortShouldWrapper, convertToByteShouldWrapper, convertToAnyRefShouldWrapper, convertToCollectionShouldWrapper, convertToSeqShouldWrapper, convertToArrayShouldWrapper, convertToListShouldWrapper, convertToMapShouldWrapper, convertToStringShouldWrapper, convertToJavaCollectionShouldWrapper, convertToJavaListShouldWrapper, convertToJavaMapShouldWrapper, convertHasIntGetLengthMethodToLengthShouldWrapper, convertHasIntGetLengthFieldToLengthShouldWrapper, convertHasIntLengthFieldToLengthShouldWrapper, convertHasIntLengthMethodToLengthShouldWrapper, convertHasLongGetLengthMethodToLengthShouldWrapper, convertHasLongGetLengthFieldToLengthShouldWrapper, convertHasLongLengthFieldToLengthShouldWrapper, convertHasLongLengthMethodToLengthShouldWrapper, convertHasIntGetSizeMethodToSizeShouldWrapper, convertHasIntGetSizeFieldToSizeShouldWrapper, convertHasIntSizeFieldToSizeShouldWrapper, convertHasIntSizeMethodToSizeShouldWrapper, convertHasLongGetSizeMethodToSizeShouldWrapper, convertHasLongGetSizeFieldToSizeShouldWrapper, convertHasLongSizeFieldToSizeShouldWrapper, convertHasLongSizeMethodToSizeShouldWrapper
Methods inherited from Matchers
convertToMatcherWrapper, convertIterableMatcherToJavaCollectionMatcher, convertMapMatcherToJavaMapMatcher, convertLengthFieldToIntLengthWrapper, convertLengthMethodToIntLengthWrapper, convertGetLengthFieldToIntLengthWrapper, convertGetLengthMethodToIntLengthWrapper, convertLengthFieldToLongLengthWrapper, convertLengthMethodToLongLengthWrapper, convertGetLengthFieldToLongLengthWrapper, convertGetLengthMethodToLongLengthWrapper, convertSizeFieldToIntSizeWrapper, convertSizeMethodToIntSizeWrapper, convertGetSizeFieldToIntSizeWrapper, convertGetSizeMethodToIntSizeWrapper, convertSizeFieldToLongSizeWrapper, convertSizeMethodToLongSizeWrapper, convertGetSizeFieldToLongSizeWrapper, convertGetSizeMethodToLongSizeWrapper, convertSymbolToHavePropertyMatcherGenerator, equal, convertDoubleToPlusOrMinusWrapper, convertFloatToPlusOrMinusWrapper, convertLongToPlusOrMinusWrapper, convertIntToPlusOrMinusWrapper, convertShortToPlusOrMinusWrapper, convertByteToPlusOrMinusWrapper, <, >, <=, >=, ===, evaluating, produce
Methods inherited from Assertions
assert, assert, assert, assert, convertToEqualizer, intercept, expect, expect, fail, fail, fail, fail
Methods inherited from AnyRef
getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf

Copyright (C) 2001-2010 Artima, Inc. All rights reserved.