【问题标题】:Got an error with Junit platform v.5.8.1 java.lang.NoSuchMethodError: org.junit.platform.commons.util.AnnotationUtils.findAnnotationJunit 平台 v.5.8.1 java.lang.NoSuchMethodError: org.junit.platform.commons.util.AnnotationUtils.findAnnotation 出现错误
【发布时间】:2021-09-30 16:13:48
【问题描述】:

我遇到了这个代码的问题:

@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))));
  }
} 

我遇到了一个错误:

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

我使用 JUnit 5.8.1,但对于较低版本,如 5.7.2,它可以正常工作,

【问题讨论】:

  • 很可能是版本不匹配。请发布您的完整 Gradle 或 Maven 构建文件。

标签: java spring-mvc junit5 spring-test


【解决方案1】:

在 JUnit Platform 1.8 中引入了缺失的方法 org.junit.platform.commons.util.AnnotationUtils.findAnnotation(Class&lt;?&gt;, Class&lt;A&gt;, boolean)

因此,您需要确保升级到junit-platform-commons 1.8.1。

使用 junit-bom 将有助于简化此类升级,以确保您使用所有 JUnit 5 工件的兼容版本。

【讨论】:

  • 谢谢你为我做的。将此注释留给其他潜在读者:我的父 Maven 项目依赖管理分别指定了 org.junit.jupiter:junit-jupiter-engineorg.junit.jupiter:junit-jupiter-api。更改为org.junit:junit-bom,错误就消失了。 example project 应该是一个很好的参考。
猜你喜欢
  • 2011-01-26
  • 2021-11-24
  • 1970-01-01
  • 2020-09-07
  • 2018-08-06
  • 2011-09-05
  • 1970-01-01
  • 2016-06-14
  • 2023-04-08
相关资源
最近更新 更多