consciouscode.junit
Class TestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by consciouscode.junit.TestCase
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
SeedlingTestCase

public abstract class TestCase
extends junit.framework.TestCase

Base class with common assertions used in JUnit test cases.

Property Accessors

Several methods access properties of JavaBeans under test. Bean access is implemented via the Apache Commons BeanUtils, and so the syntax of property names can be complex. Properties may be specified in several ways:


Field Summary
static String AUTO_SUITES
          By default, all subclasses will be in the 'common' suite.
 
Constructor Summary
TestCase()
           
TestCase(String name)
           
 
Method Summary
static void assertContained(Object item, Collection<?> collection)
           
static void assertEqualContents(List<?> list, Collection<?> collection)
          Assert that a collection has the same elements as a list, though not in any particular order.
static void assertEqualContents(Object[] expected, Collection<?> collection)
          Asserts that a collection has the same elements as an array, though not in any particular order.
static void assertEqualElements(Object[] array, List<?> list)
           
static void assertEqualElements(Object[] expected, Object[] found)
           
static void assertEqualMembers(Object lhs, Object rhs)
          Asserts that the given objects have equal members.
static void assertEquals(int[] expected, int[] found)
           
static void assertNotContained(Object item, Collection<?> collection)
           
static void assertNotEquals(int left, int right)
          Asserts that two ints are not equal.
static void assertNotEquals(Object left, Object right)
          Asserts that two objects are not equal.
static void assertNotEquals(String message, int left, int right)
          Asserts that two ints are not equal.
static void assertNotEquals(String message, Object left, Object right)
          Asserts that two objects are not equal.
static void assertPropertyEquals(boolean expected, Object bean, String property)
          Asserts that a JavaBean property has an expected boolean value.
static void assertPropertyEquals(int expected, Object bean, String property)
          Asserts that a JavaBean property has an expected int value.
static void assertPropertyEquals(Object expected, Object bean, String property)
          Asserts that a JavaBean property has an expected object value.
static void fail(String message, Throwable cause)
          Fails a test with the given message.
static void fail(Throwable cause)
          Fails a test with no specific message.
static Object getProperty(Object bean, String property)
          Gets the value of a JavaBean property.
protected  Properties loadTestPropertiesFile()
           
protected  File projectFile(String relativePath)
          Retrieves a file relative to the project root directory.
static void setProperty(Object bean, String property, boolean value)
          Sets the value of a boolean JavaBean property.
static void setProperty(Object bean, String property, int value)
          Sets the value of an int JavaBean property.
static void setProperty(Object bean, String property, Object value)
          Sets the value of a JavaBean property.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, setUp, tearDown, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

AUTO_SUITES

public static final String AUTO_SUITES
By default, all subclasses will be in the 'common' suite.

See Also:
AutoSuite, Constant Field Values
Constructor Detail

TestCase

public TestCase()

TestCase

public TestCase(String name)
Method Detail

getProperty

public static Object getProperty(Object bean,
                                 String property)
                          throws junit.framework.AssertionFailedError
Gets the value of a JavaBean property. If any exceptions are encountered (e.g., one thrown by a getter-method), an assertion failure is thrown.

Parameters:
bean - the JavaBean whose property is being inspected.
property - specifies the desired property. See above for syntax and examples.
Returns:
the current value of the property.
Throws:
junit.framework.AssertionFailedError - if any problem occurs while getting the property value.
See Also:
for finer-grained control over exception handling.

setProperty

public static void setProperty(Object bean,
                               String property,
                               Object value)
                        throws junit.framework.AssertionFailedError
Sets the value of a JavaBean property. If any exceptions are encountered (e.g., one thrown by a setter-method), an assertion failure is thrown.

Parameters:
bean - the JavaBean whose property is being changed.
property - specifies the desired property. See above for syntax and examples.
value - the new value for the property.
Throws:
junit.framework.AssertionFailedError - if any problem occurs while getting the property value.
See Also:
for finer-grained control over exception handling.

setProperty

public static void setProperty(Object bean,
                               String property,
                               boolean value)
                        throws junit.framework.AssertionFailedError
