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

Mocking external function or variable throws UnsatisifiedLinkError #1077

Open
olllo123 opened this issue Apr 12, 2023 · 3 comments
Open

Mocking external function or variable throws UnsatisifiedLinkError #1077

olllo123 opened this issue Apr 12, 2023 · 3 comments

Comments

@olllo123
Copy link

Expected Behavior

Mocking external methods and variables works

Current Behavior

Trying to mock external method or variable causes UnsatisifiedLinkError being thrown.

Failure Information (for bugs)

Trying to provide mocked behaviour for any method or variable marked as external results in UnsatisifiedLinkError being thrown. Although there is issue already reported here this one is connected with mocking simple Class mocking not Object.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Create a simple class with external method:
class ClassWithExternalMethod {
    external fun someExternalCall()
    var listener: Any?
        external get
        external set
}
  1. Create a test method mocking above
class TestClass {
    
    @Test
    fun test(){
        val mock: ClassWithExternalMethod = mockk {
            every { someExternalCall() } just Runs
        }
        mock.someExternalCall()
    }
    
}
  1. Run the test above.

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.13.4
  • OS: Mac
  • Kotlin version: 1.7.20
  • JDK version: 1.8
  • JUnit version: JUnit 4
  • Type of test: Unit

Stack trace

'void com.test.android.common.ClassWithExternalMethod.someExternalCall()'
java.lang.UnsatisfiedLinkError: 'void com.test.android.common.ClassWithExternalMethod.someExternalCall()'
	at com.test.android.common.ClassWithExternalMethod.someExternalCall(Native Method)

@stevepham
Copy link

any update for this issue? I tried to mockStatic System and mock method loadLibrary but seems it causes another issue when mock System class

@rfermontero
Copy link

The same, looking for a solution.

@olllo123
Copy link
Author

olllo123 commented Nov 14, 2023

I did find a workaround though.

Create a class eg. FakeClassWithExternalMethod that inherits from ClassWithExternalMethod

In it's implementation override all the external methods of base class so super.someExternalMethod is not called.

Then when mocking use instance of FakeClassWithExternalMethod instead of ClassWithExternalMethod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants