如何在testNG中注入控制器JSF作用域视图

时间:2017-02-06 12:12:22

标签: spring jsf testing scope view-scope

尝试在我的测试中注入一个控制器,调用用户

@Component("user")
@Scope("view")
public class UserController

项目使用:

  • Spring 4.2.4
  • JSF 2.2
  • Hibernate 5
  • Primefaces 5.3
  • JUnit
  • TestNG的
  • 当ready方法getViewMap()我的类 SysViewScope

当在测试中注入控制器时,会收到一个异常null点,但是当在tomcat中部署系统时,没有发生这种错误。

    public class SysViewScope implements Scope{

    public static final String VIEW_SCOPE_CALLBACKS = "viewScope.callbacks";

    public synchronized Object get(String name, ObjectFactory<?> objectFactory){
        Object instance = this.getViewMap().get(name);
        if (instance == null){
            instance = objectFactory.getObject();
            this.getViewMap().put(name, instance);
        }
        return instance;
    }


    public Object remove(String name){
        Object instance = getViewMap().remove(name);
        if (instance != null){
            Map<String, Runnable> callbacks = (Map<String, Runnable>) getViewMap().
            get(VIEW_SCOPE_CALLBACKS);
            if (callbacks != null){
                callbacks.remove(name);
            }
        }
        return instance;
    }


    public void registerDestructionCallback(String name, Runnable runnable)
    {
        Map<String, Runnable> callbacks = (Map<String, Runnable>) getViewMap().
        get(VIEW_SCOPE_CALLBACKS);
        if (callbacks != null){
            callbacks.put(name, runnable);
        }
    }

    public Object resolveContextualObject(String name){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesRequestAttributes facesRequestAttributes = new FacesRequestAttributes(facesContext);
        return facesRequestAttributes.resolveReference(name);
    }

    public String getConversationId(){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesRequestAttributes facesRequestAttributes = new FacesRequestAttributes(facesContext);
        return facesRequestAttributes.getSessionId() + "-" + facesContext.getViewRoot().getViewId();
    }

    private Map<String, Object> getViewMap(){
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap();
    }
}

参见我的班级考试

@Scope("singleton")
@Configuration("webappTest")
@ComponentScan
@WebAppConfiguration
@EnableTransactionManagement
@ContextConfiguration(classes = SysSampleConfig.class)
public class NotConfirmedEmailTest extends AbstractTestNGSpringContextTests
{

    @Autowired
    private WebApplicationContext   webApplicationContext;

    @Autowired
    @Qualifier("chromeDriver")
    private WebDriver                   driver;

    @Qualifier("user")
    @Autowired
    private UserController      userController;

    @PostConstruct
    public void init()
    {
        LOG.info("init ..." + webApplicationContext.toString());
    }

请参阅stacktrace


ERROR - 11:04:26 093  - org.springframework.test.context.TestContextManager.prepareTestInstance(231) - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@549d1965] to prepare test instance [com.sys.mod.email.SysNotConfirmedEmailTest@1e0f9063] 
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.sys.mod.email.SysNotConfirmedEmailTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sys.mod.person.controllers.SysClientCaptureController com.sys.mod.email.SysNotConfirmedEmailTest.clientCaptureController; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) ~[spring-test-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) ~[spring-test-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228) ~[spring-test-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:149) [spring-test-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_73]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_73]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_73]
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104) [testng-6.10.jar:na]
        at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515) [testng-6.10.jar:na]
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217) [testng-6.10.jar:na]
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144) [testng-6.10.jar:na]
        at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:169) [testng-6.10.jar:na]
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) [testng-6.10.jar:na]
        at org.testng.TestRunner.privateRun(TestRunner.java:756) [testng-6.10.jar:na]
        at org.testng.TestRunner.run(TestRunner.java:610) [testng-6.10.jar:na]
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:387) [testng-6.10.jar:na]
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382) [testng-6.10.jar:na]
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) [testng-6.10.jar:na]
        at org.testng.SuiteRunner.run(SuiteRunner.java:289) [testng-6.10.jar:na]
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) [testng-6.10.jar:na]
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) [testng-6.10.jar:na]
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293) [testng-6.10.jar:na]
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1218) [testng-6.10.jar:na]
        at org.testng.TestNG.runSuites(TestNG.java:1133) [testng-6.10.jar:na]
        at org.testng.TestNG.run(TestNG.java:1104) [testng-6.10.jar:na]
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) [testng-remote.jar:na]
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152) [testng-remote.jar:na]
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57) [testng-remote.jar:na]
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sys.mod.person.controllers.SysClientCaptureController com.sys.mod.email.SysNotConfirmedEmailTest.clientCaptureController; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        ... 31 common frames omitted
    Caused by: java.lang.NullPointerException: null
        at com.sys.util.SysViewScope.getViewMap(SysViewScope.java:78) ~[classes/:na]
        at com.sys.util.SysViewScope.get(SysViewScope.java:24) ~[classes/:na]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        ... 33 common frames omitted
    FAILED CONFIGURATION: @BeforeClass springTestContextPrepareTestInstance
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.sys.mod.email.SysNotConfirmedEmailTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sys.mod.person.controllers.SysClientCaptureController com.sys.mod.email.SysNotConfirmedEmailTest.clientCaptureController; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
        at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:149)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
        at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144)
        at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:169)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
        at org.testng.TestRunner.privateRun(TestRunner.java:756)
        at org.testng.TestRunner.run(TestRunner.java:610)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
        at org.testng.SuiteRunner.run(SuiteRunner.java:289)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
        at org.testng.TestNG.runSuites(TestNG.java:1133)
        at org.testng.TestNG.run(TestNG.java:1104)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sys.mod.person.controllers.SysClientCaptureController com.sys.mod.email.SysNotConfirmedEmailTest.clientCaptureController; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
        ... 31 more
    Caused by: java.lang.NullPointerException
        at com.sys.util.SysViewScope.getViewMap(SysViewScope.java:78)
        at com.sys.util.SysViewScope.get(SysViewScope.java:24)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
        ... 33 more

我为解决这个问题做了什么,FacesContext.getCurrentInstance(),不返回更多null?

0 个答案:

没有答案