运行Spring Security和Struts JUnit串联测试

时间:2014-03-25 19:58:13

标签: java spring struts2 junit spring-security

目前,在我的网络应用程序中,我试图为Struts 2.0.6和Spring 3运行一系列单元测试。这样做,我遇到了一个问题,我有两个测试在各自的测试中分别调用上下文配置路径,并抛出异常,说某些bean已经连线。

对于我的Struts单元测试,我已经扩展了BaseStrutsTestCase,可在此处找到: http://depressedprogrammer.wordpress.com/2007/06/18/unit-testing-struts-2-actions-spring-junit

我为struts动作测试配置了上下文配置路径:

private static final String CONFIG_LOCATIONS = "classpath:applicationContext-test.xml";

然后我开始为Spring Security的登录功能创建一个单独的Spring JUnit测试:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:applicationContext-test.xml"})
public class UserAuthenticationTest {

    @Autowired
    protected UserManagementService userManagementService;

    @Test
    public void testUserSuccess() {

        ArrayList<GrantedAuthority> grantedAuthorities = 
            new ArrayList<GrantedAuthority>();

        ConsoleUser consoleUser = 
            userManagementService.getConsoleUser("user@test.com","password");

       if (consoleUser == null){
            Assert.assertTrue(consoleUser == null);
       } else {
            for (ConsoleRole role : consoleUser.getRoles()){
                StringBuilder permissions = new StringBuilder();
                    for (ConsolePermission permission : role.getPermissions()){
                        grantedAuthorities.add(new GrantedAuthorityImpl(permission.getPermissionType().toString()));
                        permissions.append(permission.getPermissionType().toString() + " ");
                    }
            }

            Authentication authentication = new UsernamePasswordAuthenticationToken(consoleUser.getEmail(), consoleUser.getPassword(), grantedAuthorities);

            SecurityContextHolder.getContext().setAuthentication(authentication);
            Assert.assertTrue(true);
        }

    }
}

所以我运行了我的单元测试套件(1个struts动作和1个Spring测试),并在我的日志中抛出以下异常:

2974 [main] ERROR org.springframework.web.context.ContextLoader  - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource' defined in class path resource [spring-messagesource-config.xml]: Cannot resolve reference to bean 'messageBundleLocation' while setting bean property 'basename'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageBundleLocation' defined in class path resource [spring-messagesource-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.config.internalEhCacheCachingAdvisor': Cannot resolve reference to bean 'com.googlecode.ehcache.annotations.impl.CacheStaticMethodMatcherPointcut#0' while setting bean property 'pointcut'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.impl.CacheStaticMethodMatcherPointcut#0': Cannot resolve reference to bean 'com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl#0' while setting bean property 'cacheAttributeSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl#0': Cannot resolve reference to bean 'facadeCacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'facadeCacheManager' defined in class path resource [spring-cache-config-test.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Another CacheManager with same name 'API_CACHE_MANAGER' already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@327df0b7]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1387)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.AbstractApplicationContext.initMessageSource(AbstractApplicationContext.java:786)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
    at BaseStrutsTestCase.setUp(BaseStrutsTestCase.java:80)
    at junit.framework.TestCase.runBare(TestCase.java:139)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
    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:597)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)

有没有办法一个接一个地运行这些测试,要么通过持久化我的Spring单元测试已经创建的bean,要么在Spring单元测试运行完毕后销毁上下文配置,然后让Struts使用BaseStrutsTestCase中的上下文配置?

0 个答案:

没有答案