Enable/Disable Modules for Deploy/Functional Tests

View: New views
4 Messages — Rating Filter:   Alert me  

Enable/Disable Modules for Deploy/Functional Tests

by Steve Mosley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everybody,



We are using the Jemmy/Jelly functional testing libraries with our app and they are working very well (just a couple of minor issues we have needed to work around.)



Most of our application uses remote services to provide real interactions. What we are trying to do is mock these out for functional tests.



We have created two modules, both of which implement our remote service interface. And we can look these up using Lookup.getDefault().lookup(...); ....



The problem we are trying to solve is figuring out how to enable the mock implementation for functional tests and disable the real one ... and the reverse for the real deployment, include the real version and not the mock. At the moment the only way we can switch is to remove them completely from the project.



I thought we might be able to do something with the enableModules part of this



        NbModuleSuite.Configuration testConfig = NbModuleSuite.createConfiguration(classUnderTest);

        testConfig.clusters(".*").enableModules(".*");

        testConfig.gui(true);

        return NbModuleSuite.create(testConfig);



.. but haven't been able to get anything working (and that code we just copied from the wiki)



... the other option was maybe passing a system param to the functional tests that the mock module can look for and decide weather to enable itself ... but that still means the mock would be deployed to the users.



We are using NB 6.7 and the default ANT scripts for building our project.



Anyone else tried something like this before / knows how we could do this?



Cheers

Steve





Re: Enable/Disable Modules for Deploy/Functional Tests

by Jesse Glick :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

smozely wrote:
> I thought we might be able to do something with the enableModules part of this
>
>         NbModuleSuite.Configuration testConfig = NbModuleSuite.createConfiguration(classUnderTest);
>         testConfig.clusters(".*").enableModules(".*");

That is indeed how you control which modules get enabled during the test: by passing an appropriate regexp to enableModules.

If you just want to pass in mock services for your functional tests, there is no need to create a new module to hold these and play around with module enablement. Simply
implement the mocks in your test sources, and use org.netbeans.nbjunit.MockServices to register them during the test. (They will be loaded "ahead of" any normally
available services, which is generally enough for them to take precedence inside the test.)


Enable/Disable Modules for Deploy/Functional Tests

by Steve Mosley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the hints ... of course MockServices is exactly what I want, and I had read about it before but ... it slipped my mind.



I do have a problem with this though ... I can't get it to work in my UI Functional Tests



here is the basic code ...




Code:
public class MockTest extends JellyTestCase {



    public MockTest(String name) {

        super(name);

    }



    /**

     * Without this method the test will work, But I don't get a UI

     */

    public static Test suite() {

        NbModuleSuite.Configuration testConfig = NbModuleSuite.createConfiguration(MockTest.class);

        testConfig.clusters(".*").enableModules(".*");

        testConfig.gui(true);

        return NbModuleSuite.create(testConfig);

    }



    @Override

    public void setUp() {

        MockServices.setServices(Mock.class);

    }



    public void testIt(){

        BulletinService stuff = Lookup.getDefault().lookup(BulletinService.class);

        assertEquals(Mock.class, stuff.getClass());

    }



    public static class Mock implements BulletinService {



        @Override

        public BulletinResult submitBulletin(Bulletin arg0) {

            throw new UnsupportedOperationException("Not supported yet.");

        }

    }

}





Its having the suite method that is breaking it (but the suite method is required to setup the UI testing framework)



What happens is I get this exception stack ...




Code:
Testcase: testIt(com.metservice.castlepoint.maps.MockTest):        FAILED

expected:<class com.metservice.castlepoint.maps.MockTest$Mock> but was:<class com.metservice.castlepoint.maps.MockTest$Mock>

junit.framework.AssertionFailedError: expected:<class com.metservice.castlepoint.maps.MockTest$Mock> but was:<class com.metservice.castlepoint.maps.MockTest$Mock>

        at org.netbeans.junit.MockServices$ServiceClassLoader.<init>(MockServices.java:146)

        at org.netbeans.junit.MockServices.setServices(MockServices.java:91)

        at com.metservice.castlepoint.maps.MockTest.setUp(MockTest.java:40)

        at org.netbeans.junit.NbTestCase.runBare(NbTestCase.java:328)

        at org.netbeans.jellytools.JellyTestCase.runBare(JellyTestCase.java:147)

        at org.netbeans.junit.NbTestCase.run(NbTestCase.java:213)

        at org.netbeans.junit.NbModuleSuite$S.runInRuntimeContainer(NbModuleSuite.java:782)

        at org.netbeans.junit.NbModuleSuite$S.run(NbModuleSuite.java:570)





I can understand some of it, I have seen that kind of assertion fail before when one class is loaded from different class loaders, equal will then fail... and can see in the source (well the source I could find, must be an old version) in the MockServices class that some classloader magic is happening



But I have no idea how to fix things in this case ... any ideas floating around?



Cheers

Steve





Re: Enable/Disable Modules for Deploy/Functional Tests

by Jesse Glick :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

smozely wrote:
> junit.framework.AssertionFailedError: expected:<class com.metservice.castlepoint.maps.MockTest$Mock> but was:<class com.metservice.castlepoint.maps.MockTest$Mock>
>         at org.netbeans.junit.MockServices$ServiceClassLoader.<init>(MockServices.java:146)
>         at org.netbeans.junit.MockServices.setServices(MockServices.java:91)
>         at com.metservice.castlepoint.maps.MockTest.setUp(MockTest.java:40)
>         at org.netbeans.junit.NbTestCase.runBare(NbTestCase.java:328)
>         at org.netbeans.jellytools.JellyTestCase.runBare(JellyTestCase.java:147)
>         at org.netbeans.junit.NbTestCase.run(NbTestCase.java:213)
>         at org.netbeans.junit.NbModuleSuite$S.runInRuntimeContainer(NbModuleSuite.java:782)
>         at org.netbeans.junit.NbModuleSuite$S.run(NbModuleSuite.java:570)

May be a bug in MockServices and/or NbModuleSuite. Please file with complete self-contained steps to reproduce under xtest/nbjunit for it to be evaluated.