【问题标题】:Problems running Spring with JUnit使用 JUnit 运行 Spring 的问题
【发布时间】:2014-04-17 02:03:19
【问题描述】:

在尝试使用 Eclipse、Spring 和 JUnit 运行一个简单的测试(还没有真正的断言)时,我遇到了以下问题。以下是它的抱怨:

java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;

我在 StackOverflow 上发现了一个类似的问题: Spring JUnit Test Error

所以,我认为问题是由于弹簧依赖项不匹配造成的。但是,它似乎对我不起作用。基本上,如果我正确理解该链接所说的解决方案是什么,那就是明确声明我正在做的 pom 文件上的 spring 依赖项的版本(我声明所有依赖项都具有 3.1.x 版本)。

当我查看 ~/m2 时,我可以发现安装了这些早于 3.1 的依赖项,即使我将它们全部删除并再次运行 maven build:

./spring-aop/3.0.4.RELEASE
./spring-aop/3.0.7.RELEASE
./spring-asm/3.0.4.RELEASE
./spring-asm/3.0.7.RELEASE
./spring-beans/3.0.4.RELEASE
./spring-beans/3.0.7.RELEASE
./spring-context/3.0.4.RELEASE
./spring-context/3.0.7.RELEASE
./spring-core/3.0.4.RELEASE
./spring-core/3.0.7.RELEASE
./spring-expression/3.0.4.RELEASE
./spring-expression/3.0.7.RELEASE
./spring-jdbc/3.0.7.RELEASE
./spring-parent/3.0.4.RELEASE
./spring-parent/3.0.7.RELEASE
./spring-tx/3.0.7.RELEASE
./spring-web/3.0.4.RELEASE
./spring-web/3.0.7.RELEASE

我的 pom 文件中没有关于这些版本号的任何内容,因此很明显有些东西在调用它们。有没有办法确保没有安装这些?或者,如果它们需要在那里,因为它们是来自其他部门的依赖项,我如何确保我的测试上下文不使用那些,而是使用 3.1?

这就是我要运行的:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sample.persistence.manager.ProfileManager;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={ JpaTestConfig.class, TestConfig.class })
public class ProfileManagerTest {

    @Autowired
    private ProfileManager profileManager;

    @Test
    public void testCreateAndRetrieve() {

    }

}

