Issue
I'm starting to use Kotlin on a little demo android app. I've created a sharedpreferences
helper class which i'm trying to test with Junit and Mockito. Below is my sharedprefshelper:
public class SharedPrefsHelperImp( cont : Context) : SharedPrefsHelper {
val prefsname: String = "prefs"
var prefs: SharedPreferences? = null
var edit: SharedPreferences.Editor? = null
init {
prefs = cont.getSharedPreferences(prefsname, Context.MODE_PRIVATE)
edit = prefs!!.edit()
}
override fun getPrefsStringValue(key: String) : String {
return prefs!!.getString(key, "")
}
override fun addPrefsStringVal( key : String, value: String) {
edit!!.putString(key, value).commit()
}
override fun getSharedPrefsBool(key : String): Boolean {
return prefs!!.getBoolean(key, false)
}
override fun addSharedPrefsBool(key : String, value : Boolean) {
edit!!.putBoolean(key, value).commit()
}
}
here is my test class:
class SharedPrefsHelperImpTest {
@Mock var cont : Context? = null
@Mock var mockprefs : SharedPreferences? = null
@Mock var mockprefsedit : SharedPreferences.Editor? = null
var prefshelper : SharedPrefsHelper? = null
@Before
fun setUp() {
//MockitoAnnotations.initMocks(this)
cont = Mockito.mock(Context::class.java)
mockprefs = Mockito.mock(SharedPreferences::class.java)
mockprefsedit = Mockito.mock(SharedPreferences.Editor::class.java)
`when`(cont!!.getSharedPreferences(anyString(), anyInt())).thenReturn(mockprefs!!)
`when`(mockprefs!!.edit()).thenReturn(mockprefsedit!!)
prefshelper = SharedPrefsHelperImp(cont!!)
}
@Test
fun testNotNull(){
Assert.assertNotNull(cont)
Assert.assertNotNull(mockprefs)
Assert.assertNotNull(mockprefsedit)
}
@Test
fun testItemAdded()
{
prefshelper!!.addPrefsStringVal("thing", "thing")
verify(mockprefsedit)!!.putString(anyString(), anyString())
}
@Test
fun testGetString()
{
prefshelper!!.getPrefsStringValue("key")
verify(mockprefs)!!.getString("key", "")
}
}
Issue is when I call addPrefsValueString()
in the helper. the line
edit!!.putString(key, value).commit()
throws a null pointer exception? not sure why? I've setup the mock sharedprefs
and sharedpreferences.Edit
in the test class method annotated with @Before
(shown below)
@Before
fun setUp() {
//MockitoAnnotations.initMocks(this)
cont = Mockito.mock(Context::class.java)
mockprefs = Mockito.mock(SharedPreferences::class.java)
mockprefsedit = Mockito.mock(SharedPreferences.Editor::class.java)
`when`(cont!!.getSharedPreferences(anyString(), anyInt())).thenReturn(mockprefs!!)
`when`(mockprefs!!.edit()).thenReturn(mockprefsedit!!)
prefshelper = SharedPrefsHelperImp(cont!!)
}
i'm sure my code is less than optimal.
EDIT:
Here's my fix for the testItemAdded()
method. need to return the mock preferences editor on the first call.
@Test
fun testItemAdded()
{
`when`(mockprefsedit?.putString(anyString(), anyString())).thenReturn(mockprefsedit)
`when`(mockprefsedit?.commit()).thenReturn(true)
prefshelper!!.addPrefsStringVal("thing", "thing")
verify(mockprefsedit)!!.putString(anyString(), anyString())
verify(mockprefsedit)!!.commit()
}
Solution
You should set expectations for the call below, on your mock object (mockprefsedit
). As well for the object returned, on which commit
is invoked.
edit!!.putString(key, value)
thanks Sriram
Answered By - Sri
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.