Spring MVC Test In Fitnesse

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 .


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?

链接地址: http://www.djcxy.com/p/67726.html

上一篇: 测试时如何在控制器中自动装入Spring bean?

下一篇: 在Fitnesse进行Spring MVC测试