Spring junit test not working

user3388744

I've got a Spring (4.2.5) project with Java configuration classes. These work when running the project normally, but I can't get my tests to work. In the test below the repo is null.

@ContextConfiguration(classes = { HibernateConfiguration.class })
public class TestTest {

    @Autowired
    private InstituteRepository repo;

    //some test
}

If I add the @RunWith(SpringJUnit4ClassRunner.class) annotation i get an exception

java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotationAttributes(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/String;ZZ)Lorg/springframework/core/annotation/AnnotationAttributes;
    at org.springframework.test.util.MetaAnnotationUtils$AnnotationDescriptor.<init>(MetaAnnotationUtils.java:290)
    at org.springframework.test.util.MetaAnnotationUtils$UntypedAnnotationDescriptor.<init>(MetaAnnotationUtils.java:365)
    at org.springframework.test.util.MetaAnnotationUtils$UntypedAnnotationDescriptor.<init>(MetaAnnotationUtils.java:360)
    at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:191)
    at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:166)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:274)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:110)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:154)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:145)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Any help is appreciated.

[EDIT]

If I change the springframework dependecy version to 4.1.3, the test works. Do I need different configuration for 4.2.5? The HibernateConfiguration is the following

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = { "myapp.services" })
@EnableJpaRepositories("myapp.repositories")
@Import({ HSQLConfiguration.class, MySQLConfiguration.class })
public class HibernateConfiguration {
}

Where the DataSource and Properties are defined in both HSQLConfiguration and MySQLConfiguration, which is selected based on active profile.

[EDIT 2]

After more googling I found this issue, and after checking my dependencies it appeared I had spring-core-4.1.9 under the maven dependencies and I hadn't included a dependency it in my POM. After adding the dependency I see the jar under the maven dependencies, but I get a build-path error:

Project 'myapp' is missing required library: 'C:\Users\user\.m2\repository\org\springframework\spring-core\4.1.9.RELEASE\spring-core-4.1.9.RELEASE.jar' 

This is my POM file

<dependencies>
    <!-- spring data -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${springframework.data.version}</version>
    </dependency>
    <!-- spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <!-- annotations -->
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>jsr250-api</artifactId>
        <version>1.0</version>
    </dependency>
    <!-- spring dep -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <!-- mysql -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.connector.version}</version>
    </dependency>
    <!-- hsql -->
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>${hsqldb.version}</version>
    </dependency>
    <!-- test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <!-- I added this dependency -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
</dependencies>

With the following properties

<properties>
    <project.version>0.1</project.version> 
    <junit.version>4.12</junit.version>
    <springframework.version>4.2.5.RELEASE</springframework.version>
    <springframework.data.version>1.10.1.RELEASE</springframework.data.version>
    <hibernate.version>5.1.0.Final</hibernate.version>
    <mysql.connector.version>5.1.31</mysql.connector.version>
    <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
    <hsqldb.version>2.3.2</hsqldb.version>
</properties>

[EDIT 3]

The build path problem seems to be Eclipse related, running from command line the test works!

ilopezluna

If the problem is only on eclipse, you will fix it doing: Project->Clean right click on project: Maven->Update dependencies

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Persist/commit not working in test environment with Spring JPA JUnit

From Dev

Simple JUnit test not working

From Dev

JUnit for Spring WebServiceTemplate not working

From Dev

JUnit test not working correctly with Timer

From Dev

Value annotation not working in Junit test

From Dev

JUnit test not working correctly with Timer

From Dev

How to test ConfigurationProperties in Spring with JUnit?

From Dev

Spring JUnit test case failed

From Dev

Multiple Spring datasources for JUnit test

From Dev

Spring data repository junit Test

From Dev

Spring Test Properties with JUnit 5

From Dev

Spring JUnit test case failed

From Dev

Spring JUnit Test - Instantiation problems

From Dev

Spring Junit Controller Mock Not Working

From Dev

Spring Junit Controller Mock Not Working

From Dev

Spring xml + JUnit - xml context file not working in src/test/resources but works in src/main/resources

From Dev

Test a Spring Controller with JUnit that depends of Spring Security

From Dev

ClassNotFoundException on Spring JavaMailSenderImpl in JUnit test with Spring Boot

From Java

ActiveProfile is not working in Junit5 test

From Dev

Parametrized JUnit+JUnitParams test is not working for arrays with

From Dev

Android PointF constructor not working in JUnit test

From Dev

ActiveProfile is not working in Junit5 test

From Dev

Why this simple Junit 5 test is not working

From Dev

How to run Spring Shell scripts in a JUnit test

From Java

How to make Junit test in spring boot

From Dev

Spring JUnit test not properly reusing ApplicationContext

From Dev

Spring MVC application Junit test case failing

From Java

spring-boot-starter-test with JUnit 5

From Dev

JUnit test web.xml with Spring MVC

Related Related

HotTag

Archive