Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring的Junit测试中的Bean创建异常_Java_Spring_Spring Mvc_Junit4_Autowired - Fatal编程技术网

Java 使用Spring的Junit测试中的Bean创建异常

Java 使用Spring的Junit测试中的Bean创建异常,java,spring,spring-mvc,junit4,autowired,Java,Spring,Spring Mvc,Junit4,Autowired,在Junit中运行测试时,我遇到了BeanCreation异常。看来春天来了 尝试自动关联字段时出现问题 测试类别为:- @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath*:servlet-context.xml"}) @ComponentScan("com.org.devcenter.processtutorials") public cla

在Junit中运行测试时,我遇到了BeanCreation异常。看来春天来了 尝试自动关联字段时出现问题

测试类别为:-

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath*:servlet-context.xml"})
    @ComponentScan("com.org.devcenter.processtutorials")
    public class LdapDirectoryContextSetupTest {
    @Autowired
    LdapDirectoryContext ldapSetup;

    private final static String securityPrincipal = "xxx"; 
    private final static String initialContextfactory = "xxx";
    private final static String providerUrl = "xxx";
    private final static String securityAuthentication = "xxx";

    @Test
    public void testEnvironmentProperties(){

        assertEquals(securityPrincipal, ldapSetup.createLdapEnvironmentProperties().get(Context.SECURITY_PRINCIPAL));
        assertEquals(providerUrl, ldapSetup.createLdapEnvironmentProperties().get(Context.PROVIDER_URL));
        assertEquals(securityAuthentication, ldapSetup.createLdapEnvironmentProperties().get(Context.SECURITY_AUTHENTICATION));
        assertEquals(initialContextfactory, ldapSetup.createLdapEnvironmentProperties().get(Context.INITIAL_CONTEXT_FACTORY));       
    }
}
我尝试测试的类如下所示:-

    @Component
    public class LdapDirectoryContext {

    @Value("${security_Principal}")
    private String securityPrincipal;
    @Value("${intital_context_Factory}")    
    private String initialContextfactory;

    @Value("${provider_Url}")
    private String providerUrl;
    @Value("${security_Authentication}")
    private String securityAuthentication;

    /**
     * This method sets up and returns the ldapConnectionProperties hashtable. 
     * @return ldapConnectionProperties Hashtable.
     */
    public Hashtable<String,String> createLdapEnvironmentProperties(){

        Hashtable<String,String> ldapEnvironmentProperties = new  Hashtable<String,String>();
        ldapEnvironmentProperties.put(Context.PROVIDER_URL, providerUrl);
        ldapEnvironmentProperties.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
        ldapEnvironmentProperties.put(Context.SECURITY_AUTHENTICATION, securityAuthentication);
        ldapEnvironmentProperties.put(Context.INITIAL_CONTEXT_FACTORY, initialContextfactory);  

        return ldapEnvironmentProperties;
    }

    /**
     * This method creates a DirContext(directory context) object based on the ldapConnectionProperties Hashtable passed.
     * @param ldapConnectionProperties Hashtable passed.
     * @return DirContext object.
     * @throws NamingException if the context object could not be created due to invalid Ldap properties supplied
     * or when encountering issues interacting with the Ldap Server. 
     */
    public DirContext getDirContext(Hashtable<String, String> ldapConnectionProperties ) throws NamingException{
        return new InitialDirContext(ldapConnectionProperties);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc     
http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing 
infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.org.devcenter.processtutorials" />
<context:property-placeholder location="classpath*:*.properties" />

</beans:beans>

尝试将ldapSetup重命名为ldapdirectorycontext重命名无效。
INFO : org.springframework.test.context.TestContextManager - Could not instantiateTestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@5cf99513: startup date [Mon Oct 20 13:44:29 CDT 2014]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3d36472f: defining beans  [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing  TestExecutionListener  [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@64c53235] to  prepare test instance  [com.xxx.devcenter.processtutorials.ldapAuthentication.LdapDirectoryContextSetupTest@4e636942]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.xxx.devcenter.processtutorials.ldapAuthentication.LdapDirectoryContextSetupTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xxx.devcenter.processtutorials.ldapAuthentication.LdapDirectoryContext com.xxx.devcenter.processtutorials.ldapAuthentication.LdapDirectoryContextSetupTest.ldapSetup; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xxx.devcenter.processtutorials.ldapAuthentication.LdapDirectoryContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.  Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
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.factory.BeanCreationException: Could not autowire field:   com.xxx.devcenter.processtutorials.ldapAuthentication.LdapDirectoryContext  com.xxx.devcenter.processtutorials.ldapAuthentication.LdapDirectoryContextSetupTest.ldapSetup;  nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xxx.devcenter.processtutorials.ldapAuthentication.LdapDirectoryContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.  Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:518)
at  org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xxx.devcenter.processtutorials.ldapAuthentication.LdapDirectoryContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:997)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:867)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:490)
... 28 more
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@5cf99513: startup date [Mon Oct 20 13:44:29 CDT 2014]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3d36472f: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy