java - 将 HttpUnit+junit 组织为独立应用程序的最佳设计

标签 java unit-testing junit

我的任务是编写一组 http 单元测试,这些测试应该作为单线程应用程序在外部服务器上部署和启动。

经过一些调查和阅读文档后,我得出以下应用程序结构:

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);
    }

测试用例看起来像:

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);    
    }    
}

有人做过类似的任务吗?您有一些可以改进此结构的建议吗?如何实现测试用例之间的依赖关系(例如,几乎所有内容都需要用户进行身份验证 ->必须调用 loginTestCase)?

非常感谢任何建议!

提前致谢。

最佳答案

目前,我找到了一种设计测试应用程序的合适方法:

TestCase 类(扩展 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
    }
   }

一段时间后,我决定为每个页面创建特定的类,该类将具有特定的控制方法,例如“转到下一页”、“注销”、“单击某个按钮”等...

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 类(实现所有页面的一些通用逻辑):

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();
    }
}

关于java - 将 HttpUnit+junit 组织为独立应用程序的最佳设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5460653/

相关文章:

android - 使用 Mockito 1.9.5 和 DexMaker-Mockito-1.0 的验证错误

java - JUnit - 没有可运行的方法

java - 在嵌套面板中监听击键

c# - C# 代码的单元测试,其中主要处理发生在 SQL 存储过程中

java - 如何合并arraylist元素?

java - 使用 Junit 4 进行 java servlet 的 Happy Flow 测试用例

c++ - 我怎样才能告诉 GoogleMock 在测试完成后停止检查期望值?

java - JUnit Spring - HSQLDB 在测试后不会关闭

java - 在Google App Engine上配置Quercus

java - 矩阵中的最大值