Volodymyr
Volodymyr

Reputation: 91

Got an error with Junit platform v.5.8.1 java.lang.NoSuchMethodError: org.junit.platform.commons.util.AnnotationUtils.findAnnotation

I run into an issue with this code:

@ExtendWith(MockitoExtension.class)
class ApiRestControllerTest {

  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
  private MockMvc mockMvc;

  @InjectMocks
  private ApiRestController apiRestController;

  @BeforeEach
  void setUp() {
    mockMvc = MockMvcBuilders
        .standaloneSetup(apiRestController)
        .build();
  }

  @Test
  void shouldReturnsVersion() throws Exception {
    mockMvc.perform(get("/api/v1/version"))
        .andExpect(status().isOk())
        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
        .andExpect(content().json(OBJECT_MAPPER.writeValueAsString(new VersionResponse(VERSION))));
  }
} 

I got an error:

Caused by: java.lang.NoSuchMethodError: org.junit.platform.commons.util.AnnotationUtils.findAnnotation(Ljava/lang/Class;Ljava/lang/Class;Z)Ljava/util/Optional;
    at org.junit.jupiter.engine.descriptor.DisplayNameUtils.getDisplayNameGenerator(DisplayNameUtils.java:110)
    at org.junit.jupiter.engine.descriptor.DisplayNameUtils.lambda$createDisplayNameSupplierForClass$2(DisplayNameUtils.java:98)
    at org.junit.jupiter.engine.descriptor.DisplayNameUtils.determineDisplayName(DisplayNameUtils.java:88)
    at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.<init>(JupiterTestDescriptor.java:69)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.<init>(ClassBasedTestDescriptor.java:96)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.<init>(ClassTestDescriptor.java:51)
    at org.junit.jupiter.engine.discovery.ClassSelectorResolver.newClassTestDescriptor(ClassSelectorResolver.java:119

I use JUnit 5.8.1, but for lower versions, like 5.7.2 it works fine,

Upvotes: 7

Views: 9445

Answers (1)

Sam Brannen
Sam Brannen

Reputation: 31177

The missing method org.junit.platform.commons.util.AnnotationUtils.findAnnotation(Class<?>, Class<A>, boolean) was introduced in JUnit Platform 1.8.

So, you need to make sure that you upgrade to junit-platform-commons 1.8.1.

Using the junit-bom will help to simplify such upgrades to ensure you use compatible versions of all JUnit 5 artifacts.

Upvotes: 8

Related Questions