0

I get the following error when I try to unit test my Spring Hibernate DAO classes. This error only comes when I try to deploy the app on tomcat, not when executing the unit tests. When I comment out the test class and test dependencies no error comes.

...Caused by: java.lang.ClassNotFoundException: org.springframework.core.io.Resource...

My Maven dependencies for Spring DAO test as follows: (I am using Spring version 4.0.5.RELEASE)

 <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.7</version>
                <scope>test</scope>
            </dependency>

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

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

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

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

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

My unit test class as follows:

   @TransactionConfiguration(defaultRollback = true)
@ContextConfiguration({ "classpath:spring/*.xml" })
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
public class EmployerDaoHbnTest {

    @Inject
    EmployerDao employerDao;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testRegisterEmployer() {
        assertTrue(employerDao.findEmployer(5) != null);

    }

    @Test
    public void testToString() {
        fail("Not yet implemented");
    }

}

What dependency am I missing? Where can I officially (such as Spring docs) find that exactly what are the dependencies for Spring DAO test?

siva636
  • 16,109
  • 23
  • 97
  • 135
  • Can you show the annotation of your unitTest class?. – paul Jul 06 '14 at 12:12
  • @paul Question has been updated. – siva636 Jul 06 '14 at 12:45
  • Why do all your dependencies have the scope `test`? Except for spring-test, you need them at compile-time and at runtime. So their scope should be compile. – JB Nizet Jul 06 '14 at 12:55
  • I made the scope as compile except spring-test, still I get the same error. Initially I got the dependencies from here: http://stackoverflow.com/questions/10008374/cannot-junit-test-using-spring – siva636 Jul 06 '14 at 13:09

1 Answers1

0

Try to changes with this annotations

      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration({"classpath:/applicationContext.xml"})
      @WebAppConfiguration

I believe part of you problem is the transactional

paul
  • 12,873
  • 23
  • 91
  • 153