pom.xml:

      <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.sample</groupId>
        <artifactId>common</artifactId>
        <version>0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>common</name>

        <parent>
            <groupId>com.sample</groupId>
            <artifactId>ws-parent</artifactId>
            <version>0.1-SNAPSHOT</version>
            <relativePath>..</relativePath>
        </parent>

        <properties>
            <spring.security.version>3.1.3.RELEASE</spring.security.version>
            <spring.framework.version>3.1.1.RELEASE</spring.framework.version>        
            <hibernate.version>3.6.0.Final</hibernate.version>
            <commons-dbcp.version>1.2.2</commons-dbcp.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-asm</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>        

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

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>

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

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-parent</artifactId>
                <version>3.1.1.RELEASE</version>
                <type>pom</type>
            </dependency>        

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>        

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>        

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.ws</groupId>
                <artifactId>spring-oxm-tiger</artifactId>
                <version>1.5.4</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-core</artifactId>
                <version>${spring.security.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-web</artifactId>
                <version>${spring.security.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-config</artifactId>
                <version>${spring.security.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.framework.version}</version>
            </dependency>        

            <dependency>
                <groupId>com.relayrides</groupId>
                <artifactId>pushy</artifactId>
                <version>0.1.1</version>
            </dependency>

            <dependency>
                <groupId>org.versly</groupId>
                <artifactId>versly-wsdoc</artifactId>
                <version>1.0-SNAPSHOT</version>
                <scope>compile</scope>
            </dependency>

            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk</artifactId>
                <version>1.4.2.1</version>
            </dependency>

            <!-- Hibernate -->

            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
            </dependency>

            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-ehcache</artifactId>
                <version>${hibernate.version}</version>
            </dependency>

            <!--
                    <dependency>
                        <groupId>javax.persistence</groupId>
                        <artifactId>persistence-api</artifactId>
                        <version>${javax.persistence.version}</version>
                    </dependency>
            -->

            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>${commons-dbcp.version}</version>
            </dependency>

            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2</version>
            </dependency>

            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.5</version>
            </dependency>

            <dependency>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils</artifactId>
                <version>1.8.3</version>
            </dependency>

            <dependency>
                <groupId>javax.annotation</groupId>
                <artifactId>jsr250-api</artifactId>
                <version>1.0</version>
            </dependency>

            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <version>1.8.4</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.4</version>
            </dependency>

            <dependency>
                <groupId>com.googlecode.json-simple</groupId>
                <artifactId>json-simple</artifactId>
                <version>1.1.1</version>
            </dependency>


            <dependency>
                <groupId>hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
                <version>1.8.0.10</version>
            </dependency>
        </dependencies>

        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
                <resource>
                    <directory>src/main/sertificates</directory>
                </resource>
            </resources>
            <filters>
                <filter>../${build.profile}.properties</filter>
            </filters>

            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.5.7.201204190339</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>report</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                </plugin>

                <plugin>
                    <groupId>org.bsc.maven</groupId>
                    <artifactId>maven-processor-plugin</artifactId>
                    <version>1.3.6</version>

                    <configuration>
                        <outputDiagnostics>true</outputDiagnostics>
                        <processors>
                            <processor>org.versly.rest.wsdoc.AnnotationProcessor</processor>
                        </processors>
                    </configuration>

                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>process</goal>
                            </goals>
                        </execution>
                    </executions>

                    <dependencies>

                        <dependency>
                            <groupId>org.versly</groupId>
                            <artifactId>versly-wsdoc</artifactId>
                            <version>1.0-SNAPSHOT</version>
                            <scope>compile</scope>
                        </dependency>
                    </dependencies>
                </plugin>

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.2</version>

                    <executions>
                        <execution>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>java</goal>
                            </goals>

                            <configuration>
                                <mainClass>org.versly.rest.wsdoc.RestDocAssembler</mainClass>
                                <arguments>
                                    <argument>${project.build.directory}/classes</argument>
                                </arguments>
                            </configuration>

                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
            </plugins>
            <pluginManagement>
                   <plugins>
                           <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                           <plugin>
                                   <groupId>org.eclipse.m2e</groupId>
                                   <artifactId>lifecycle-mapping</artifactId>
                                   <version>1.0.0</version>
                                   <configuration>
                                           <lifecycleMappingMetadata>
                                                   <pluginExecutions>
                                                           <pluginExecution>
                                                                   <pluginExecutionFilter>
                                                                           <groupId>org.jacoco</groupId>
                                                                           <artifactId>
                                                                                   jacoco-maven-plugin
                                                                           </artifactId>
                                                                           <versionRange>
                                                                                   [0.5.7.201204190339,)
                                                                           </versionRange>
                                                                           <goals>
                                                                                   <goal>prepare-agent</goal>
                                                                           </goals>
                                                                   </pluginExecutionFilter>
                                                                   <action>
                                                                           <ignore></ignore>
                                                                   </action>
                                                           </pluginExecution>
                                                   </pluginExecutions>
                                           </lifecycleMappingMetadata>
                                   </configuration>
                           </plugin>
                   </plugins>
            </pluginManagement>        
        </build>
    </project>

【问题讨论】:

  • 你能发布你的pom.xml吗?
  • 好的,刚刚添加到主要问题
  • 顺便说一下,我可以在 Eclipse 上看到依赖关系层次结构,并且我可以看到一些依赖关系试图为其他库获取可传递的 3.0.x 版本。我只是不知道如何强迫它不得到那些但得到 3.1。而是
  • 由于 spring-security jars,你得到了这些依赖项。
  • 为什么你不能为 Spring Security 的依赖项添加一个排除项(或者任何依赖项会引入错误的 JAR 版本)?类似于这里的答案:stackoverflow.com/questions/3659810/…

标签: java spring maven spring-mvc junit


【解决方案1】:

尝试排除 spring-aop、spring-beans、spring-context、spring-core、spring-expression、spring-jdbc 和 spring-tx 来自 spring 安全依赖项。

Spring Security 3.1.3 依赖于所有上述版本 3.0.7.RELEASE 的 spring jar

有关如何在此处添加排除项的更多详细信息:How to handle sub projects dependencies in Maven

【讨论】:

  • 这个成功了!谢谢!
猜你喜欢
  • 2014-09-21
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2023-03-03
  • 1970-01-01
  • 2016-03-13
相关资源
最近更新 更多