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

lateinit property has not been initialized on some ancestor classes #1214

Open
superdrenner opened this issue Feb 1, 2024 · 2 comments
Open

Comments

@superdrenner
Copy link

Expected Behavior

Mockks should be injected in all superclasses and not just the top most parent

  • MockK version: 1.13.9
  • OS: Mac OS 13.6.3
  • Kotlin version: 1.4.10
  • JDK version: Corretto-11.0.21.9.1 (build 11.0.21+9-LTS)
  • JUnit version: 5.7.0
  • Type of test: Unit Test
kotlin.UninitializedPropertyAccessException: lateinit property convertApplicationService has not been initialized

Minimal reproducible code (the gist of this issue)

abstract class Grandparent {
    @Autowired
    private lateinit var serviceA: ServiceA
	
    open fun doSomething() {
        // serviceA has been injected correctly
        serviceA.doIt()
    }
}

abstract class Parent : Grandparent() {
    @Autowired
    lateinit var serviceB: ServiceB
  
    override fun doSometing() {
        super.doSomething()
  
        // Failure happens here, serviceB has not been initialized
        serviceB.doIt()
    }
}

class Child: Parent() {
    @Autowired
    lateinit var serviceC: ServiceC
 
    override fun doSomething() {
        super.doSomething()
        // serviceC has been injected correctly
        serviceC.doIt()
    }
}

@ExtendWith(MockkExtension::class)
class ChildTest {
    @Mockk
    private lateinit var serviceA: ServiceA

    @Mockk
    private lateinit var serviceB: ServiceB

    @Mockk
    private lateinit var serviceC: ServiceC

    @InjectMockks
    private lateinit var child: Child


    @Test
    fun theTest () {
       child.doSomething()
    }
}
@OmarBeshary
Copy link

Hi @drennerpfc6 , Did you use MockKAnnotations.init() in your test or not ?

@superdrenner
Copy link
Author

@OmarBeshary the test class is using MockkExtension so MockkAnnotations.init() is not required.

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

2 participants