Skip to content

When using kotlin Collection then @Autowire Collection<SomeInterface> does not work correctly #20494

Closed as not planned
@spring-projects-issues

Description

@spring-projects-issues

Mario Zagar opened SPR-15940 and commented

Using:

  • spring-framework 5.0.0.RC3 (also tested wth 1.5.6.RELEASE, problem also present)
  • kotlin-stdlib 1.1.4-3

Following test fails when autowiring kotlin Collection @Autowired lateinit var beans : Collection.

When using java.util.Collection autowiring works as expected.

Not really sure if this is kotlin issue or spring issue.

Here's the test:

package spring.autowire.test

import org.assertj.core.api.Assertions
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringRunner

interface FirstInterface
interface SecondInterface
interface ThirdInterface

class Bean1 : FirstInterface, SecondInterface, ThirdInterface
class Bean2 : FirstInterface, SecondInterface, ThirdInterface
class Bean3 : FirstInterface, SecondInterface, ThirdInterface
class Bean4 : FirstInterface, SecondInterface, ThirdInterface

@Configuration
open class TestSpringConfig {
    @Bean open fun bean1() : Bean1 = Bean1()
    @Bean open fun bean2() : FirstInterface = Bean2()
    @Bean open fun bean3() : SecondInterface = Bean3();
    @Bean open fun bean4() : ThirdInterface = Bean4();
}

@RunWith(SpringRunner::class)
@ContextConfiguration(classes = arrayOf(TestSpringConfig::class))
class KotlinAutowireTest {
    @Autowired
    lateinit var beans : Collection<SecondInterface> // test passes if I use java.util.Collection<SecondInterface>

    @Test
    fun test() {
        Assertions.assertThat(beans)
                .extracting{it.javaClass.name}
                .containsOnly(
                        "spring.autowire.test.Bean1",
                        "spring.autowire.test.Bean2",
                        "spring.autowire.test.Bean3",
                        "spring.autowire.test.Bean4"
                )
    }
}

Test output:

java.lang.AssertionError: 
Expecting:
  <["spring.autowire.test.Bean1", "spring.autowire.test.Bean3"]>
to contain only:
  <["spring.autowire.test.Bean1",
    "spring.autowire.test.Bean2",
    "spring.autowire.test.Bean3",
    "spring.autowire.test.Bean4"]>
but could not find the following elements:
  <["spring.autowire.test.Bean2", "spring.autowire.test.Bean4"]>

Affects: 5.0 RC3

1 votes, 3 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: duplicateA duplicate of another issuetheme: kotlinAn issue related to Kotlin support

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions