-1

Mockk Error in android Koltin Unit test. matching with Two URL

io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock

@Before
    fun setUp() {
        mockkStatic(Uri::class)
        every { Uri.parse(any()) } returns mockk()
        every { Uri.Builder() } returns mockk()// Error occurred here
   
        MockKAnnotations.init(this, relaxUnitFun = true)
    }
 @Test
    fun isParamTest() {
        every { Uri.parse(any()) } returns mockk(relaxed = true)
        every { Uri.Builder() } returns mockk(relaxed = true)
        val  inputURL  ="https://test"
        val expected="https://test"
       parameterKey="test",value="testvalue"
        val Output = getWebURL(inputURL,parameterKey,value)
        assertThat(Output).isEqualTo(expected)
    }

    private Uri buildURI(String url, Map<String, String> params) {
    
        // build url with parameters.
        Uri.Builder builder = Uri.parse(url).buildUpon();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.appendQueryParameter(entry.getKey(), entry.getValue());
        }
    
        return builder.build();
    }

Thanks in Advance

Need to solve this unit test case
Android Dev
  • 421
  • 8
  • 26

1 Answers1

1

Uri.Builder() is not a static function call of Uri but a constructor call of Uri.Builder. Thus, you cannot use every here. Instead, you can use a constructor mock:

mockkConstructor(Uri.Builder::class)

Then every call to Uri.Builder() automatically returns a mock.

Karsten Gabriel
  • 3,115
  • 6
  • 19