Skip to content

Commit

Permalink
Fixed issue of verify causing a call on the actual class of the mock …
Browse files Browse the repository at this point in the history
…due to methods not being open.
  • Loading branch information
Michael Richardson committed Jan 7, 2017
1 parent 2b4d0f9 commit 46e18b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ open class ThirdPartyAnalyticsWrapper @Inject constructor() {
inject()
}

fun inject() {
open fun inject() {
MyApplication.myComponent.inject(this)
}

open fun getMyHello() : String {
return hello
}

fun trackLog(message: String) {
open fun trackLog(message: String) {
thirdPartyLogger.logStuff(message)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import junit.framework.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import org.mockito.Mockito.*
import org.robolectric.RobolectricGradleTestRunner
import org.robolectric.annotation.Config
import javax.inject.Inject
Expand All @@ -27,7 +26,6 @@ class MyNonLifecycleClassTest {
thirdPartyAnalyticsWrapper.inject()
}

// This test passes
@Test
fun testAnalyticsWrappersTheSame() {
assertEquals(thirdPartyAnalyticsWrapper, myNonLifecycleClass.thirdPartyAnalyticsWrapper)
Expand All @@ -37,11 +35,15 @@ class MyNonLifecycleClassTest {
assertEquals("test", myNonLifecycleClass.thirdPartyAnalyticsWrapper.getMyHello())
}

// This test fails
@Test
fun testMakeError() {
myNonLifecycleClass.makeError()
verify(thirdPartyAnalyticsWrapper).trackLog("test")
verify(thirdPartyAnalyticsWrapper).trackLog("error")
}

@Test
fun testVerifyDoesNotCallItself() {
verify(thirdPartyAnalyticsWrapper, never()).trackLog("test2")
}

}

0 comments on commit 46e18b6

Please sign in to comment.