简体   繁体   中英

kotlin.UninitializedPropertyAccessException: lateinit property has not been initialized

This is my main function

object Service {
  fun getConfigMappings(client: RedissonClient, request: GetRequest): IndataType {
        ****
        return obj
  }
}

I calling it in my main class, and everything works good, I can get the response.

@Autowired
lateinit var client: RedissonClient

val indataObj = Service.getConfigMappings(client, request)

When I want to write a test for it, I got error "kotlin.UninitializedPropertyAccessException: lateinit property client has not been initialized", can anyone help me with that? "

class ServiceTest {
    @Autowired
    lateinit var client: RedissonClient

    @Test
    fun `test1`() {

        val request = GetRequest {
            ***
        }

        val indataObj = Service.getConfigMappings(client, request)

      }
 }

kotlin.UninitializedPropertyAccessException: lateinit property has not been initialized is occured because there is no configuration for AutoWired .

If you want to use AutoWired in the unit test, it needs annotation for configuration.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:config/springbeans.xml")
public class BeanSpringTest {

if you want to use @Autowired, there must have a @Bean somewhere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM