Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get JUnit4 annotations to work in Spring2.5?

I've been trying to create a JUnit test for my DAO that uses Hibernate and is linked to the database via Spring in my applicationContext.xml file.

The error only occurs when adding:

@RunWith(SpringJUnit4ClassRunner.class)

Otherwise I get a null pointer exception to the partDao, which is expected because it isn't linking.

Normally I'd just say scrap it and continue developing without JUnit tests, but I would prefer to start using it in my projects.

Here is the error:

Testsuite: SpecBuilder.webapp.dao.PartDaoTest
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.runner.notification.RunNotifier.testAborted(Lorg/junit/runner/Description;Ljava/lang/Throwable;)V
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:146)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:52)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:50)
    at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:515)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1031)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:888)
Test SpecBuilder.webapp.dao.PartDaoTest FAILED (crashed)
/home/zclark/Projects/SpecBuilder/nbproject/build-impl.xml:798: Some tests failed; see details above.
BUILD FAILED (total time: 2 seconds)

Here is my test class:

package SpecBuilder.webapp.dao;

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="{applicationContext.xml}")
public class PartDaoTest {

    @Autowired
    PartDao partDao;

    public PartDaoTest() {}

    @Test
    public void autowireTest() {
        assertNotNull(partDao);
    }
}

And here is my applicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/specbuilder.properties"/>

    <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}" />

    <bean id="partDao" class="SpecBuilder.webapp.dao.PartDao">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="annotatedClasses">
            <list>
                <value>SpecBuilder.webapp.model.Part</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            </props>
        </property>
    </bean>

</beans>

Any ideas?

like image 473
zclark Avatar asked Jan 28 '11 00:01

zclark


People also ask

Is JUnit 5 backwards compatible with JUnit 4?

Please note that JUnit 5 is not backward compatible with JUnit 4, but the JUnit team created the JUnit Vintage Project to support JUnit 4 test cases on top of JUnit 5.

What is the difference between junit4 and JUnit 5?

Only one test runner can execute tests at a time in JUnit 4 (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously. JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of the Java 8 features.

How do I switch from junit4 to junit5?

Let's see what we need to do to run existing test on the new platform. In order to run both JUnit 4 and JUnit 5 tests we need: JUnit Jupiter to write and run JUnit 5 tests. Vintage test engine to run JUnit 4 tests.

What are JUnit annotations?

The JUnit annotations are predefined texts provided by Java API, which helps JVM to recognize the type of method or class for testing. In other words, they are used to specify or declare the methods or classes with few properties like testing, disabling tests, ignoring tests, etc.


1 Answers

It looks like you need JUnit 4.3 or 4.4 to work with Spring 2.5.

like image 93
axtavt Avatar answered Oct 27 '22 01:10

axtavt