【问题标题】:SpringFramework JUnit MergedAnnotations java.lang.NoClassDefFoundErrorSpringFramework JUnit MergedAnnotations java.lang.NoClassDefFoundError
【发布时间】:2020-11-18 20:54:21
【问题描述】:

尝试在 Java SpringBoot 应用程序中运行一些单元测试。

我运行的单元测试是:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertEquals;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {WebAppInitializer.class})
public class FilePropertyInjectionUnitTest {
   @Value("${geckoDir}")
   private String geckoDir;

   @Test
   public void whenFilePropertyProvided_thenProperlyInjected() {
      assertEquals(geckoDir,"C:\\GeckoDriver\\geckodriver.exe");
   }
}

我收到以下错误:

java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations

at org.springframework.test.context.support.DynamicPropertiesContextCustomizerFactory.isAnnotated(DynamicPropertiesContextCustomizerFactory.java:53)
at org.springframework.core.MethodIntrospector.lambda$selectMethods$1(MethodIntrospector.java:97)
at org.springframework.core.MethodIntrospector.lambda$selectMethods$0(MethodIntrospector.java:74)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:410)
at org.springframework.core.MethodIntrospector.selectMethods(MethodIntrospector.java:72)
at org.springframework.core.MethodIntrospector.selectMethods(MethodIntrospector.java:96)

我使用 ItelliJ 作为 IDE。

【问题讨论】:

  • 能否请您添加 pom.xml 或 build.gradle 详细信息?
  • 这样的错误通常来自于你正在混合来自不同版本的框架的 jars/modules,在这种情况下是 Spring。
  • 是的,我知道。但不确定我在混合什么。是一个大项目,有很多依赖。

标签: java spring spring-boot maven junit


【解决方案1】:

我终于想通了。我不得不将 spring-test 依赖升级到版本 5.2.8:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.2.8.RELEASE</version>
        <scope>test</scope>
    </dependency>

我真的用这个掉进了兔子洞

【讨论】:

    【解决方案2】:

    以下是我的 POM 的一部分。我不知道什么罐子发生冲突。

       <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
    
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>
    

    【讨论】:

      猜你喜欢
      • 2013-04-06
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 2014-04-15
      • 2017-01-07
      相关资源
      最近更新 更多