Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package junit.framework;

A set of assert methods. Messages are only displayed when an assert fails.
  
  
  public class Assert {
Protect constructor since it is a static only class
 
 	protected Assert() {
 	}

Asserts that a condition is true. If it isn't it throws an AssertionFailedError with the given message.
 
 	static public void assertTrue(String messageboolean condition) {
 		if (!condition)
 			fail(message);
 	}
Asserts that a condition is true. If it isn't it throws an AssertionFailedError.
 
 	static public void assertTrue(boolean condition) {
 		assertTrue(nullcondition);
 	}
Asserts that a condition is false. If it isn't it throws an AssertionFailedError with the given message.
 
 	static public void assertFalse(String messageboolean condition) {
 		assertTrue(message, !condition);
 	}
Asserts that a condition is false. If it isn't it throws an AssertionFailedError.
 
 	static public void assertFalse(boolean condition) {
 		assertFalse(nullcondition);
 	}
Fails a test with the given message.
 
 	static public void fail(String message) {
 		throw new AssertionFailedError(message);
 	}
Fails a test with no message.
 
 	static public void fail() {
 		fail(null);
 	}
Asserts that two objects are equal. If they are not an AssertionFailedError is thrown with the given message.
 
 	static public void assertEquals(String messageObject expectedObject actual) {
 		if (expected == null && actual == null)
 			return;
 		if (expected != null && expected.equals(actual))
 			return;
 		failNotEquals(messageexpectedactual);
 	}
Asserts that two objects are equal. If they are not an AssertionFailedError is thrown.
 
 	static public void assertEquals(Object expectedObject actual) {
 	    assertEquals(nullexpectedactual);
 	}
Asserts that two Strings are equal.
 
 	static public void assertEquals(String messageString expectedString actual) {
 		if (expected == null && actual == null)
 			return;
 		if (expected != null && expected.equals(actual))
 			return;
 		throw new ComparisonFailure(messageexpectedactual);
 	}
Asserts that two Strings are equal.
 
 	static public void assertEquals(String expectedString actual) {
 	    assertEquals(nullexpectedactual);
 	}
Asserts that two doubles are equal concerning a delta. If they are not an AssertionFailedError is thrown with the given message. If the expected value is infinity then the delta value is ignored.
 
 	static public void assertEquals(String messagedouble expecteddouble actualdouble delta) {
 		if (Double.compare(expectedactual) == 0)
 			return;
 		if (!(Math.abs(expected-actual) <= delta))
 			failNotEquals(messagenew Double(expected), new Double(actual));
 	}
Asserts that two doubles are equal concerning a delta. If the expected value is infinity then the delta value is ignored.
	static public void assertEquals(double expecteddouble actualdouble delta) {
	    assertEquals(nullexpectedactualdelta);
	}
Asserts that two floats are equal concerning a positive delta. If they are not an AssertionFailedError is thrown with the given message. If the expected value is infinity then the delta value is ignored.
	static public void assertEquals(String messagefloat expectedfloat actualfloat delta) {
		if (Float.compare(expectedactual) == 0)
			return;
		if (!(Math.abs(expected - actual) <= delta))
				failNotEquals(messagenew Float(expected), new Float(actual));
	}
Asserts that two floats are equal concerning a delta. If the expected value is infinity then the delta value is ignored.
	static public void assertEquals(float expectedfloat actualfloat delta) {
		assertEquals(nullexpectedactualdelta);
	}
Asserts that two longs are equal. If they are not an AssertionFailedError is thrown with the given message.
	static public void assertEquals(String messagelong expectedlong actual) {
	    assertEquals(messagenew Long(expected), new Long(actual));
	}
Asserts that two longs are equal.
	static public void assertEquals(long expectedlong actual) {
	    assertEquals(nullexpectedactual);
	}
Asserts that two booleans are equal. If they are not an AssertionFailedError is thrown with the given message.
	static public void assertEquals(String messageboolean expectedboolean actual) {
    		assertEquals(message, Boolean.valueOf(expected), Boolean.valueOf(actual));
  	}
Asserts that two booleans are equal.
	static public void assertEquals(boolean expectedboolean actual) {
		assertEquals(nullexpectedactual);
	}
Asserts that two bytes are equal. If they are not an AssertionFailedError is thrown with the given message.
  	static public void assertEquals(String messagebyte expectedbyte actual) {
		assertEquals(messagenew Byte(expected), new Byte(actual));
	}
Asserts that two bytes are equal.
	static public void assertEquals(byte expectedbyte actual) {
		assertEquals(nullexpectedactual);
	}
Asserts that two chars are equal. If they are not an AssertionFailedError is thrown with the given message.
  	static public void assertEquals(String messagechar expectedchar actual) {
    		assertEquals(messagenew Character(expected), new Character(actual));
  	}
Asserts that two chars are equal.
  	static public void assertEquals(char expectedchar actual) {
		assertEquals(nullexpectedactual);
	}
Asserts that two shorts are equal. If they are not an AssertionFailedError is thrown with the given message.
	static public void assertEquals(String messageshort expectedshort actual) {
    		assertEquals(messagenew Short(expected), new Short(actual));
	}
  
Asserts that two shorts are equal.
	static public void assertEquals(short expectedshort actual) {
		assertEquals(nullexpectedactual);
	}
Asserts that two ints are equal. If they are not an AssertionFailedError is thrown with the given message.
  	static public void assertEquals(String messageint expectedint actual) {
		assertEquals(messagenew Integer(expected), new Integer(actual));
  	}
  
Asserts that two ints are equal.
  	static public void assertEquals(int expectedint actual) {
  		assertEquals(nullexpectedactual);
	}
Asserts that an object isn't null.
	static public void assertNotNull(Object object) {
		assertNotNull(nullobject);
	}
Asserts that an object isn't null. If it is an AssertionFailedError is thrown with the given message.
	static public void assertNotNull(String messageObject object) {
		assertTrue(messageobject != null);
	}
Asserts that an object is null. If it isn't an java.lang.AssertionError is thrown. Message contains: Expected: <null> but was: object

Parameters:
object Object to check or null
	static public void assertNull(Object object) {
		String message = "Expected: <null> but was: " + String.valueOf(object);
		assertNull(messageobject);
	}
Asserts that an object is null. If it is not an AssertionFailedError is thrown with the given message.
	static public void assertNull(String messageObject object) {
		assertTrue(messageobject == null);
	}
Asserts that two objects refer to the same object. If they are not an AssertionFailedError is thrown with the given message.
	static public void assertSame(String messageObject expectedObject actual) {
		if (expected == actual)
			return;
		failNotSame(messageexpectedactual);
	}
Asserts that two objects refer to the same object. If they are not the same an AssertionFailedError is thrown.
	static public void assertSame(Object expectedObject actual) {
	    assertSame(nullexpectedactual);
	}
Asserts that two objects do not refer to the same object. If they do refer to the same object an AssertionFailedError is thrown with the given message.
	static public void assertNotSame(String messageObject expectedObject actual) {
		if (expected == actual)
			failSame(message);
	}
Asserts that two objects do not refer to the same object. If they do refer to the same object an AssertionFailedError is thrown.
	static public void assertNotSame(Object expectedObject actual) {
		assertNotSame(nullexpectedactual);
	}
	static public void failSame(String message) {
		String formatted"";
 		if (message != null)
 			formattedmessage+" ";
 		fail(formatted+"expected not same");
	}
	static public void failNotSame(String messageObject expectedObject actual) {
		String formatted"";
		if (message != null)
			formattedmessage+" ";
		fail(formatted+"expected same:<"+expected+"> was not:<"+actual+">");
	}
	static public void failNotEquals(String messageObject expectedObject actual) {
		fail(format(messageexpectedactual));
	}
	public static String format(String messageObject expectedObject actual) {
		String formatted"";
		if (message != null)
			formattedmessage+" ";
		return formatted+"expected:<"+expected+"> but was:<"+actual+">";
	}
New to GrepCode? Check out our FAQ X