I have a working Spring application compiled with maven and deployed on tomcat 7.

I'm trying to implements some test cases using JUnit 4. I successfully make some test for a part of the application but I get compilation problem when I want to test two parts which used external framework: Shiro and OIOSAML. Since there is no OIOSAML community here, I'll focus on shiro but I believe that the cause of the problem is the same.

When I try to load the application-context in the JUnit test, I obtains loading errors (see next). It looks like some dependencies can't be loaded/found in the frameworks part. Here is some helpful information:

Shiro exception:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'authorization' defined in class path resource [test-context.xml]: 
Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Could not instantiate bean class [myPackage.ShiroAuthorization]: 
Constructor threw exception; nested exception is org.apache.shiro.config.ConfigurationException: 
Unable to instantiate class [com.mysql.jdbc.jdbc2.optional.MysqlDataSource] for object named 'ds'.  
Please ensure you've specified the fully qualified class name correctly.
            at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
            at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
            at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
            at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
            at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
            at myPackage.Test.init(Test.java:30)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
            at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
            at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
            at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
            at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
            at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
            at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
        Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [myPackage.ShiroAuthorization]: Constructor threw exception; nested exception is org.apache.shiro.config.ConfigurationException: Unable to instantiate class [com.mysql.jdbc.jdbc2.optional.MysqlDataSource] for object named 'ds'.  Please ensure you've specified the fully qualified class name correctly.
            at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
            at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:110)
            at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
            ... 29 more
        Caused by: org.apache.shiro.config.ConfigurationException: Unable to instantiate class [com.mysql.jdbc.jdbc2.optional.MysqlDataSource] for object named 'ds'.  Please ensure you've specified the fully qualified class name correctly.
            at org.apache.shiro.config.ReflectionBuilder.createNewInstance(ReflectionBuilder.java:151)
            at org.apache.shiro.config.ReflectionBuilder.buildObjects(ReflectionBuilder.java:119)
            at org.apache.shiro.config.IniSecurityManagerFactory.buildInstances(IniSecurityManagerFactory.java:161)
            at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:124)
            at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:102)
            at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:88)
            at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:46)
            at org.apache.shiro.config.IniFactorySupport.createInstance(IniFactorySupport.java:123)
            at org.apache.shiro.util.AbstractFactory.getInstance(AbstractFactory.java:47)
            at myPackage.ShiroAuthorization.<init>(ShiroAuthorization.java:31)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
            at java.lang.reflect.Constructor.newInstance(Unknown Source)
            at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
            ... 31 more
        Caused by: org.apache.shiro.util.UnknownClassException: Unable to load class named [com.mysql.jdbc.jdbc2.optional.MysqlDataSource] from the thread context, current, or system/application ClassLoaders.  All heuristics have been exhausted.  Class could not be found.
            at org.apache.shiro.util.ClassUtils.forName(ClassUtils.java:148)
            at org.apache.shiro.util.ClassUtils.newInstance(ClassUtils.java:164)
            at org.apache.shiro.config.ReflectionBuilder.createNewInstance(ReflectionBuilder.java:144)
            ... 45 more

Shiro.ini

...
ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
ds.url = jdbc:mysql://*******
...
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled=true
jdbcRealm.dataSource = $ds
...
securityManager.realms = $jdbcRealm,
...

OIOSAML exception:

...Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/opensaml/xml/ConfigurationException...

Test Case

...
public static ApplicationContext context;
@BeforeClass
public static void  init() throws Exception {
    context = new ClassPathXmlApplicationContext("test-context.xml");
}

I tried to play with the context file but nothing helps. Which is strange is I can successfully test the connection pool of tomcat 7 which shows that dependencies are correctly loaded.

I remind you that everything works except when I want to test it with JUnit.

有帮助吗?

解决方案

Do you have mySql driver in your classpath? Probably that's the problem.

其他提示

Try to annotate your JUnit test class using:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:/test-context.xml"
})

This loads your applicationContext when running your JUnit tests.

Make sure your applicationContext xml is on your classpath.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top