File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Testing and the fly likes Unit tetsing (Junit) Spring Framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Unit tetsing (Junit) Spring Framework" Watch "Unit tetsing (Junit) Spring Framework" New topic

Unit tetsing (Junit) Spring Framework

Vidu Greg

Joined: Sep 05, 2005
Posts: 10
Hi I am very new to unit testing. I am using Spring framework + struts 2.0
I am trying to test a serice. I have two Context files. When I run this class as a junit test, it gave me an error like could not find file. ApplicationContext.xml file has property configurer.

I like to know if I am running in a proper way or should I create a build file so that applicationContext can find all the properties file or how should I load the properties file.

Here is what I am trying to do--

package myproject.service;
import java.util.List;

import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

import myproject.model.Shipment;
import myproject.model.ShipmentBeanForListing;
import myproject.model.ShipmentSelectionCriteria;

public class ShipmentServiceTest extends
AbstractDependencyInjectionSpringContextTests {

public ShipmentServiceTest(String name) {

protected String[] getConfigLocations(){
return new String[] {

public void testSaveShipment() throws Exception {
ShipmentService service = (ShipmentService) applicationContext.getBean("shipmentService");
Shipment shipment = new Shipment();
shipment.setShipmentId(new Integer(999999));

System.out.println("Shipment Added");
ShipmentSelectionCriteria selectionCriteria = new ShipmentSelectionCriteria();
List<ShipmentBeanForListing> shipments = service.getShipments(selectionCriteria);

Part of applicationContext.xml file
<!-- Configurer that replaces ${...} placeholders with values from properties files -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<property name="systemPropertiesModeName">
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32844

Originally posted by Vidu Greg:
Hi I am very new to unit testing. I am using Spring framework + struts 2.0
I am trying to test a serice.

First of all, note that you are writing an integration test, not a unit test. A unit test does not access the database/web app/etc. This distinction is important when describing the problem to others.

it gave me an error like could not find file.

I don't see any references to in your post. Which code tries to read this file? Is it something Spring uses internally?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210


The complaints is on your file can't be found. By default Spring's PropertyConfigurator will search the properties file from your classpath. So very likely you do not have your classpath set correctly when running the test.

Especially looking at where define the location of your Spring config files, which were located in 2 different tomcat's deployment area, you sure have unconventional classpath
Vidu Greg

Joined: Sep 05, 2005
Posts: 10
Thanks for the advice.

I am encoutering a different problem now. While running a junit test (In Eclipse right click runas junit test), I am getting this error

java.lang.NoSuchMethodError: org.apache.commons.pool.impl.GenericObjectPool: method <init>()V not found
at org.apache.commons.dbcp.BasicDataSource.createDataSource(
at org.apache.commons.dbcp.BasicDataSource.getConnection(
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(
at $Proxy8.saveShipment(Unknown Source)
at vms.service.ShipmentServiceTest.testSaveShipment(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at junit.framework.TestCase.runTest(
at junit.framework.TestCase.runBare(
at org.springframework.test.ConditionalTestCase.runBare(
at junit.framework.TestResult$1.protect(
at junit.framework.TestResult.runProtected(
at junit.framework.TestSuite.runTest(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(

I have all the jars in my library.

Any guess why I am having this.

I agree. Here's the link:
subject: Unit tetsing (Junit) Spring Framework
It's not a secret anymore!