Skip to content

MockBean does not resolve the right type for abstract test class #20916

@onacit

Description

@onacit

Please forgive me if this kind of issue is not appropriate. I already SO-ed.

Just in cases of issue titles, this issue might have a same issue with #15969. I'm not sure.

I have a tightly coupled abstract service class with generic types.

I have an abstract service class which autowires a specific type of repository.

@NoRepositoryBean
interface SomeRepository<T> { }

abstract class SomeService<T extends SomeRepository<U>, U ...> {

    @Autowired
    private U repositoryInstance;
}

Now I'm trying to create an abstract test class for subclasses of the service class.

@SpringBootTest
abstract class SomeServiceTest<T extends SomeService<U>, U extends SomeRepository<V>, V ...> {

    @Autowired
    private T serviceInstance;

    // personally expected
    // to mock the serviceInstance.repositoryInstance, and it doesn't.
    @MockBean
    private U repositoryInstance; // != serviceInstance.repositoryInstance();
}

But mocking the bean in a test class of actual service class works.
(Which is I intended that extended modules don't have to do.)

class OtherServiceTest
        extends SomeServiceTest<OtherService, OtherRepository, ...> {

    @TestConfiguration
    OtherServiceTestConfiguration {

        // WORKS!!!
        // == serviceInstance().repositoryInstance();
        @MockBean private OtherRepository repositoryInstance;
    }
}

class AnotherServiceTest
        extends SomeServiceTest<AnotherService, AnotherRepository, ...> {

    // WORKS!!!
    // == serviceInstance().repositoryInstance();
    @MockBean private AnotherRepository repositoryInstance;
}

Is there any intrinsic mechanism for mocking the SomeServiceTest#serviceIntance.repositoryInstance?

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions