Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: AssertException for SharedPreferences when running couple test cases together #313

Open
3 tasks done
kdani41 opened this issue Jun 4, 2019 · 4 comments
Open
3 tasks done

Comments

@kdani41
Copy link

kdani41 commented Jun 4, 2019

  • I am running the latest version [1.9.3]
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

Behaviour should be same regardless if they are run together or solo.

Current Behavior

Each individual test case passes when running it solo but when running it together with the entire class it throws AssertException for all the tests except the first one.

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Run test for the entire class PreferenceManagerTest
  2. 1st test case will pass but 2nd verify will fail.

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version: 1.9.3
  • OS: MacOS
  • Kotlin version: 1.3.20
  • JDK version: 1.8
  • JUnit version: 4.12
  • Type of test: unit test

Failure Logs

Please include any relevant log snippets or files here.

Stack trace

// -----------------------[ YOUR STACK STARTS HERE ] -----------------------
java.lang.AssertionError: Verification failed: call 1 of 2: SharedPreferences(#7).edit()) was not called

	at io.mockk.impl.recording.states.VerifyingState.failIfNotPassed(VerifyingState.kt:66)
	at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:42)
	at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
	at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
	at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
	at io.mockk.MockKDsl.internalVerify(API.kt:118)
	at io.mockk.MockKKt.verify(MockK.kt:139)
	at io.mockk.MockKKt.verify$default(MockK.kt:136)
	at xx.xx.xx.xx.PreferenceManagerTest.test for saving first key(PreferenceManagerTest.kt:32)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

java.lang.AssertionError: Verification failed: call 1 of 2: SharedPreferences(#7).edit()) was not called.

Calls to same mock:
1) SharedPreferences(#7).toString()
2) SharedPreferences(#7).toString()

Stack traces:
1)                                 io.mockk.impl.InternalPlatform.captureStackTrace      (InternalPlatform.kt:121)             
                                     io.mockk.impl.stub.MockKStub.handleInvocation       (MockKStub.kt:247)                    
   io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation             (JvmMockFactoryHelper.kt:25)          
                            io.mockk.proxy.jvm.advice.Interceptor.call                   (Interceptor.kt:20)                   
                             io.mockk.proxy.jvm.advice.BaseAdvice.handle                 (BaseAdvice.kt:42)                    
           io.mockk.proxy.jvm.advice.jvm.JvmMockKProxyInterceptor.intercept              (JvmMockKProxyInterceptor.java:31)    
                      android.content.SharedPreferences$Subclass0.toString               (-:-1)                                
               xx.xx.xx.xx.PreferenceManagerTest.setUp                  (PreferenceManagerTest.kt:21)         
                             sun.reflect.NativeMethodAccessorImpl.invoke0                (NativeMethodAccessorImpl.java:-2)N   
                             sun.reflect.NativeMethodAccessorImpl.invoke                 (NativeMethodAccessorImpl.java:62)    
                         sun.reflect.DelegatingMethodAccessorImpl.invoke                 (DelegatingMethodAccessorImpl.java:43)
                                         java.lang.reflect.Method.invoke                 (Method.java:498)                     
                        org.junit.runners.model.FrameworkMethod$1.runReflectiveCall      (FrameworkMethod.java:50)             
              org.junit.internal.runners.model.ReflectiveCallable.run                    (ReflectiveCallable.java:12)          
                          org.junit.runners.model.FrameworkMethod.invokeExplosively      (FrameworkMethod.java:47)             
                 org.junit.internal.runners.statements.RunBefores.evaluate               (RunBefores.java:24)                  
                  org.junit.internal.runners.statements.RunAfters.evaluate               (RunAfters.java:27)                   
                                   org.junit.runners.ParentRunner.runLeaf                (ParentRunner.java:325)               
                         org.junit.runners.BlockJUnit4ClassRunner.runChild               (BlockJUnit4ClassRunner.java:78)      
                         org.junit.runners.BlockJUnit4ClassRunner.runChild               (BlockJUnit4ClassRunner.java:57)      
                                 org.junit.runners.ParentRunner$3.run                    (ParentRunner.java:290)               
                                 org.junit.runners.ParentRunner$1.schedule               (ParentRunner.java:71)                
                                   org.junit.runners.ParentRunner.runChildren            (ParentRunner.java:288)               
                                   org.junit.runners.ParentRunner.access$000             (ParentRunner.java:58)                
                                 org.junit.runners.ParentRunner$2.evaluate               (ParentRunner.java:268)               
                                   org.junit.runners.ParentRunner.run                    (ParentRunner.java:363)               
                                       org.junit.runner.JUnitCore.run                    (JUnitCore.java:137)                  
                         com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs    (JUnit4IdeaTestRunner.java:68)        
          com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs    (IdeaTestRunner.java:47)              
                     com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart (JUnitStarter.java:242)               
                     com.intellij.rt.execution.junit.JUnitStarter.main                   (JUnitStarter.java:70)                

2)                                 io.mockk.impl.InternalPlatform.captureStackTrace         (InternalPlatform.kt:121)             
                                     io.mockk.impl.stub.MockKStub.handleInvocation          (MockKStub.kt:247)                    
   io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation                (JvmMockFactoryHelper.kt:25)          
                            io.mockk.proxy.jvm.advice.Interceptor.call                      (Interceptor.kt:20)                   
                             io.mockk.proxy.jvm.advice.BaseAdvice.handle                    (BaseAdvice.kt:42)                    
           io.mockk.proxy.jvm.advice.jvm.JvmMockKProxyInterceptor.intercept                 (JvmMockKProxyInterceptor.java:31)    
                      android.content.SharedPreferences$Subclass0.toString                  (-:-1)                                
               xx.xx.xx.xx.PreferenceManagerTest.test for saving first key (PreferenceManagerTest.kt:35)         
                             sun.reflect.NativeMethodAccessorImpl.invoke0                   (NativeMethodAccessorImpl.java:-2)N   
                             sun.reflect.NativeMethodAccessorImpl.invoke                    (NativeMethodAccessorImpl.java:62)    
                         sun.reflect.DelegatingMethodAccessorImpl.invoke                    (DelegatingMethodAccessorImpl.java:43)
                                         java.lang.reflect.Method.invoke                    (Method.java:498)                     
                        org.junit.runners.model.FrameworkMethod$1.runReflectiveCall         (FrameworkMethod.java:50)             
              org.junit.internal.runners.model.ReflectiveCallable.run                       (ReflectiveCallable.java:12)          
                          org.junit.runners.model.FrameworkMethod.invokeExplosively         (FrameworkMethod.java:47)             
               org.junit.internal.runners.statements.InvokeMethod.evaluate                  (InvokeMethod.java:17)                
                 org.junit.internal.runners.statements.RunBefores.evaluate                  (RunBefores.java:26)                  
                  org.junit.internal.runners.statements.RunAfters.evaluate                  (RunAfters.java:27)                   
                                   org.junit.runners.ParentRunner.runLeaf                   (ParentRunner.java:325)               
                         org.junit.runners.BlockJUnit4ClassRunner.runChild                  (BlockJUnit4ClassRunner.java:78)      
                         org.junit.runners.BlockJUnit4ClassRunner.runChild                  (BlockJUnit4ClassRunner.java:57)      
                                 org.junit.runners.ParentRunner$3.run                       (ParentRunner.java:290)               
                                 org.junit.runners.ParentRunner$1.schedule                  (ParentRunner.java:71)                
                                   org.junit.runners.ParentRunner.runChildren               (ParentRunner.java:288)               
                                   org.junit.runners.ParentRunner.access$000                (ParentRunner.java:58)                
                                 org.junit.runners.ParentRunner$2.evaluate                  (ParentRunner.java:268)               
                                   org.junit.runners.ParentRunner.run                       (ParentRunner.java:363)               
                                       org.junit.runner.JUnitCore.run                       (JUnitCore.java:137)                  
                         com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs       (JUnit4IdeaTestRunner.java:68)        
          com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs       (IdeaTestRunner.java:47)              
                     com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart    (JUnitStarter.java:242)               
                     com.intellij.rt.execution.junit.JUnitStarter.main                      (JUnitStarter.java:70)                

	at io.mockk.impl.recording.states.VerifyingState.failIfNotPassed(VerifyingState.kt:66)
	at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:42)
	at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
	at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
	at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
	at io.mockk.MockKDsl.internalVerify(API.kt:118)
	at io.mockk.MockKKt.verify(MockK.kt:139)
	at io.mockk.MockKKt.verify$default(MockK.kt:136)
	at xx.xx.xx.xx.PreferenceManagerTest.test for saving first key(PreferenceManagerTest.kt:37)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Process finished with exit code 255
