lateinit property apiInterface has not been initialized kotlin.UninitializedPropertyAccessException:?

We Are Going To Discuss About lateinit property apiInterface has not been initialized kotlin.UninitializedPropertyAccessException:?. So lets Start this Android Article.

lateinit property apiInterface has not been initialized kotlin.UninitializedPropertyAccessException:?

How to solve lateinit property apiInterface has not been initialized kotlin.UninitializedPropertyAccessException:?

According to the exception, you are not initializing the apiInterface.
In normal cases Retrofit is responsible for creating the instance of NewsInterface. But in case of testing, you will be responsible for passing the instance. For that, you can create the mock implementation.
class MockNewsInterface: NewsInterface{ suspend fun getNews(): NewsResponse{ return NewsResponse() // Pass dummy implementation } }
Just replace this in your code
newsRepository = NewsRepository(MockNewsInterface())
I would suggest to use Mockito or Mockk.io library. This will help write the test cases easily and efficiently.

lateinit property apiInterface has not been initialized kotlin.UninitializedPropertyAccessException:?

According to the exception, you are not initializing the apiInterface.
In normal cases Retrofit is responsible for creating the instance of NewsInterface. But in case of testing, you will be responsible for passing the instance. For that, you can create the mock implementation.
class MockNewsInterface: NewsInterface{ suspend fun getNews(): NewsResponse{ return NewsResponse() // Pass dummy implementation } }
Just replace this in your code
newsRepository = NewsRepository(MockNewsInterface())
I would suggest to use Mockito or Mockk.io library. This will help write the test cases easily and efficiently.

Solution 1

According to the exception, you are not initializing the apiInterface.

In normal cases Retrofit is responsible for creating the instance of NewsInterface. But in case of testing, you will be responsible for passing the instance. For that, you can create the mock implementation.

class MockNewsInterface: NewsInterface{
  suspend fun getNews(): NewsResponse{
     return NewsResponse() // Pass dummy implementation 
  }
}

Just replace this in your code

newsRepository = NewsRepository(MockNewsInterface())

I would suggest to use Mockito or Mockk.io library. This will help write the test cases easily and efficiently.

Original Author Ankit Chandora Of This Content

Solution 2

As your exception says lateinit property apiInterface has not been initialized

the apiInterface in BBCNewsViewModelTest is not initialized

and in your setUp() method, you’re initializing newsRepository = NewsRepository(apiInterface) while the apiInterface is not initialized yet

you can initialize your apiInterface while declaring without lateinit, or in the setup() or if youre using any Dependency Injection you can do field injection

since you’re using retrofit this is how you’d have to initialize

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .build();

NewsInterface service = retrofit.create(NewsInterface.class);

Original Author Praveen G Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment