Orbit
Orbit

Reputation: 2375

How to unit test function that references CookieManager?

The application in question has a logout function that among other things, performs a call to CookieManager.getInstance().removeAllCookies(null):

fun logout(...) {                                                                           
    showLoader(true)                                                          
                                                                                                                                                    
    msgRepo.unregisterSendBirdHandlersAndPushToken(                           
        onSuccess = {                                                         
            clearUserData(...)
            // ...                                       
            onLogout()                                                        
        },                                                                    
        onFailure = {                                                          
            clearUserData(...)
            // ...                                       
            onLogout()                                                        
        }                                                                     
    )                                                                         
}                                                                                                                                                    
                                                                              
/**                                                                           
 * Helper to clear user data before logging user out                          
 */                                                                           
private fun clearUserData(...) {                                                                           
    // ...                                            
    CookieManager.getInstance().removeAllCookies(null)
}                                                                             

Two tests reference the aforementioned logout function. They both are quite similar and look a bit like this:

@Test                                                              
fun `verify logout with success`() {                               
    // .. Other code                                                             
    HardLogoutHelper.logout(...)                                                              
    // ...                            
}                                                                  

The tests fail with the following error message:

'void android.webkit.CookieManager.removeAllCookies(android.webkit.ValueCallback)' java.lang.NoSuchMethodError: 'void android.webkit.CookieManager.removeAllCookies(android.webkit.ValueCallback)' at com.example.helpers.HardLogoutHelper.clearUserData(HardLogoutHelper.kt:86)

I've tried running the test with Roboelectric via @RunWith(RoboelectricTestRunner::class) and I have have tried creating a custom Roboelectric Shadow for the CookieManager class. The issue seems to persist even when using Roboelectric.

Why is the previously mentioned error message being thrown, and how can it be fixed?

Upvotes: 1

Views: 384

Answers (0)

Related Questions