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

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

There is a newer version: 2.0.M6-SNAP27
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.{TestCase, Assert, AssertionFailedError}
import org.junit.runner.RunWith

/**
 * A suite of tests that mimics the syntax of a JUnit 3 TestCase, 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 with test, and can create fixtures with methods
 * named setUp and tearDown. 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 JUnit3ComfortSuites with ScalaTest's Runner, you must include a JUnit 4 jar file on the class path or runpath. *

* *

* One difference between a JUnit3ComfortSuite and a JUnit 3 TestCase * is that tags are supported in a JUnit3ComfortSuite, but JUnit 3 had no such * concept. Tags work in a JUnit3ComfortSuite exactly like they work in Suite: * you place annotations on the test methods. This allows you, for example, to ignore a test in * a JUnit3ComfortSuite like this: *

* *
 * @Ignore
 * def testSubtraction() {
 *   val diff = 4 - 1
 *   assert(diff === 3)
 *   assert(diff - 2 === 1)
 * }
 * 
* *

* Note: the reason JUnit3ComfortSuite is not named JUnit3Suite is because that name might imply * that an instance of this class is a JUnit 3 TestCase, just as JUnitSuite is an actual JUnit 4 test * class and TestNGSuite is an actual TestNG test class. By contrast, a JUnit3ComfortSuite is not actually * a JUnit 3 TestCase, 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. *

* * @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 * of 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 * of runTest 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 to setUp and tearDown. * This trait's implementation of this method ("this method") invokes setUp * before running each test and tearDown * after running each test. It runs each test by invoking super.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 * invoke tearDown. If tearDown also completes abruptly with an exception, this * method will nevertheless complete abruptly with the exception previously thrown by super.runTest. * If super.runTest returns normally, but tearDown 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 in String * message, is true. * If the condition is true, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param condition the boolean condition to assert * @param message a String message to include in a failure report. * @throws AssertionFailedError if the condition is false. */ def assertTrue(message: String, condition: Boolean) { Assert.assertTrue(message, condition) } /** * Assert that a boolean condition is true. * If the condition is true, this method returns normally. * Else, it throws AssertionFailedError. * * @param condition the boolean condition to assert * @throws AssertionFailedError if the condition is false. */ def assertTrue(condition: Boolean) { Assert.assertTrue(condition) } /** * Assert that a boolean condition, described in String * message, is false. * If the condition is false, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param condition the boolean condition to ensure is false * @param message a String message to include in a failure report. * @throws AssertionFailedError if the condition is true. */ def assertFalse(message: String, condition: Boolean) { Assert.assertFalse(message, condition) } /** * Assert that a boolean condition is false. * If the condition is true, this method returns normally. * Else, it throws AssertionFailedError. * * @param condition the boolean condition to assert * @throws AssertionFailedError if the condition is false. */ def assertFalse(condition: Boolean) { Assert.assertFalse(condition) } /** * Throws AssertionFailedError, 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) /** * Throws AssertionFailedError to indicate a test failed. */ override def fail(): Nothing = throw new AssertionFailedError /** * Assert that two objects are equal, with additional information described in a String * message. * If the two objects are equal, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message a String 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 throws AssertionFailedError. * * @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 two Strings are equal, with additional information described in a String * message. * If the two Strings are equal, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message a String message to include in a failure report. * @throws AssertionFailedError if the two Strings are not equal. */ def assertEquals(message: String, expected: String, actual: String) { Assert.assertEquals(message, expected, actual) } /** * Assert that a two Strings are equal. * If the two Strings are equal, this method returns normally. * Else, it throws AssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the two Strings are not equal. */ def assertEquals(expected: String, actual: String) { Assert.assertEquals(expected, actual) } /** * Assert that two Doubles are equal within a tolerance specified by delta, * with additional information described in a String message. * If the two Doubles are equal within the tolerance, this method returns normally. * Else, it throws TestFailedException with the * specified message 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 a String message to include in a failure report. * @throws AssertionFailedError if the two Doubles are not equal within the tolerance specified by delta. */ def assertEquals(message: String, expected: Double, actual: Double, delta: Double) { Assert.assertEquals(message, expected, actual, delta) } /** * Assert that a two Doubles are equal within a tolerance specified by delta. * If the two Doubles are equal within the tolerance, this method returns normally. * Else, it throws AssertionFailedError. * 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 two Doubles are not equal within the tolerance specified by delta. */ def assertEquals(expected: Double, actual: Double, delta: Double) { Assert.assertEquals(expected, actual, delta) } /** * Assert that two Floats are equal within a tolerance specified by delta, * with additional information described in a String message. * If the two Floats are equal within the tolerance, this method returns normally. * Else, it throws TestFailedException with the * specified message 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 a String message to include in a failure report. * @throws AssertionFailedError if the two Floats are not equal within the tolerance specified by delta. */ def assertEquals(message: String, expected: Float, actual: Float, delta: Float) { Assert.assertEquals(message, expected, actual, delta) } /** * Assert that a two Floats are equal within a tolerance specified by delta. * If the two Floats are equal within the tolerance, this method returns normally. * Else, it throws AssertionFailedError. * 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 two Floats are not equal within the tolerance specified by delta. */ def assertEquals(expected: Float, actual: Float, delta: Float) { Assert.assertEquals(expected, actual, delta) } /** * Assert that two Longs are equal within a tolerance specified by delta, * with additional information described in a String message. * If the two Longs are equal, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message a String message to include in a failure report. * @throws AssertionFailedError if the two Longs are not equal. */ def assertEquals(message: String, expected: Long, actual: Long) { Assert.assertEquals(message, expected, actual) } /** * Assert that a two Longs are equal. * If the two Longs are equal, this method returns normally. * Else, it throws AssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the two Longs are equal. */ def assertEquals(expected: Long, actual: Long) { Assert.assertEquals(expected, actual) } /** * Assert that two Booleans are equal, with additional information described in a String * message. * If the two Booleans are equal, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message a String message to include in a failure report. * @throws AssertionFailedError if the two Booleans are not equal. */ def assertEquals(message: String, expected: Boolean, actual: Boolean) { Assert.assertEquals(message, expected, actual) } /** * Assert that a two Booleans are equal. * If the two Booleans are equal, this method returns normally. * Else, it throws AssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the two Booleans are equal. */ def assertEquals(expected: Boolean, actual: Boolean) { Assert.assertEquals(expected, actual) } /** * Assert that two Bytes are equal, with additional information described in a String * message. * If the two Bytes are equal, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message a String message to include in a failure report. * @throws AssertionFailedError if the two Bytes are not equal. */ def assertEquals(message: String, expected: Byte, actual: Byte) { Assert.assertEquals(message, expected, actual) } /** * Assert that a two Bytes are equal. * If the two Bytes are equal, this method returns normally. * Else, it throws AssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the two Bytes are equal. */ def assertEquals(expected: Byte, actual: Byte) { Assert.assertEquals(expected, actual) } /** * Assert that two Chars are equal, with additional information described in a String * message. * If the two Chars are equal, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message a String message to include in a failure report. * @throws AssertionFailedError if the two Chars are not equal. */ def assertEquals(message: String, expected: Char, actual: Char) { Assert.assertEquals(message, expected, actual) } /** * Assert that a two Chars are equal. * If the two Chars are equal, this method returns normally. * Else, it throws AssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the two Chars are equal. */ def assertEquals(expected: Char, actual: Char) { Assert.assertEquals(expected, actual) } /** * Assert that two Shorts are equal, with additional information described in a String * message. * If the two Shorts are equal, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message a String message to include in a failure report. * @throws AssertionFailedError if the two Shorts are not equal. */ def assertEquals(message: String, expected: Short, actual: Short) { Assert.assertEquals(message, expected, actual) } /** * Assert that a two Shorts are equal. * If the two Shorts are equal, this method returns normally. * Else, it throws AssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the two Shorts are equal. */ def assertEquals(expected: Short, actual: Short) { Assert.assertEquals(expected, actual) } /** * Assert that two Ints are equal, with additional information described in a String * message. * If the two Ints are equal, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message a String message to include in a failure report. * @throws AssertionFailedError if the two Ints are not equal. */ def assertEquals(message: String, expected: Int, actual: Int) { Assert.assertEquals(message, expected, actual) } /** * Assert that a two Ints are equal. * If the two Ints are equal, this method returns normally. * Else, it throws AssertionFailedError. * * @param expected the expected value * @param actual the actual value * @throws AssertionFailedError if the two Ints are equal. */ def assertEquals(expected: Int, actual: Int) { Assert.assertEquals(expected, actual) } /** * Assert that an object reference is not null. * If the object is non-null, this method returns normally. * Else, it throws AssertionFailedError. * * @param anyRef the object reference to check for null * @throws AssertionFailedError if the object reference is null */ def assertNotNull(anyRef: AnyRef) { Assert.assertNotNull(anyRef) } /** * Assert that an object reference is not null, with * additional information described in a String message. * If the object is non-null, this method returns normally. * Else, it throws AssertionFailedError with the * specified message as the exception's detail message. * * @param anyRef the object reference to check for null * @param message a String message to include in a failure report. * @throws AssertionFailedError if the object reference is null */ def assertNotNull(message: String, anyRef: AnyRef) { Assert.assertNotNull(message, anyRef) } /** * Assert that an object reference is null. * If the object is null, this method returns normally. * Else, it throws AssertionFailedError. * * @param anyRef the object reference to ensure is null * @throws AssertionFailedError if the object reference is non-null */ def assertNull(anyRef: AnyRef) { Assert.assertNull(anyRef) } /** * Assert that an object reference is null, with * additional information described in a String message. * If the object is null, this method returns normally. * Else, it throws AssertionFailedError with the * specified message as the exception's detail message. * * @param anyRef the object reference to ensure is null * @param message a String 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 a String message. * If the two object references refer to the same object, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message a String 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 throws TestFailedException. * * @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 a String message. * If the two object references refer to different objects, this method returns normally. * Else, it throws TestFailedException with the * specified message as the exception's detail message. * * @param expected the expected value * @param actual the actual value * @param message a String 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 throws TestFailedException. * * @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 using JUnit3ComforSuite instead. JUnit3ComfortSuite does not extend junit.framework.TestCase. * In versions of ScalaTest prior to 0.9.5, JUnit3Suite extended TestCase so that it could be run by a JUnit 3 runner. In * 0.9.6, ScalaTest's execute methods were renamed to run, which is not compatible with TestCase. * 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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy