lundi 6 février 2017

How to inject controller JSF scope view in testNG

Try to inject one controler in my test, call user

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

Project use:

  • Spring 4.2.4
  • JSF 2.2
  • Hibernate 5
  • Primefaces 5.3
  • JUnit
  • testNG

When inject controller in test, receive a exception null point, but when deploy system in tomcat, not happened this wrong.

See my class test

@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());
    }

See 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

What i'm doing for solve this problem, FacesContext.getCurrentInstance(), not return more null?

Aucun commentaire:

Enregistrer un commentaire