All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scalatest.junit.MustMatchersForJUnit.scala Maven / Gradle / Ivy

Go to download

ScalaTest is a free, open-source testing toolkit for Scala and Java programmers.

There is a newer version: 2.0.M6-SNAP4
Show newest version
/*
 * Copyright 2001-2008 Artima, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.scalatest.junit

import org.scalatest._
import _root_.junit.framework.AssertionFailedError
import org.scalatest.matchers.MustMatchers

/**
 * Trait that makes ScalaTest's MustMatchers DSL syntax available for use with JUnit.
 *
 * 

* The assertion methods provided in this trait look and behave exactly like the ones in * MustMatchers, 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 * MustMatchersForJUnit trait instead of plain-old ScalaTest * MustMatchers. *

* *

* 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.MustMatchersForJUnit
 *
 * class MyTestCase extends TestCase with MustMatchersForJUnit {
 *
 *   def testSomething() {
 *     "hello, world!" must startWith ("hello")
 *   }
 *
 *   // ...
 * }
 * 
* *

* You can alternatively import the methods defined in this trait. *

* *
 * import junit.framework.TestCase
 * import org.scalatest.junit.MustMatchersForJUnit._
 *
 * class MyTestCase extends TestCase {
 *
 *   def testSomething() {
 *     "hello, world!" must startWith ("hello")
 *   }
 *
 *   // ...
 * }
 * 
* *

* For details on the importing approach, see the documentation * for the MustMatchersForJUnit companion object. * For the details on the MustMatchersForJUnit syntax, see the Scaladoc documentation for * org.scalatest.matchers.MustMatchers *

* * @author Bill Venners */ trait MustMatchersForJUnit extends MustMatchers with AssertionsForJUnit { //private[scalatest] override def newTestFailedException(message: String): Throwable = new AssertionFailedError(message) private[scalatest] override def newTestFailedException(message: String, optionalCause: Option[Throwable] = None, stackDepthAdjustment: Int): Throwable = { val fileNames = List("Matchers.scala", "MustMatchers.scala", "MustMatchers.scala", "MustMatchersForJUnit.scala", "MustMatchersForJUnit.scala") val temp = new RuntimeException val stackDepth = temp.getStackTrace.takeWhile(stackTraceElement => fileNames.exists(_ == stackTraceElement.getFileName) || stackTraceElement.getMethodName == "newTestFailedException").length optionalCause match { case Some(cause) => new JUnitTestFailedError(message, cause, stackDepth + stackDepthAdjustment) case None => new JUnitTestFailedError(message, stackDepth + stackDepthAdjustment) } } } /** * Companion object that facilitates the importing of MustMatchersForJUnit members as * an alternative to mixing it in. One use case is to import MustMatchersForJUnit 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.MustMatchersForJUnit._
 * import org.scalatest.junit.MustMatchersForJUnit._
 * 
 * scala> "hi" must have length (3)
 * junit.framework.AssertionFailedError: "hi" did not have length 3
 * 	at org.scalatest.junit.MustMatchersForJUnit$class.newTestFailedException(MustMatchersForJUnit.scala:22)
 * 	at org.scalatest.junit.MustMatchersForJUnit$.newTestFailedException(MustMatchersForJUnit.scala:63)
 * 	at org.scalatest.matchers.Matchers$ResultOfHaveWordForString.length(Matchers.scala:4102)
 * 	at .( 1 must equal (2)
 * junit.framework.AssertionFailedError: 1 did not equal 2
 * 	at org.scalatest.junit.MustMatchersForJUnit$class.newTestFailedException(MustMatchersForJUnit.scala:22)
 * 	at org.scalatest.junit.MustMatchersForJUnit$.newTestFailedException(MustMatchersForJUnit.scala:63)
 * 	at org.scalatest.matchers.MustMatchers$MustMethodHelper$.mustMatcher(MustMatchers.scala:800)
 * 	at org.scal...
 * scala> "hello, world" must startWith ("hello")
 * 
 * scala> 7 must (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.MustMatchersForJUnit$class.newTestFailedException(MustMatchersForJUnit.scala:22)
 * 	at org.scalatest.junit.MustMatchersForJUnit$.newTestFailedException(MustMatchersForJUnit.scala:63)
 * 	at org.scalatest.matchers.MustMatchers$MustMethodHelper$.sh...
 * 
* * @author Bill Venners */ object MustMatchersForJUnit extends MustMatchersForJUnit




© 2015 - 2024 Weber Informatics LLC | Privacy Policy