org.scalatest.junit.JUnit3ComfortSuite.scala Maven / Gradle / Ivy
Show all versions of scalatest_2.11.0-M3 Show documentation
/* * 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.{TestCase, Assert, AssertionFailedError} import org.junit.runner.RunWith /** * A suite of tests that mimics the syntax of a JUnit 3
also completes abruptly with an exception, this * method will nevertheless complete abruptly with the exception previously thrown byTestCase
, which can be run with either * JUnit 4 or ScalaTest. * * This class allows you to write tests with ScalaTest's more concise assertion syntax as well as JUnit-style assertions (assertEquals
, etc.). * You create tests by defining methods that start withtest
, and can create fixtures with methods * namedsetUp
andtearDown
.JUnit3ComfortSuite
is intended for people who are * familiar with JUnit 3 and want to get started quickly writing tests with ScalaTest. Here's an example: * ** import org.scalatest.junit.JUnit3ComfortSuite * import scala.collection.mutable.ListBuffer * * class BlastFromThePastSuite extends JUnit3ComfortSuite { * * var sb: StringBuilder = _ * var lb: ListBuffer[String] = _ * * override def setUp() { * sb = new StringBuilder("ScalaTest is ") * lb = new ListBuffer[String] * } * * def testEasy() { // Uses JUnit-style assertions * sb.append("easy!") * assertEquals("ScalaTest is easy!", sb.toString) * assertTrue(lb.isEmpty) * lb += "sweet" * } * * def testFun() { // Uses ScalaTest assertions * sb.append("fun!") * assert(sb.toString === "ScalaTest is fun!") * assert(lb.isEmpty) * } * } ** ** To execute a
* *JUnit3ComfortSuite
s with ScalaTest'sRunner
, you must include a JUnit 4 jar file on the class path or runpath. ** One difference between a
* *JUnit3ComfortSuite
and a JUnit 3TestCase
* is that tags are supported in aJUnit3ComfortSuite
, but JUnit 3 had no such * concept. Tags work in aJUnit3ComfortSuite
exactly like they work inSuite
: * you place annotations on the test methods. This allows you, for example, to ignore a test in * aJUnit3ComfortSuite
like this: ** @Ignore * def testSubtraction() { * val diff = 4 - 1 * assert(diff === 3) * assert(diff - 2 === 1) * } ** ** Note: the reason
* * @author Bill Venners */ @RunWith(classOf[JUnitRunner]) private [junit] class JUnit3ComfortSuite extends Suite with OneInstancePerTest { /** * Defines a method to be run before each of this suite's tests. This trait's implementation * ofJUnit3ComfortSuite
is not namedJUnit3Suite
is because that name might imply * that an instance of this class is a JUnit 3TestCase
, just asJUnitSuite
is an actual JUnit 4 test * class andTestNGSuite
is an actual TestNG test class. By contrast, aJUnit3ComfortSuite
is not actually * a JUnit 3TestCase
, it just looks and behaves like one. Thus it gives you that comfortable familiar feeling of * programming with JUnit 3, without actually being JUnit 3. *runTest
invokes this method before running * each test, thus this method can be used to set up a test fixture * needed by each test. This trait's implementation of this method does nothing. */ protected def setUp() = () /** * Defines a method to be run after each of this suite's tests. This trait's implementation * ofrunTest
invokes this method after running * each test, thus this method can be used to tear down a test fixture * needed by each test. This trait's implementation of this method does nothing. */ protected def tearDown() = () /** * Run a test surrounded by calls tosetUp
andtearDown
. * This trait's implementation of this method ("this method") invokessetUp
* before running each test andtearDown
* after running each test. It runs each test by invokingsuper.runTest
, passing along * the four parameters passed to it. * ** If any invocation of
setUp
completes abruptly with an exception, this * method will complete abruptly with the same exception. If any call to *super.runTest
completes abruptly with an exception, this method * will complete abruptly with the same exception, however, before doing so, it will * invoketearDown
. IftearDown super.runTest
. * Ifsuper.runTest
returns normally, buttearDown
completes abruptly with an * exception, this method will complete abruptly with the same exception. * */ override def runTest(testName: String, reporter: Reporter, stopper: Stopper, configMap: Map[String, Any], tracker: Tracker) { var thrownException: Option[Throwable] = None setUp() try { super.runTest(testName, reporter, stopper, configMap, tracker) } catch { case e: Exception => thrownException = Some(e) } finally { try { tearDown() // Make sure that afterEach is called even if runTest completes abruptly. thrownException match { case Some(e) => throw e case None => } } catch { case laterException: Exception => thrownException match { // If both run and afterAll throw an exception, report the test exception case Some(earlierException) => throw earlierException case None => throw laterException } } } } /** * Assert that a boolean condition, described inString
*message
, is true. * If the condition istrue
, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param condition the boolean condition to assert * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the condition isfalse
. */ def assertTrue(message: String, condition: Boolean) { Assert.assertTrue(message, condition) } /** * Assert that a boolean condition is true. * If the condition istrue
, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param condition the boolean condition to assert * @throws AssertionFailedError if the condition isfalse
. */ def assertTrue(condition: Boolean) { Assert.assertTrue(condition) } /** * Assert that a boolean condition, described inString
*message
, is false. * If the condition isfalse
, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param condition the boolean condition to ensure is false * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the condition istrue
. */ def assertFalse(message: String, condition: Boolean) { Assert.assertFalse(message, condition) } /** * Assert that a boolean condition is false. * If the condition istrue
, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param condition the boolean condition to assert * @throws AssertionFailedError if the condition isfalse
. */ def assertFalse(condition: Boolean) { Assert.assertFalse(condition) } /** * ThrowsAssertionFailedError
, with the passed *String
message
as the exception's detail * message, to indicate a test failed. * * @param message A message describing the failure. */ override def fail(message: String) = throw new AssertionFailedError(message) /** * ThrowsAssertionFailedError
to indicate a test failed. */ override def fail(): Nothing = throw new AssertionFailedError /** * Assert that two objects are equal, with additional information described in aString
*message
. * If the two objects are equal, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the two objects are not equal. */ def assertEquals(message: String, expected: AnyRef, actual: AnyRef) { Assert.assertEquals(message, expected, actual) } /** * Assert that a two objects are equal. * If the two objects are equal, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the two objects are equal. */ def assertEquals(expected: AnyRef, actual: AnyRef) { Assert.assertEquals(expected, actual) } /** * Assert that twoString
s are equal, with additional information described in aString
*message
. * If the twoString
s are equal, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the twoString
s are not equal. */ def assertEquals(message: String, expected: String, actual: String) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoString
s are equal. * If the twoString
s are equal, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoString
s are not equal. */ def assertEquals(expected: String, actual: String) { Assert.assertEquals(expected, actual) } /** * Assert that twoDouble
s are equal within a tolerance specified bydelta
, * with additional information described in aString
message
. * If the twoDouble
s are equal within the tolerance, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * If the expected value is infinity then the delta value is ignored. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the twoDouble
s are not equal within the tolerance specified bydelta
. */ def assertEquals(message: String, expected: Double, actual: Double, delta: Double) { Assert.assertEquals(message, expected, actual, delta) } /** * Assert that a twoDouble
s are equal within a tolerance specified bydelta
. * If the twoDouble
s are equal within the tolerance, this method returns normally. * Else, it throwsAssertionFailedError
. * If the expected value is infinity then the delta value is ignored. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoDouble
s are not equal within the tolerance specified bydelta
. */ def assertEquals(expected: Double, actual: Double, delta: Double) { Assert.assertEquals(expected, actual, delta) } /** * Assert that twoFloat
s are equal within a tolerance specified bydelta
, * with additional information described in aString
message
. * If the twoFloat
s are equal within the tolerance, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * If the expected value is infinity then the delta value is ignored. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the twoFloat
s are not equal within the tolerance specified bydelta
. */ def assertEquals(message: String, expected: Float, actual: Float, delta: Float) { Assert.assertEquals(message, expected, actual, delta) } /** * Assert that a twoFloat
s are equal within a tolerance specified bydelta
. * If the twoFloat
s are equal within the tolerance, this method returns normally. * Else, it throwsAssertionFailedError
. * If the expected value is infinity then the delta value is ignored. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoFloat
s are not equal within the tolerance specified bydelta
. */ def assertEquals(expected: Float, actual: Float, delta: Float) { Assert.assertEquals(expected, actual, delta) } /** * Assert that twoLong
s are equal within a tolerance specified bydelta
, * with additional information described in aString
message
. * If the twoLong
s are equal, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the twoLong
s are not equal. */ def assertEquals(message: String, expected: Long, actual: Long) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoLong
s are equal. * If the twoLong
s are equal, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoLong
s are equal. */ def assertEquals(expected: Long, actual: Long) { Assert.assertEquals(expected, actual) } /** * Assert that twoBoolean
s are equal, with additional information described in aString
*message
. * If the twoBoolean
s are equal, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the twoBoolean
s are not equal. */ def assertEquals(message: String, expected: Boolean, actual: Boolean) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoBoolean
s are equal. * If the twoBoolean
s are equal, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoBoolean
s are equal. */ def assertEquals(expected: Boolean, actual: Boolean) { Assert.assertEquals(expected, actual) } /** * Assert that twoByte
s are equal, with additional information described in aString
*message
. * If the twoByte
s are equal, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the twoByte
s are not equal. */ def assertEquals(message: String, expected: Byte, actual: Byte) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoByte
s are equal. * If the twoByte
s are equal, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoByte
s are equal. */ def assertEquals(expected: Byte, actual: Byte) { Assert.assertEquals(expected, actual) } /** * Assert that twoChar
s are equal, with additional information described in aString
*message
. * If the twoChar
s are equal, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the twoChar
s are not equal. */ def assertEquals(message: String, expected: Char, actual: Char) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoChar
s are equal. * If the twoChar
s are equal, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoChar
s are equal. */ def assertEquals(expected: Char, actual: Char) { Assert.assertEquals(expected, actual) } /** * Assert that twoShort
s are equal, with additional information described in aString
*message
. * If the twoShort
s are equal, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the twoShort
s are not equal. */ def assertEquals(message: String, expected: Short, actual: Short) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoShort
s are equal. * If the twoShort
s are equal, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoShort
s are equal. */ def assertEquals(expected: Short, actual: Short) { Assert.assertEquals(expected, actual) } /** * Assert that twoInt
s are equal, with additional information described in aString
*message
. * If the twoInt
s are equal, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the twoInt
s are not equal. */ def assertEquals(message: String, expected: Int, actual: Int) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoInt
s are equal. * If the twoInt
s are equal, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoInt
s are equal. */ def assertEquals(expected: Int, actual: Int) { Assert.assertEquals(expected, actual) } /** * Assert that an object reference is notnull
. * If the object is non-null
, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param anyRef the object reference to check fornull
* @throws AssertionFailedError if the object reference isnull
*/ def assertNotNull(anyRef: AnyRef) { Assert.assertNotNull(anyRef) } /** * Assert that an object reference is notnull
, with * additional information described in aString
message
. * If the object is non-null
, this method returns normally. * Else, it throwsAssertionFailedError
with the * specifiedmessage
as the exception's detail message. * * @param anyRef the object reference to check fornull
* @param message aString
message to include in a failure report. * @throws AssertionFailedError if the object reference isnull
*/ def assertNotNull(message: String, anyRef: AnyRef) { Assert.assertNotNull(message, anyRef) } /** * Assert that an object reference isnull
. * If the object isnull
, this method returns normally. * Else, it throwsAssertionFailedError
. * * @param anyRef the object reference to ensure isnull
* @throws AssertionFailedError if the object reference is non-null
*/ def assertNull(anyRef: AnyRef) { Assert.assertNull(anyRef) } /** * Assert that an object reference isnull
, with * additional information described in aString
message
. * If the object isnull
, this method returns normally. * Else, it throwsAssertionFailedError
with the * specifiedmessage
as the exception's detail message. * * @param anyRef the object reference to ensure isnull
* @param message aString
message to include in a failure report. * @throws AssertionFailedError if the object reference is non-null
*/ def assertNull(message: String, anyRef: AnyRef) { Assert.assertNull(message, anyRef) } /** * Assert that two object references refer to the same object, * with additional information described in aString
message
. * If the two object references refer to the same object, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the two object references refer to different objects. */ def assertSame(message: String, expected: AnyRef, actual: AnyRef) { Assert.assertSame(message, expected, actual) } /** * Assert that two object references refer to the same object. * If the two object references refer to the same object, this method returns normally. * Else, it throwsTestFailedException
. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the two object references refer to different objects. */ def assertSame(expected: AnyRef, actual: AnyRef) { Assert.assertSame(expected, actual) } /** * Assert that two object references do not refer to the same object, * with additional information described in aString
message
. * If the two object references refer to different objects, this method returns normally. * Else, it throwsTestFailedException
with the * specifiedmessage
as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aString
message to include in a failure report. * @throws AssertionFailedError if the two object references refer to the same object. */ def assertNotSame(message: String, expected: AnyRef, actual: AnyRef) { Assert.assertNotSame(message, expected, actual) } /** * Assert that two object references do not refer to the same object. * If the two object references refer to different objects, this method returns normally. * Else, it throwsTestFailedException
. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the two object references refer to the same object. */ def assertNotSame(expected: AnyRef, actual: AnyRef) { Assert.assertNotSame(expected, actual) } /** * Suite style name. */ final override val styleName: String = "JUnit3ComfortSuite" } /* * Note:JUnit3Suite
has been deprecated, and will be removed in a future version of ScalaTest. Please * change to usingJUnit3ComforSuite
instead.JUnit3ComfortSuite
does not extendjunit.framework.TestCase
. * In versions of ScalaTest prior to 0.9.5,JUnit3Suite
extendedTestCase
so that it could be run by a JUnit 3 runner. In * 0.9.6, ScalaTest'sexecute
methods were renamed torun
, which is not compatible withTestCase
. * As a result the goal of providing a trait in ScalaTest that can either run with ScalaTest and JUnit 3 was dropped. Instead, the *JUnit3ComfortSuite
trait can give you that comfortable feeling of using JUnit 3-like syntax, and it can be run with * either ScalaTest or JUnit 4. */ //@deprecated //class JUnit3Suite extends JUnit3ComfortSuite