// -----------------------[ YOUR STACK TRACE ENDS HERE ] -----------------------

Minimal reproducible code (the gist of this issue)

// -----------------------[ YOUR CODE STARTS HERE ] -----------------------

import android.content.Context
import android.content.SharedPreferences
import io.mockk.*
import org.junit.Before
import org.junit.Test

class PreferenceManagerTest {

    private val sharedPref = mockk<SharedPreferences> {
        every { getString(any(), any()) } returns ""
        every { edit().putString(any(), any()).apply() } just Runs
    }

    @Before
    fun setUp() {
        mockkStatic(ABApplication::class)
        every {
            ABApplication.getSingleton().getSharedPreferences(
                    "NewFileName", Context.MODE_PRIVATE
            )
        } returns sharedPref
    }

    @After
    fun tearDown() {
        unmockkAll()
    }

    @Test
    fun `test for saving first key`() {
        val sampleData = DummyData(false)
        PreferenceManager.saveSymbolSS(sampleData)
        verify {
            sharedPref.edit().putString(PreferenceManager.KEY_2, any())
        }
    }

    @Test
    fun `test for saving second key`() {
        val sampleData = DummyData(true)

        PreferenceManager.saveSymbolSS(sampleData)
        verify {
            sharedPref.edit().putString(PreferenceManager.KEY_1, any())
        }
    }
}
import android.content.Context
import android.support.annotation.VisibleForTesting

object PreferenceManager {
    private val sharedPreferences = ABApplication.getSingleton().getSharedPreferences(
            "NewFileName", Context.MODE_PRIVATE
    )
    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    internal const val KEY_1 = "Key.#1"
    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    internal const val KEY_2 = "Key.#2"

    fun saveSymbolSS(symbolType: DummyData) {
        if (symbolType.baseVal) {
            sharedPreferences.edit().putString(KEY_1, "").apply()
        } else {
            sharedPreferences.edit().putString(KEY_2, "").apply()
        }
    }
}
class DummyData(val baseVal: Boolean)

// -----------------------[ YOUR CODE ENDS HERE ] -----------------------

@oleksiyp
Copy link
Collaborator

oleksiyp commented Jun 4, 2019

Try cleaning or unmocking

@kdani41
Copy link
Author

kdani41 commented Jun 4, 2019

@oleksiyp Tried adding unmockAll in teardown method. Same result

@kdani41
Copy link
Author

kdani41 commented Jun 7, 2019

@oleksiyp Can you please confirm is this is a valid issue or am I missing something. For some reason it seems to be the issue with sharedPreferences only??

@stale
Copy link

stale bot commented Aug 6, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as stale just ask to put an important label.

@stale stale bot added the stale label Aug 6, 2019
@stale stale bot closed this as completed Aug 13, 2019
@oleksiyp oleksiyp changed the title AssertException: SharedPreferences - When running couple test cases together Bug: AssertException for SharedPreferences when running couple test cases together Nov 2, 2019
@oleksiyp oleksiyp added this to the 1.9.4 milestone Nov 2, 2019
@oleksiyp oleksiyp added bug and removed stale labels Nov 2, 2019
@oleksiyp oleksiyp reopened this Nov 2, 2019
@oleksiyp oleksiyp added this to To do in Critical to fix Nov 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

2 participants