marcelorocks
marcelorocks

Reputation: 2035

Problems running Spring with JUnit

I an having the following when trying to run a simple test (no real assertions yet) using Eclipse, Spring and JUnit. Here is what it complains about:

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

I have found a similar issue on StackOverflow here: Spring JUnit Test Error

So, I supposed the problem was due to spring dependencies mismatch. However, it doesn't seem to work for me. Basically, if I understand it right what that link says the solution is, is to declare explicitly the version of my spring dependencies on the pom file, which I am doing (I am declaring all dependencies to have 3.1.x versions).

When I look at ~/m2, I can find these prior-to-3.1 dependencies installed, even when I remove them all and run maven build again:

./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

I don't have anything with these version numbers on my pom file so something is clearly calling them. Is there a way to make sure these are not installed? Or, if they need to be there because they are dependencies from other deps, how can I make sure my test context doesnt use those but 3.1 instead?

This is what I am trying to run:

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>

Upvotes: 1

Views: 1142

Answers (1)

geoand
geoand

Reputation: 63991

Try excluding spring-aop, spring-beans, spring-context, spring-core, spring-expression, spring-jdbc and spring-tx from the spring security dependencies.

Spring Security 3.1.3 has dependencies to all the aforementioned spring jars for version 3.0.7.RELEASE

More details on how to add exclusions here: How to handle sub projects dependencies in Maven

Upvotes: 2

Related Questions