Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Controller Test: mocking service requiring downstream objects causing ApplicationContext to not load

I am attempting to run a controller-level Spring Boot unit test with a Mock for my service-layer dependency. However, this Mock is requiring a downstream repository dependency which uses an EntityManager object, which is causing my test to fail on loading the ApplicationContext.

My test does not involve the repository dependency or EntityManager, it is using the Mocked service object to return a canned response. Why is Spring complaining about the repo/EntityManager if I only want to mock the service-layer object?

Controller unit test code:

@RunWith(SpringRunner.class)
@WebMvcTest
@AutoConfigureWebClient
public class MobileWearControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    UserDeviceService userDeviceService;

    //.....
}

UserDeviceService code:

@Service
public class UserDeviceService {

    private UserDeviceRepository userDeviceRepository;

    public UserDeviceService(UserDeviceRepository userDeviceRepository) {
        this.userDeviceRepository = userDeviceRepository;
    }

    //....
}

UserDeviceRepository code:

@Repository
public class UserDeviceRepositoryImpl implements UserDeviceRepositoryCustom {

    @PersistenceContext
    private EntityManager em;

    //....
}

Expecting the test to run.

Actual result is getting the following stack trace:

java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDeviceRepositoryImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
...

like image 929
Alan Bentley Avatar asked May 25 '26 21:05

Alan Bentley


1 Answers

My issue was the annotations I was using for my test.

Using @AutoConfigureWebClient tries to standup the entire Spring Context; since I am unit testing my controller I want to test only the web layer and mock the downstream dependencies (ie UserDeviceService). So, I should be using @SpringBootTest and @AutoConfigureMockMvc instead, which will set up my Spring context only for the controller layer.

Using this approach I'm able to get UserDeviceService to mock successfully and thus allowing my test to compile and run:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MobileWearControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    UserDeviceService userDeviceService;

    //...
}
like image 131
Alan Bentley Avatar answered May 27 '26 10:05

Alan Bentley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!