001package beginner; 002 003import com.sandwich.koan.Koan; 004 005import static com.sandwich.koan.constant.KoanConstants.__; 006import static com.sandwich.util.Assert.*; 007 008public class AboutAssertions { 009 010 @Koan 011 public void assertBooleanTrue() { 012 // there are two possibilities, true or false, what would it be here? 013 assertTrue(true); 014 } 015 016 @Koan 017 public void assertBooleanFalse() { 018 assertFalse(false); 019 } 020 021 @Koan 022 public void assertNullObject() { 023 // reference to the object can be null, a magic keyword, null, which means 024 // that there is nothing there 025 assertNull(__); 026 } 027 028 @Koan 029 public void assertNullObjectReference() { 030 Object someObject = __; 031 assertNull(someObject); 032 } 033 034 @Koan 035 public void assertNotNullObject() { 036 // but what when there should not be a null value? 037 assertNotNull(null); 038 } 039 040 @Koan 041 public void assertEqualsUsingExpression() { 042 assertTrue("Hello World!".equals(__)); 043 } 044 045 @Koan 046 public void assertEqualsWithAFewExpressions() { 047 assertEquals("Hello World!", __); 048 assertEquals(1, __); 049 assertEquals(2 + 2, __); 050 assertEquals(2 * 3, __); 051 assertEquals(3 - 8, __); 052 assertEquals(10 / 2, __); 053 } 054 055 @Koan 056 public void assertEqualsWithDescriptiveMessage() { 057 // Generally, when using an assertXXX methods, expectation is on the 058 // left and it is best practice to use a String for the first arg 059 // indication what has failed 060 assertEquals("The answer to 'life the universe and everything' should be 42", 42, __); 061 } 062 063 @Koan 064 public void assertSameInstance() { 065 // Just because something is equal doesn't mean that it is the same. 066 // It's only the same if the reference is the same. 067 Object same = new Integer(1); 068 Object sameReference = __; 069 assertSame(same, sameReference); 070 } 071 072 @Koan 073 public void assertNotSameInstance() { 074 Integer same = new Integer(1); 075 Integer sameReference = same; 076 assertNotSame(same, sameReference); 077 } 078}