当前位置: 首页 > 知识库问答 >
问题:

JUNIT期间的Spring Maven依赖问题

严远
2023-03-14
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.company</groupId>
  <artifactId>artifact</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>artifact</name>
  <url>http://maven.apache.org</url>

  <properties>
    <org.springframework.version>3.2.3.RELEASE</org.springframework.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
     <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc-portlet</artifactId>
        <version>${org.springframework.version}</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
  <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/test/resources</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
            <excludes>
                <exclude>**/*local.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
   <testResources>
                  <testResource>
                    <directory>src/test/java</directory>
                    <includes>
                      <include>**/*</include>
                    </includes>
                    <excludes>
                      <exclude>**/*.java</exclude>
                    </excludes>
                  </testResource>
                  <testResource>
                    <directory>src/test/resources</directory>
                    <includes>
                      <include>**/*</include>
                    </includes>
                  </testResource>
                </testResources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>

  </build>

</project>
java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;
    at org.springframework.test.context.ContextLoaderUtils.resolveContextLoader(ContextLoaderUtils.java:118)
    at org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:594)
    at org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:560)
    at org.springframework.test.context.TestContext.<init>(TestContext.java:99)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:117)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:119)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:108)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我的Junit示例类是

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring-config.xml")
public class HelloWorldTest {


    @Autowired
    public HelloWorld hello1;

    @Test
    public void sayHelloTest(){
         hello1.printHello();
    }
}

共有1个答案

纪秋月
2023-03-14

我通过检查与我使用的spring版本的早期版本有关的spring的依赖关系来解决这个问题。

例如,我使用的是Spring3.2.4版本,而在我的依赖项中,我发现spring-beans和spring-expression与3.0.4版本相同。我所做的是将这两个添加到我使用的spring版本的pom.xml依赖项中。在本例中添加

 <dependency>
   <groupId> org.springframework </ groupId>
   <artifactId> spring-beans </ artifactId>
   <version> ${spring.version} </ version>
 </ dependency>
 <dependency>
   <groupId> org.springframework </ groupId>
   <artifactId> spring-expression </ artifactId>
   <version> $ {spring.version} </ version>
 </ dependency>

而且奏效了。

 类似资料:
  • 问题内容: 最近换用了Go 1.11版本,并正在尝试将我们的项目转换为使用新的模块系统。但是,我在缓存系统方面遇到了一个令人沮丧的问题(到目前为止,由于过去不相关的问题,我一直在使用它,但是模块不可以这样做)。 下面的命令日志基于使用我的系统使用Homebrew进行的全新升级到1.11(我通常使用gvm来安装和管理Go版本,但是为此交换到系统构建中以查看是否是gvm)。我将我的GOPATH设置为一

  • 对于 REST API 的测试部分。正在查看可以在Spring启动中使用的依赖项。我看到有JUnit依赖项和Spring启动器测试依赖项。 JUnit依赖项和Spring Boot启动器测试依赖项有什么区别?

  • 我认为这是spring boot Framework内部的依赖问题: 依附性层次制 所以我在pom.xml中添加了以下几行: 拉维。

  • 我的JUnit测试用例在将datastax Cassandra驱动程序升级到3.0.1后中断。添加了一个检查,以确保Google guava版本16.0.1的使用与Datastax升级页面中描述的一样:第3点。 pom.xml相关部分包括: 这是Maven的依赖树。只是因为它太大了,所以才会在相关章节上复制。

  • org.springframework.beans.factory.unsatisfiedDependencyException:创建名为“com.sonos.arcor.service.AddressServiceTest”的bean时出错:通过字段“service”表示的未满足的依赖项;嵌套异常为org.springframework.beans.factory.noSuchBeanDefin

  • 我的应用程序中使用了弹性城堡依赖项,我希望在pom.xml文件中排除此依赖项。即使从pom文件中删除此依赖项,它仍然出现在m2存储库文件夹中。我的应用程序是Spring-MVC 5框架,运行在Tomcat版本9上。部署时有两个不同版本的“bcprov”。我需要从pom.xml文件中排除bcprov-jdk14和bcprov-jdk15on。 下面是pom文件中的依赖关系。 我还尝试了另一个链接的排