• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unit tetsing (Junit) Spring Framework

 
Vidu Greg
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 local.config.properties 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) {
super(name);
}

protected String[] getConfigLocations(){
return new String[] {
"file:///C://web_servers//apache-tomcat-6.0.14//webapps//myproject//WEB-INF//applicationContext.xml",
"file:///C://web_servers//apache-tomcat-6.0.14//webapps//vms//WEB-INF//dataAccessContext.xml"};
}


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

service.saveShipment(shipment);
System.out.println("Shipment Added");
ShipmentSelectionCriteria selectionCriteria = new ShipmentSelectionCriteria();
selectionCriteria.setShipmentIds("999999");
List<ShipmentBeanForListing> shipments = service.getShipments(selectionCriteria);
assertTrue(shipments.contains(shipment));
}

}
--------------------------------------------------------------------------
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">
<list>
<value>WEB-INF/${HOST_NAME}.config.properties</value>
<value>WEB-INF/${HOST_NAME}.jdbc.properties</value>
</list>
</property>
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
</bean>
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34218
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 local.config.properties file.

I don't see any references to local.config.properties in your post. Which code tries to read this file? Is it something Spring uses internally?
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidu,

The complaints is on your locations.properties 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
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(BasicDataSource.java:1165)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:200)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:350)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:261)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy8.saveShipment(Unknown Source)
at vms.service.ShipmentServiceTest.testSaveShipment(ShipmentServiceTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:166)
at junit.framework.TestCase.runBare(TestCase.java:140)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:131)
at junit.framework.TestSuite.runTest(TestSuite.java:173)
at junit.framework.TestSuite.run(TestSuite.java:168)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


I have all the jars in my library.

Any guess why I am having this.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic