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 https://github.com/spring-projects/spring-boot/issues/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?

Comment From: mbhave

This might be happening because the mock is created for the SomeRepository type instead of the actual type which is OtherRepository or AnotherRepository.

@onacit Just to be sure, could you provide a minimal sample that we can run to reproduce the issue?

Comment From: onacit

@mbhave Thank you. I managed to create a simplest and reproducible project from my original code base.

https://github.com/onacit/spring-boot-issue20916

There is only one @Test method there in an abstract class and there are two actual test classes inherit.

  • ...some0.SomeProductRepositoryServiceTest which passes.
  • ...some1.SomeProductRepositoryServiceTest which fails.

Comment From: mbhave

Thanks for the sample @onacit. I have taken a look and it seems like a bug to me. It is marked for: team-attention which means we will discuss it on one of the weekly team calls.

Comment From: Xyaren

Could this be backported to 2.2.x ? We would really appreciate that, as we are still relying on Elasticsearch Jest, that is not included in 2.3.x anymore.

Comment From: wilkinsona

@Xyaren The fix was made in 2.2.9.

Comment From: Xyaren

I'm sorry, picket I got confused between two issues. Thanks for the quick response!