开发者

Best design to organize HttpUnit+junit as standalone app

开发者 https://www.devze.com 2023-02-20 02:23 出处:网络
I have a task to write a set of http unit tests, which should be deployed and started as single threaded application on external server.

I have a task to write a set of http unit tests, which should be deployed and started as single threaded application on external server.

After some investigation and reading of documentation I came to following application structure:

public static void main(String... args) throws Exception {

        Class[] testCases = {
                LoginTest.class,
                Test2.class,
                Test3.class
        };
        TestSuite suite = new TestSuite(testCases);

        TestResult result = new TestResult();
        suite.run(result);

        displayResults(result);
    }

And testcase looks like:

public class LoginPageTest extends TestCase {    
    public void testLogin() throws IOException, SAXException {

        WebConversation wc = new开发者_开发百科 WebConversation();
        //Some HttpUnit init code here    

        loginForm.setParameter("j_username", login);
        loginForm.setParameter("j_password", pass);

        loginForm.submit();

        String expected = String.format("/%s/action/logon.do", endpoint); 

        assertEquals(wc.getCurrentPage().getURL().getPath(), expected);    
    }    
}

Has anyone made similar tasks ? Have you some advices than can improve this structure? How can I implement dependency between testcases (e.g. almost everything needs that user should be authenticated -> loginTestCase must be called) ?

Any advice greatly appreciated !

Thanks in advance.


Currently I've found one appropriate way to design my testing application:

TestCase class (extending jUnit):

public class LogoutTest extends TestCase {

    public void testLogout() throws IOException, SAXException {

        new Config().initApplication()
                .doLogin("user", "pass") // returns WelcomePage class
                .goToFilterPage()  //Returns another page
                .doLogout();  //returns LoginPage class
    }
   }

After some time I've decided that it will be good to make for each page specific class which will have it's specific control methods e.g. "go to next page", "logout", "click some button", etc...

public class WelcomePage extends AbstractPage  {

    protected WelcomePage(AbstractPage other) {
        super(other);

        String expected = String.format("/%s/welcome.do", Config.endpoint);
        assertEquals(expected, webConversation.getCurrentPage().getURL().getPath());
        logger.info(String.format("Current page: %s", webConversation.getCurrentPage().getURL().getPath() ));
    }

    public FilterPage goToFilterPage() throws SAXException, IOException {
        WebLink link = webConversation.getCurrentPage().getLinkWithID("downloadLink");
        assertNotNull("Check if link exist on page", link);
        link.click();    
        return new FilterPage(this);
    }    
}

AbstractPage class (implements some common logic for all pages):

public abstract class AbstractPage {

    protected WebConversation webConversation;

    protected Logger logger;

    protected AbstractPage(AbstractPage other) {
        this.webConversation = other.getWebConversation();
        this.logger = Logger.getLogger(this.getClass());
    }

    protected AbstractPage(WebConversation webConversation) {
        this.webConversation = webConversation;
        this.logger = Logger.getLogger(this.getClass());
    }

    public WebConversation getWebConversation() {
        return webConversation;
    }

    public void doLogout() throws SAXException, IOException {    
        WebLink logoutLink = webConversation.getCurrentPage().getLinkWithID("logoutLink");
        assertNotNull(logoutLink);
            logoutLink.click();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消