Sets the value of a boolean JavaBean property. If any exceptions are encountered (e.g., one thrown by a setter-method), an assertion failure is thrown.

Parameters:
bean - the JavaBean whose property is being changed.
property - specifies the desired property. See above for syntax and examples.
value - the new value for the property.
Throws:
junit.framework.AssertionFailedError - if any problem occurs while getting the property value.
See Also:
for finer-grained control over exception handling.

setProperty

public static void setProperty(Object bean,
                               String property,
                               int value)
                        throws junit.framework.AssertionFailedError
Sets the value of an int JavaBean property. If any exceptions are encountered (e.g., one thrown by a setter-method), an assertion failure is thrown.

Parameters:
bean - the JavaBean whose property is being changed.
property - specifies the desired property. See above for syntax and examples.
value - the new value for the property.
Throws:
junit.framework.AssertionFailedError - if any problem occurs while getting the property value.
See Also:
for finer-grained control over exception handling.

fail

public static void fail(Throwable cause)
                 throws junit.framework.AssertionFailedError
Fails a test with no specific message.

Throws:
junit.framework.AssertionFailedError - in all cases.

fail

public static void fail(String message,
                        Throwable cause)
                 throws junit.framework.AssertionFailedError
Fails a test with the given message.

Throws:
junit.framework.AssertionFailedError - in all cases.

assertNotEquals

public static void assertNotEquals(int left,
                                   int right)
Asserts that two ints are not equal.


assertNotEquals

public static void assertNotEquals(String message,
                                   int left,
                                   int right)
Asserts that two ints are not equal.


assertNotEquals

public static void assertNotEquals(Object left,
                                   Object right)
Asserts that two objects are not equal.


assertNotEquals

public static void assertNotEquals(String message,
                                   Object left,
                                   Object right)
Asserts that two objects are not equal.


assertEqualMembers

public static void assertEqualMembers(Object lhs,
                                      Object rhs)
Asserts that the given objects have equal members. This is implemented via EqualsBuilder.reflectionEquals(Object, Object), which uses reflection to access the members of each object. Transient members are not tested.


assertPropertyEquals

public static void assertPropertyEquals(boolean expected,
                                        Object bean,
                                        String property)
Asserts that a JavaBean property has an expected boolean value.

Throws:
junit.framework.AssertionFailedError - if the property doesn't have the expected value.

assertPropertyEquals

public static void assertPropertyEquals(int expected,
                                        Object bean,
                                        String property)
Asserts that a JavaBean property has an expected int value.

Throws:
junit.framework.AssertionFailedError - if the property doesn't have the expected value.

assertPropertyEquals

public static void assertPropertyEquals(Object expected,
                                        Object bean,
                                        String property)
Asserts that a JavaBean property has an expected object value.

Throws:
junit.framework.AssertionFailedError - if the property doesn't have the expected value.

assertEquals

public static void assertEquals(int[] expected,
                                int[] found)

assertEqualElements

public static void assertEqualElements(Object[] expected,
                                       Object[] found)

assertEqualContents

public static void assertEqualContents(Object[] expected,
                                       Collection<?> collection)
Asserts that a collection has the same elements as an array, though not in any particular order.

NOTE that this assumes that there are no two equals elements in the array.


assertEqualContents

public static void assertEqualContents(List<?> list,
                                       Collection<?> collection)
Assert that a collection has the same elements as a list, though not in any particular order.

NOTE that this assumes that there are no two equals elements in the list.


assertEqualElements

public static void assertEqualElements(Object[] array,
                                       List<?> list)

assertContained

public static void assertContained(Object item,
                                   Collection<?> collection)

assertNotContained

public static void assertNotContained(Object item,
                                      Collection<?> collection)

loadTestPropertiesFile

protected Properties loadTestPropertiesFile()
                                     throws IOException
Throws:
IOException

projectFile

protected File projectFile(String relativePath)
Retrieves a file relative to the project root directory.

Returns:
a new File instance; not null.
Throws:
IllegalArgumentException - if the file doesn't exist.


Copyright © 2001–2012 Todd V. Jonker. All Rights Reserved.