Dashboard > RIFE > ... > Repository > Repository mocking and testing
RIFE Log In | Sign Up   View a printable version of the current page.
Repository mocking and testing


Added by Geert Bevin, last edited by Geert Bevin on Jun 23, 2005  (view change)
Labels: 
(None)

It's very easy to create a custom Repository implementation for testing purposes and to plug it in as the default repository. This example below creates a mock repository with a single mock participant to setup some specific configuration settings that might be needed for testing. The following class is all that is needed:

public class MockRepository implements Repository
{
    class MockParticipant extends SingleObjectParticipant
    {
        public Object getObject()
        {
            Config config = new Config();
            config.setParameter("key", "value");
            return config;
        }
    }
    
    private Map mParticipants = new HashMap();
    
    public MockRepository()
    {
        mParticipants.put("ParticipantConfig", new MockParticipant());
    }
    
    public boolean hasParticipant(String name)
    {
        return mParticipants.containsKey(name);
    }

    public Participant getParticipant(String name)
    {
        return (Participant)mParticipants.get(name);
    }
    
    public Collection getParticipants(String name)
    {
        return mParticipants.values();
    }
    
    public Object getContext() { return null; }
    public boolean isFinished() { return true; }
    public void cleanup() {}
	
    public Properties getProperties()
    {
        return System.getProperties();
    }
}

Whenever you need to use this repository instead of the current default one, you just have to add this line of code:

Rep.setDefaultRepository(new MockRepository());

If you want to restore the previously active repository afterwards, you can retrieve it beforehand with

Rep.getDefaultRepository();

and set it back later.



Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.1a Build:#515 May 19, 2006) - Bug/feature request - Contact Administrators