|
| 1 | +package org.baeldung.mocks.jmockit; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.baeldung.mocks.jmockit.AdvancedCollaborator.InnerAdvancedCollaborator; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | + |
| 13 | +import mockit.Deencapsulation; |
| 14 | +import mockit.Expectations; |
| 15 | +import mockit.Invocation; |
| 16 | +import mockit.Mock; |
| 17 | +import mockit.MockUp; |
| 18 | +import mockit.Mocked; |
| 19 | +import mockit.Tested; |
| 20 | +import mockit.integration.junit4.JMockit; |
| 21 | + |
| 22 | +@RunWith(JMockit.class) |
| 23 | +public class AdvancedCollaboratorTest<MultiMock extends List<String> & Comparable<List<String>>> { |
| 24 | + |
| 25 | + @Tested |
| 26 | + private AdvancedCollaborator mock; |
| 27 | + |
| 28 | + @Mocked |
| 29 | + private MultiMock multiMock; |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testToMockUpPrivateMethod() { |
| 33 | + new MockUp<AdvancedCollaborator>() { |
| 34 | + @Mock |
| 35 | + private String privateMethod() { |
| 36 | + return "mocked: "; |
| 37 | + } |
| 38 | + }; |
| 39 | + String res = mock.methodThatCallsPrivateMethod(1); |
| 40 | + assertEquals("mocked: 1", res); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testToMockUpDifficultConstructor() throws Exception { |
| 45 | + new MockUp<AdvancedCollaborator>() { |
| 46 | + @Mock |
| 47 | + public void $init(Invocation invocation, String string) { |
| 48 | + ((AdvancedCollaborator) invocation.getInvokedInstance()).i = 1; |
| 49 | + } |
| 50 | + }; |
| 51 | + AdvancedCollaborator coll = new AdvancedCollaborator(null); |
| 52 | + assertEquals(1, coll.i); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testToCallPrivateMethodsDirectly() { |
| 57 | + Object value = Deencapsulation.invoke(mock, "privateMethod"); |
| 58 | + assertEquals("default:", value); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testToSetPrivateFieldDirectly() { |
| 63 | + Deencapsulation.setField(mock, "privateField", 10); |
| 64 | + assertEquals(10, mock.methodThatReturnsThePrivateField()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testToGetPrivateFieldDirectly() { |
| 69 | + int value = Deencapsulation.getField(mock, "privateField"); |
| 70 | + assertEquals(5, value); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testToCreateNewInstanceDirectly() { |
| 75 | + AdvancedCollaborator coll = Deencapsulation.newInstance(AdvancedCollaborator.class, "foo"); |
| 76 | + assertEquals(3, coll.i); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testToCreateNewInnerClassInstanceDirectly() { |
| 81 | + InnerAdvancedCollaborator innerCollaborator = Deencapsulation.newInnerInstance(InnerAdvancedCollaborator.class, mock); |
| 82 | + assertNotNull(innerCollaborator); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + public void testMultipleInterfacesWholeTest() { |
| 88 | + new Expectations() { |
| 89 | + { |
| 90 | + multiMock.get(5); result = "foo"; |
| 91 | + multiMock.compareTo((List<String>) any); result = 0; |
| 92 | + } |
| 93 | + }; |
| 94 | + assertEquals("foo", multiMock.get(5)); |
| 95 | + assertEquals(0, multiMock.compareTo(new ArrayList<>())); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + @SuppressWarnings("unchecked") |
| 100 | + public <M extends List<String> & Comparable<List<String>>> void testMultipleInterfacesOneMethod(@Mocked M mock) { |
| 101 | + new Expectations() { |
| 102 | + { |
| 103 | + mock.get(5); result = "foo"; |
| 104 | + mock.compareTo((List<String>) any); |
| 105 | + result = 0; } |
| 106 | + }; |
| 107 | + assertEquals("foo", mock.get(5)); |
| 108 | + assertEquals(0, mock.compareTo(new ArrayList<>())); |
| 109 | + } |
| 110 | +} |
0 commit comments