question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JUnit testing - question

See original GitHub issue

Let’s assume I have a class that has a primary constructor with single parameter (this is required by Android)

class PaymentViewModel(application: Application) : AndroidViewModel(application) {
}

…and now I need to define dependencies using instance() delegate (I can’t add more parameters to the primary constructor)

class PaymentViewModel(application: Application) : AndroidViewModel(application) {
     private val featureEventBus: FeatureEventBus by instance()
}

The question is how to test it?

@RunWith(MockitoJUnitRunner.StrictStubs::class)
class PaymentViewModelTest {

   @Mock
   private lateinit var mockApplication: Application

   @Mock
   private lateinit var mockFeatureEventBus: FeatureEventBus

   private lateinit var cut: PaymentViewModel

   @Before
   fun setUp() {
        // I want FeatureEventBus property delegate to retrieve
        // mockFeatureEventBus instance
		cut = PaymentViewModel(mockApplication)
    }
}



Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
romainbslcommented, Oct 15, 2019

Yes but we bring it back for those that don’t want to add the import manually 😃

0reactions
igorwojdacommented, Oct 15, 2019

adding import manually works, so I guess after Kotlin plugin will be fixed this will work properly without closestKodein

Read more comments on GitHub >

github_iconTop Results From Across the Web

Top JUnit Interview Questions (2022) - InterviewBit
Open-source unit testing framework for writing test cases. TestNG is similar to JUnit but with extended functionalities. JUnit does not support ...
Read more >
Top 23 JUnit Interview Questions (2023) - javatpoint
JUnit Interview Question · 1) What is Testing? · 2) What is JUnit? · 3) What is unit testing? · 4) What is...
Read more >
Top 11 JUnit Interview Questions and Answers (2022)
1) Explain what is JUnit? 2) Explain what is Unit Test Case? 3) Explain how you can write a simple JUnit test case?...
Read more >
Top 20 JUnit Interview Questions with Answers for Java ...
20+ JUnit [Java Unit Testing] Interview Questions with Answers · 1. What is JUnit? · 2. What is the difference between JUnit 3....
Read more >
JUnit - Interview Questions - Tutorialspoint
Explain unit testing using Mock Objects.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found