How to inject a service component in Spring Test class using Kotlin?

Viewed 1275

I am trying to test the business logic/services. Using Kotlin in my Spring Boot application, I am trying to instantiate my services, using the test class constructor. But this is not allowed, as I am getting a ParameterResolutionException.

This is my code:

@SpringBootTest
class IntermediateApplicationTests(val ticketService: TicketService) {


    @Test
    fun contextLoads() {
    }

}

How can I achieve this?

Thanks for every help!

1 Answers

You can use the @Autowired annotation to do this:

@SpringBootTest
class IntermediateApplicationTests @Autowired constructor(
    val ticketService: TicketService
) {


    @Test
    fun contextLoads() {
    }

}

In fact this is the recommended way of doing injection and the IDEA Spring plugin will complain if you do it some other way.

Related