用JUnit运行Spring的问题

时间:2014-03-12 17:57:04

标签: java spring maven spring-mvc junit

在使用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

所以,我认为这个问题是由于spring依赖性不匹配造成的。但是,它似乎并不适合我。基本上,如果我理解正确的解决方案是什么链接,就是明确声明我在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文件中没有这些版本号的内容,所以有些东西显然是在调用它们。有没有办法确保没有安装这些?或者,如果他们需要在那里,因为他们是来自其他deps的依赖,我怎样才能确保我的测试上下文不使用那些而不是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>

1 个答案:

答案 0 :(得分:2)

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

Spring Security 3.1.3依赖于所有上述版本3.0.7的弹簧罐.RELEASE

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