1

I am trying to write junit test cases for my Spring boot application written in 1.2.5.RELEASE version

I have read the spring boot documentation https://docs.spring.io/spring-boot/docs/1.2.5.RELEASE/reference/htmlsingle/ and trying to write the test case.
Now I want to mock a class which is already a @Primary class in original application.

see the issue here - How to mock a class that been annotated with Primary

On further read I understand that I need to use @MockBean to mock a class which is already a primary in application. But the problem is the spring boot version which I use does not support @MockBean.
I have tried overriding spring-boot-starter-test version alone with 2.1.7.RELEASE(without changing the spring boot parent version as I am not supposed to change the application spring boot version which has many impacts). Now I am able to use @MockBean, but on running the test case, it ends in exception.
java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.getRepeatableAnnotations

  1. How can I use @MockBean in spring boot 1.2.5.RELEASE?

  2. Is there any workaround without using @MockBean?

User_1940878
  • 321
  • 1
  • 6
  • 25
  • `@MockBean` is introduced in 1.4. Probably, you can upgrade from 1.2 to 1.4? – Alexander Pavlov Aug 24 '21 at 14:41
  • you can use Mockito.mock for mocking – vaibhavsahu Aug 24 '21 at 14:45
  • @Alexander Pavlov The original application is a product code. We are not supposed to change the version!! – User_1940878 Aug 24 '21 at 14:45
  • 1
    Using different versions of spring-boot vs spring-boot-test especially over such major version difference is unlikely to work. The 2.x version of spring-boot-test would likely rely on methods from much later version of other spring libraries. `@MockBean` was introduced in 1.4 so try using that version of spring-boot-test instead of 2.1.7. If that doesn't work use the `Mockito.mock` paired with `@Bean` in one of the test context classes. – madteapot Aug 24 '21 at 15:26
  • Just a sidenote... Spring Boot 1.5.22 is EOL since August 19. If I were you I would urge to upgrade that app to 2.x (before to 1.5.22) – Joker Aug 24 '21 at 21:11

0 Answers0