1

I have a probleme with Fitnesse Integration with Spring MVC .

My use case to test is the authentication :

I have a generic Fixture that load spring Context :

public abstract class GenericSpringFixture extends GenericBelFixture {

protected static AutowireCapableBeanFactory beanFactory;
protected static ClassPathXmlApplicationContext context;

static {
    initSpring();
}

public static void initSpring() {
    if ((context == null) || !context.isActive()) {
        context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-fitnesse-common.xml");
        beanFactory = context.getAutowireCapableBeanFactory();
    }
}

public GenericSpringFixture() {
    beanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
}

public static AutowireCapableBeanFactory returnBeanFactory() {
    return beanFactory;
}
}

The applicationContext-fitnesse-common.xml file is loading all dependencies of My plateform and this include my servlet(Spring Mvc Files) config .

My fixture code is :

public class IdentificationUtilisateur extends GenericSpringFixture{

@Autowired
private WebApplicationContext webApplicationContext;

@Autowired
private FilterChainProxy springSecurityFilterChain;

private MockMvc mockMvc;

public void seLogerAvecIdentifiantEtMotDePasse(String identifiant,
        String motDePasse) {
    this.mockMvc = MockMvcBuilders
            .webAppContextSetup(this.webApplicationContext)
            .addFilters(this.springSecurityFilterChain).build();
}

public void accesReussi() throws Exception{
    mockMvc.perform(
            post("/j_spring_security_check")
                    .param("j_username", "bilal")
                    .param("j_password", "Pa1234567"));
}
}

The problem is that spring is not able to find any webApplicationContext .

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ma.awb.ebk.bel.web.fixture.authentication.IdentificationUtilisateur': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.context.WebApplicationContext ma.awb.ebk.bel.web.fixture.authentication.IdentificationUtilisateur.webApplicationContext; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.context.WebApplicationContext] 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)}

The only difference between My Slim fixture and my test Spring mvc is that in my test MVC , i am on a junit context and i have this annotation enabled on my Junit test :

@WebAppConfiguration

Thanks for your Help .

Nabil
  • 1,771
  • 4
  • 21
  • 33

1 Answers1

0

You create an applicationContext in GenericSpringFixture, but it would not be able to autowire components in IdentificationUtilisateur.

@Autowire works if you annotate your test class with @RunWith(SpringJUnit4ClassRunner.class) but I'm not sure if it works with you FitNess classes.

Maybe you need to inject them manually:

private WebApplicationContext webApplicationContext = GenericSpringFixture.context()

Update1 (not noticed the autowired code snippet): It seems ClassPathXmlApplicationContext is not a WebAppliactionContext. What about setting up an XmlWebApplicationContext instead?

Yugang Zhou
  • 7,123
  • 6
  • 32
  • 60
  • This line in GenericSpringFixture is used to enable annotation ===>beanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); I have access to all my service,Repository , Component Via @Autowired excepting WebApplicationContext – Nabil Jul 22 '13 at 14:27
  • GenericSpringFixture.context suggesion didn't work , i can't convert ClassPathXmlApplicationContext To WebApplicationContext – Nabil Jul 22 '13 at 14:29
  • Sorry, my fault not noticed beanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); – Yugang Zhou Jul 22 '13 at 14:39