Juan Paredes
Juan Paredes

Reputation: 799

How to load Spring's application context with a Fitnesse test fixture?

I have this test fixture I want to run with Fitnesse and it involves using Spring. I haven't been able to load the spring application context with the ClasspathXmlApplicationContext, and I'm sure is a classpath configuration thing that I just haven't figured it out.

So here's my setup.

Project contents

As you can see, the fitnesse.jar is inside the project, so I can run the fitnesse server and the tests anywhere I have a working copy of the project (all the wiki files are inside the FitNesseRoot folder). The bin is the output folder of the project (where all the .class are).

Now, the code:

InventarioQueryTest (wiki page from Fitnesse, notice the !path declarations)

!define TEST_SYSTEM {slim}

!path ../bin
!path ../web/WEB-INF/lib/**.jar

|import                                |
|com.softclear.inventario.test.fitnesse|


|Query:listar status          |
|id|nombre|entidad|descripcion|

ListarStatus.java (the text fixture invoked by Fitnesse)

public class ListarStatus {

    private ServicioStatus serv;
    private ClassPathXmlApplicationContext ctx;

    //fitnesse calls the constructor
    public ListarStatus(){
        ctx = new ClassPathXmlApplicationContext(new String[] {
                "applicationContext-ListarStatus.xml",
                "applicationContext-dao.xml",
                "applicationContext-hibernate.xml"});

            //performs dependency injection of DAO and HibernateSession
        serv = (ServicioStatus) ctx.getBean("servicioStatus");
    }

    //and the query method is the test
    public List<Object> query() {
        List<Status> lista = serv.listarStatus();
        QueryResultBuilder builder = new QueryResultBuilder(Status.class);
        QueryResult result = builder.build(lista.iterator());
        return result.render();
    }
}

I run fitnesse with java -jar from the working copy of my project. And when I run the test, this is the command that fitnesse executes:

java -cp fitnesse.jar;../bin;C:\Dev\WS\softclear\SistemaInventario\fitnesse\..\web\WEB-INF\lib\ajax\AjaxFileUpload-0.03.jar;%the.rest.of.the.web-inf/lib.jars...%  fitnesse.slim.SlimService 8086

And all that produces this error in the test:

java.io.FileNotFoundException: class path resource [applicationContext-ListarStatus.xml] cannot be opened because it does not exist

As fas as I understand, the !path ../bin in the wiki should indicate all the subfolders and stuff to be included in the classpath (the java -cp call), but apparently it doesn't...

Any ideas? Thanks a lot for your time!

Upvotes: 2

Views: 4133

Answers (1)

Juan Paredes
Juan Paredes

Reputation: 799

Ok so I resolved it, by doing this in the ListarStatus class:

public ListarStatus() {
        ctx = new ClassPathXmlApplicationContext(new String[] {
                "/com/softclear/inventario/test/fitnesse/applicationContext-ListarStatus.xml",
                "applicationContext-dao.xml",
                "applicationContext-hibernate.xml"});

        serv = (ServicioStatus) ctx.getBean("servicioStatus");
    }

Notice that the applicationContext-ListarStatus.xml file has a relative path and not just the name of the file.

Turns out that ClassPathXmlApplicationContext DOES NOT search anywhere in the classpath like I understood (read this, the part of the ClassPathXML...: http://freejavaclass.com/articles/j2ee/spring/spring_loading_types_ways.jsp). At least not in Spring 2.

It had nothing to do with Fitnesse, it was all Spring...

Upvotes: 2

Related Questions