1 package otherTests.testng; 2 3 import static org.testng.Assert.assertEquals; 4 import static org.testng.Assert.assertNotNull; 5 import static org.testng.Assert.assertNotSame; 6 import static org.testng.Assert.assertNull; 7 import static org.testng.Assert.assertSame; 8 9 import java.util.concurrent.Callable; 10 11 import mockit.Expectations; 12 import mockit.Injectable; 13 import mockit.Tested; 14 import mockit.Verifications; 15 16 import org.testng.annotations.AfterMethod; 17 import org.testng.annotations.BeforeMethod; 18 import org.testng.annotations.Test; 19 20 public final class TestedAndInjectablesTest { 21 static final class UtilityClass { 22 String name; 23 Collaborator collaborator1; 24 Collaborator collaborator2; 25 } 26 27 static class Collaborator { 28 void doSomething() { 29 } 30 } 31 32 static class SUT { 33 final Collaborator collaborator1; 34 Collaborator collaborator2; 35 36 SUT(Collaborator collaborator1) { 37 this.collaborator1 = collaborator1; 38 } 39 40 void useCollaborators() { 41 collaborator1.doSomething(); 42 collaborator2.doSomething(); 43 } 44 } 45 46 @Tested(availableDuringSetup = true) 47 UtilityClass util; 48 UtilityClass previousUtilityClassInstance; 49 @Injectable("util") 50 String utilName; 51 52 @Tested 53 SUT tested1; 54 @Injectable 55 Collaborator collaborator1; 56 57 @Tested 58 SUT tested2; 59 @Tested 60 final SUT tested3 = new SUT(new Collaborator()); 61 @Tested 62 final SUT tested4 = null; 63 64 SUT firstTestedObject; 65 Collaborator firstMockedObject; 66 67 @BeforeMethod 68 public void setUp() { 69 assertUtilObjectIsAvailable(); 70 tested2 = new SUT(new Collaborator()); 71 } 72 73 void assertUtilObjectIsAvailable() { 74 assertNotNull(util); 75 assertEquals(util.name, "util"); 76 assertSame(collaborator1, util.collaborator1); 77 } 78 79 @AfterMethod 80 public void tearDown() { 81 assertUtilObjectIsAvailable(); 82 } 83 84 @Injectable 85 Collaborator collaborator2; 86 87 @Test 88 public void firstTest() { 89 assertSame(collaborator1, util.collaborator1); 90 assertSame(collaborator2, util.collaborator2); 91 92 assertNotNull(tested1); 93 firstTestedObject = tested1; 94 95 assertNotNull(collaborator1); 96 firstMockedObject = collaborator1; 97 98 assertNotNull(collaborator2); 99 100 assertStatesOfTestedObjects(collaborator2); 101 102 new Expectations() { 103 { 104 collaborator1.doSomething(); 105 collaborator2.doSomething(); 106 } 107 }; 108 109 tested1.useCollaborators(); 110 111 previousUtilityClassInstance = util; 112 } 113 114 void assertStatesOfTestedObjects(Collaborator col2) { 115 assertSame(tested1.collaborator1, collaborator1); 116 assertSame(tested1.collaborator2, col2); 117 118 assertNotSame(tested2.collaborator1, collaborator1); 119 assertSame(tested2.collaborator2, col2); 120 121 assertNotSame(tested3.collaborator1, collaborator1); 122 assertNotNull(tested3.collaborator2); 123 124 assertNull(tested4); 125 } 126 127 @Test(dependsOnMethods = "firstTest") 128 public void secondTest() { 129 assertSame(collaborator1, util.collaborator1); 130 131 assertSame(collaborator1, firstMockedObject); 132 assertNotSame(tested1, firstTestedObject); 133 134 assertStatesOfTestedObjects(collaborator2); 135 136 assertNotSame(util, previousUtilityClassInstance); 137 } 138 139 @Injectable 140 Callable<String> mock; 141 142 @Test 143 public void recordAndVerifyExpectationsOnMockedInterface() throws Exception { 144 new Expectations() { 145 { 146 mock.call(); 147 result = "test"; 148 minTimes = 0; 149 } 150 }; 151 152 String value = mock.call(); 153 154 assertEquals(value, "test"); 155 new Verifications() { 156 { 157 mock.call(); 158 times = 1; 159 } 160 }; 161 } 162 }