|
| 1 | +package com.github.erikhuizinga.mockk.junit4.example |
| 2 | + |
| 3 | +import com.github.erikhuizinga.mockk.junit4.MockkTest |
| 4 | +import io.mockk.MockKException |
| 5 | +import io.mockk.every |
| 6 | +import io.mockk.mockkObject |
| 7 | +import io.mockk.verify |
| 8 | +import org.junit.Assert.* |
| 9 | +import org.junit.BeforeClass |
| 10 | +import org.junit.FixMethodOrder |
| 11 | +import org.junit.Test |
| 12 | +import org.junit.runner.RunWith |
| 13 | +import org.junit.runners.MethodSorters |
| 14 | +import org.junit.runners.Suite |
| 15 | + |
| 16 | +/** |
| 17 | + * In this test suite the order of suite classes and unit tests is fixed for the example's |
| 18 | + * purposes. |
| 19 | + * |
| 20 | + * Extending [MockkTest] ensures all MockK mock state is cleared between unit tests and all mocks |
| 21 | + * are unmocked between test classes. |
| 22 | + * |
| 23 | + * The first test class [ExampleMockkTest] runs. |
| 24 | + * First we mock [YourObject]. |
| 25 | + * Because mocking may be expensive and/or slow, we only do this once by using |
| 26 | + * [@BeforeClass][BeforeClass] in the `companion object`; |
| 27 | + * in the class body itself it would run for every unit test. |
| 28 | + * |
| 29 | + * Then the unit tests in `ExampleMockkTest` run. |
| 30 | + * Test 1 mocks [YourObject.toString] and then asserts and verifies this. |
| 31 | + * Test 2 is in the same test class, so `YourObject` is still mocked, but any mock state from test 1 |
| 32 | + * should not leak into test 2 (i.e. the mock should be cleared); this is tested in test 2. |
| 33 | + * |
| 34 | + * The next test class [YourObjectIsNoLongerMockedTest] should not be influenced by the first test |
| 35 | + * class and its unit tests. |
| 36 | + * Test 3 asserts and verifies this. |
| 37 | + * However, the verification cannot be done, because `YourObject` is no longer a mock. |
| 38 | + * Instead, MockK throws a [MockKException] and this is asserted. |
| 39 | + */ |
| 40 | +@RunWith(Suite::class) |
| 41 | +@Suite.SuiteClasses(ExampleMockkTest::class, YourObjectIsNoLongerMockedTest::class) |
| 42 | +class ExampleTestSuite |
| 43 | + |
| 44 | +/** |
| 45 | + * This will be mocked in the example. |
| 46 | + */ |
| 47 | +object YourObject |
| 48 | + |
| 49 | +private const val string = "string" |
| 50 | + |
| 51 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 52 | +class ExampleMockkTest : MockkTest() { // Remove `: MockkTest()` to leak mock state! |
| 53 | + companion object { |
| 54 | + @JvmStatic |
| 55 | + @BeforeClass |
| 56 | + fun `before class`() { |
| 57 | + mockkObject(YourObject) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `test 1`() { |
| 63 | + every { YourObject.toString() } returns string // Mock toString |
| 64 | + assertEquals(string, YourObject.toString()) |
| 65 | + verify(exactly = 1) { YourObject.toString() } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun `test 2`() { |
| 70 | + assertNotEquals(string, YourObject.toString()) // toString is no longer mocked! |
| 71 | + verify(exactly = 1) { YourObject.toString() } // YourObject still is mocked! |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +class YourObjectIsNoLongerMockedTest { |
| 76 | + @Test |
| 77 | + fun `test 3`() { |
| 78 | + assertNotEquals(string, YourObject.toString()) // toString is no longer mocked! |
| 79 | + assertThrows(MockKException::class.java) { |
| 80 | + verify(exactly = 1) { YourObject.toString() } // YourObject is no longer mocked! |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments