org.scalatest.junit.JUnit3ComfortSuite.scala Maven / Gradle / Ivy
Show all versions of scalatest_2.8.1 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 3also 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 * namedsetUpandtearDown.JUnit3ComfortSuiteis 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
* *JUnit3ComfortSuites with ScalaTest'sRunner, you must include a JUnit 4 jar file on the class path or runpath. ** One difference between a
* *JUnit3ComfortSuiteand a JUnit 3TestCase* is that tags are supported in aJUnit3ComfortSuite, but JUnit 3 had no such * concept. Tags work in aJUnit3ComfortSuiteexactly like they work inSuite: * you place annotations on the test methods. This allows you, for example, to ignore a test in * aJUnit3ComfortSuitelike 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 * ofJUnit3ComfortSuiteis not namedJUnit3Suiteis because that name might imply * that an instance of this class is a JUnit 3TestCase, just asJUnitSuiteis an actual JUnit 4 test * class andTestNGSuiteis an actual TestNG test class. By contrast, aJUnit3ComfortSuiteis 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. *runTestinvokes 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 * ofrunTestinvokes 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 tosetUpandtearDown. * 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
setUpcompletes abruptly with an exception, this * method will complete abruptly with the same exception. If any call to *super.runTestcompletes abruptly with an exception, this method * will complete abruptly with the same exception, however, before doing so, it will * invoketearDown. IftearDown super.runTest. * Ifsuper.runTestreturns normally, buttearDowncompletes 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 throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param condition the boolean condition to assert * @param message aStringmessage 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 throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param condition the boolean condition to ensure is false * @param message aStringmessage 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 *Stringmessageas 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) /** * ThrowsAssertionFailedErrorto 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 throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aStringmessage 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 twoStrings are equal, with additional information described in aString*message. * If the twoStrings are equal, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aStringmessage to include in a failure report. * @throws AssertionFailedError if the twoStrings are not equal. */ def assertEquals(message: String, expected: String, actual: String) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoStrings are equal. * If the twoStrings are equal, this method returns normally. * Else, it throwsAssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoStrings are not equal. */ def assertEquals(expected: String, actual: String) { Assert.assertEquals(expected, actual) } /** * Assert that twoDoubles are equal within a tolerance specified bydelta, * with additional information described in aStringmessage. * If the twoDoubles are equal within the tolerance, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas 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 aStringmessage to include in a failure report. * @throws AssertionFailedError if the twoDoubles 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 twoDoubles are equal within a tolerance specified bydelta. * If the twoDoubles 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 twoDoubles are not equal within the tolerance specified bydelta. */ def assertEquals(expected: Double, actual: Double, delta: Double) { Assert.assertEquals(expected, actual, delta) } /** * Assert that twoFloats are equal within a tolerance specified bydelta, * with additional information described in aStringmessage. * If the twoFloats are equal within the tolerance, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas 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 aStringmessage to include in a failure report. * @throws AssertionFailedError if the twoFloats 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 twoFloats are equal within a tolerance specified bydelta. * If the twoFloats 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 twoFloats are not equal within the tolerance specified bydelta. */ def assertEquals(expected: Float, actual: Float, delta: Float) { Assert.assertEquals(expected, actual, delta) } /** * Assert that twoLongs are equal within a tolerance specified bydelta, * with additional information described in aStringmessage. * If the twoLongs are equal, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aStringmessage to include in a failure report. * @throws AssertionFailedError if the twoLongs are not equal. */ def assertEquals(message: String, expected: Long, actual: Long) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoLongs are equal. * If the twoLongs are equal, this method returns normally. * Else, it throwsAssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoLongs are equal. */ def assertEquals(expected: Long, actual: Long) { Assert.assertEquals(expected, actual) } /** * Assert that twoBooleans are equal, with additional information described in aString*message. * If the twoBooleans are equal, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aStringmessage to include in a failure report. * @throws AssertionFailedError if the twoBooleans are not equal. */ def assertEquals(message: String, expected: Boolean, actual: Boolean) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoBooleans are equal. * If the twoBooleans are equal, this method returns normally. * Else, it throwsAssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoBooleans are equal. */ def assertEquals(expected: Boolean, actual: Boolean) { Assert.assertEquals(expected, actual) } /** * Assert that twoBytes are equal, with additional information described in aString*message. * If the twoBytes are equal, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aStringmessage to include in a failure report. * @throws AssertionFailedError if the twoBytes are not equal. */ def assertEquals(message: String, expected: Byte, actual: Byte) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoBytes are equal. * If the twoBytes are equal, this method returns normally. * Else, it throwsAssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoBytes are equal. */ def assertEquals(expected: Byte, actual: Byte) { Assert.assertEquals(expected, actual) } /** * Assert that twoChars are equal, with additional information described in aString*message. * If the twoChars are equal, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aStringmessage to include in a failure report. * @throws AssertionFailedError if the twoChars are not equal. */ def assertEquals(message: String, expected: Char, actual: Char) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoChars are equal. * If the twoChars are equal, this method returns normally. * Else, it throwsAssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoChars are equal. */ def assertEquals(expected: Char, actual: Char) { Assert.assertEquals(expected, actual) } /** * Assert that twoShorts are equal, with additional information described in aString*message. * If the twoShorts are equal, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aStringmessage to include in a failure report. * @throws AssertionFailedError if the twoShorts are not equal. */ def assertEquals(message: String, expected: Short, actual: Short) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoShorts are equal. * If the twoShorts are equal, this method returns normally. * Else, it throwsAssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoShorts are equal. */ def assertEquals(expected: Short, actual: Short) { Assert.assertEquals(expected, actual) } /** * Assert that twoInts are equal, with additional information described in aString*message. * If the twoInts are equal, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aStringmessage to include in a failure report. * @throws AssertionFailedError if the twoInts are not equal. */ def assertEquals(message: String, expected: Int, actual: Int) { Assert.assertEquals(message, expected, actual) } /** * Assert that a twoInts are equal. * If the twoInts are equal, this method returns normally. * Else, it throwsAssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the twoInts 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 aStringmessage. * If the object is non-null, this method returns normally. * Else, it throwsAssertionFailedErrorwith the * specifiedmessageas the exception's detail message. * * @param anyRef the object reference to check fornull* @param message aStringmessage 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 aStringmessage. * If the object isnull, this method returns normally. * Else, it throwsAssertionFailedErrorwith the * specifiedmessageas the exception's detail message. * * @param anyRef the object reference to ensure isnull* @param message aStringmessage 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 aStringmessage. * If the two object references refer to the same object, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aStringmessage 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 aStringmessage. * If the two object references refer to different objects, this method returns normally. * Else, it throwsTestFailedExceptionwith the * specifiedmessageas the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message aStringmessage 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:JUnit3Suitehas been deprecated, and will be removed in a future version of ScalaTest. Please * change to usingJUnit3ComforSuiteinstead.JUnit3ComfortSuitedoes not extendjunit.framework.TestCase. * In versions of ScalaTest prior to 0.9.5,JUnit3SuiteextendedTestCaseso that it could be run by a JUnit 3 runner. In * 0.9.6, ScalaTest'sexecutemethods 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 *JUnit3ComfortSuitetrait 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