Olá,
Esse é meu código
/*...*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext*.xml")
public class AtribuicaoFuncaoBeanTest {
@Autowired
@Qualifier("atribuicaoFuncaoBean")
private AtribuicaoFuncaoBean atribuicaoFuncaoBean;
private CoordenacaoCurso funcaoTmp;
private TipoCoordenacaoCurso tipoFuncao;
@Before
public void setUpBefore() {
this.funcaoTmp = new CoordenacaoCurso();
this.tipoFuncao = new TipoCoordenacaoCurso();
}
@Test
public void grupoCursoEstaHabilitadoSeFuncaoForCoordenadorDeCurso() {
tipoFuncao.setId(TipoCoordenacaoCurso.TIPO_COORDENADOR_DE_CURSO);
funcaoTmp.setTipoCoordenacaoCurso(tipoFuncao);
atribuicaoFuncaoBean.setFuncaoTmp(funcaoTmp);
assertTrue(atribuicaoFuncaoBean.isGrupoCursoHabilitado());
}
}
Estou tentando fazer testes do meu Bean, mas dá erro porque o spring não consegue injetar as dependências. Abaixo coloco um resume do erro:
29/08/2017 16:27:26 ERROR - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@401516ad] to prepare test instance [br.ufsc.setic.sistema.service.config.AtribuicaoFuncaoBeanTest@520d0137]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'br.ufsc.setic.sistema.service.config.AtribuicaoFuncaoBeanTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private br.ufsc.setic.sistema.controller.config.AtribuicaoFuncaoBean br.ufsc.setic.sistema.service.config.AtribuicaoFuncaoBeanTest.atribuicaoFuncaoBean; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private br.ufsc.setic.sistema.controller.config.AtribuicaoFuncaoBean br.ufsc.setic.sistema.service.config.AtribuicaoFuncaoBeanTest.atribuicaoFuncaoBean; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:522)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
... 26 more
Caused by: java.lang.NullPointerException
at br.ufsc.setic.comum.util.jsf.ViewScope.get(ViewScope.java:14)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:336)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1021)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:964)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494)
Pelo que entendi, esse erro ocorre porque o FacesContext.getCurrentInstance() retorna nulo no código abaixo:
public class ViewScope implements Scope {
public Object get(String name, ObjectFactory objectFactory) {
FacesContext context = FacesContext.getCurrentInstance();
Map<String,Object> viewMap = context.getViewRoot().getViewMap();
Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
/*...*/
Se puderem ajudar fico muito agradecido.