51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3782|回复: 2
打印 上一主题 下一主题

学习《JUnit IN ACTION》一书时,遇到的jetty问题

[复制链接]
  • TA的每日心情
    无聊
    2018-2-28 12:22
  • 签到天数: 4 天

    连续签到: 1 天

    [LV.2]测试排长

    跳转到指定楼层
    1#
    发表于 2008-2-29 10:40:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    //*********************TestWebClient1.java*******************************//
    package stubtest;
    import java.net.URL;
    import junit.framework.TestCase;
    import junit.framework.Test;
    import junit.framework.TestSuite;

    public class TestWebClient1 extends TestCase{

    public static Test suite()
    {
      TestSuite suite=new TestSuite();
      suite.addTestSuite(TestWebClient1.class);
      return new TestWebClientSetup1(suite);
      
    }
    //*********************在没有下面这个测试方法时,上面的Test suite()方法就不会运行************************************//
    public void testSuite() {
      junit.textui.TestRunner.run(
        suite());
    }

    public void testGetContentOk() throws Exception
    {
      WebClient client=new WebClient();
      String result=client.getContent(new URL("http://localhost:8080/testGetContentOK"));
      assertEquals("It works",result);
    }
    public void testGetContentNotFound() throws Exception
    {
      WebClient client=new WebClient();
      String result=client.getContent(new URL("http://localhost:8080/testGetContentNotFound"));
      assertNull(result);
    }
    /*
    public static void main(String[] args){
      junit.textui.TestRunner.run(
        suite());
    }*/


    }



    //****************************************TestWebClientSetup1.java*************************************************//
    package stubtest;
    import junit.framework.Test;
    import junit.framework.TestSuite;
    import org.mortbay.http.HttpContext;
    import org.mortbay.http.HttpFields;
    import org.mortbay.http.HttpRequest;
    import org.mortbay.http.HttpResponse;
    import org.mortbay.http.HttpServer;
    import org.mortbay.http.SocketListener;
    import org.mortbay.http.handler.AbstractHttpHandler;
    import org.mortbay.http.handler.ResourceHandler;
    import org.mortbay.util.ByteArrayISO8859Writer;
    import junit.extensions.TestSetup;
    import org.mortbay.jetty.*;
    import org.mortbay.http.handler.NotFoundHandler;
    import java.io.IOException;
    import java.io.OutputStream;

    public class TestWebClientSetup1 extends TestSetup{
    protected static HttpServer server;
    public TestWebClientSetup1(Test suite)
    {
      super(suite);
    }
    protected void setUp() throws Exception
    {
      server = new HttpServer();
      SocketListener listener = new SocketListener();
      listener.setPort(8080);
      server.addListener(listener);
      HttpContext context1 = new HttpContext();
      context1.setContextPath("/testGetContentOK");
      context1.setResourceBase("./");
      context1.addHandler(new TestGetContentOkHandler());
      server.addContext(context1);
      
      HttpContext context2=new HttpContext();
      context2.setContextPath("/testGetContentNotFound");
      context2.setResourceBase("./");
      context2.addHandler(new NotFoundHandler());
      server.addContext(context2);
      
      server.start();
    }
    protected void tearDown() throws Exception
    {
      server.stop();
    }
    private class TestGetContentOkHandler extends AbstractHttpHandler
    {
      public void handle(String pathInContent,String pathParams,
        HttpRequest request,HttpResponse response)throws IOException
      {
       OutputStream out=response.getOutputStream();
       ByteArrayISO8859Writer writer=new ByteArrayISO8859Writer();
       writer.write("It works");
       writer.flush();
       response.setIntField(HttpFields.__ContentLength, writer.size());
       writer.writeTo(out);
       out.flush();
       request.setHandled(true);
      }
    }
    }



    运行Test后console显示如下:

    09:57:37.953 EVENT  Checking Resource aliases
    09:57:38.000 EVENT  Starting Jetty/4.2.17
    09:57:38.000 EVENT  Started HttpContext[/testGetContentOK]
    09:57:38.000 EVENT  Started HttpContext[/testGetContentNotFound]
    09:57:38.000 EVENT  Started SocketListener on 0.0.0.0:8080
    09:57:38.000 EVENT  Started [email=org.mortbay.http.HttpServer@1ffb8dc]org.mortbay.http.HttpServer@1ffb8dc[/email]
    .09:57:38.000 EVENT  Starting Jetty/4.2.17
    09:57:38.015 EVENT  Started HttpContext[/testGetContentOK]
    09:57:38.015 EVENT  Started HttpContext[/testGetContentNotFound]
    09:57:38.015 WARN!! Failed to start: [email=SocketListener@0.0.0.0:8080]SocketListener@0.0.0.0:8080[/email]
    E
    Time: 0.015
    There was 1 error:
    1) [email=junit.framework.TestSuite@dc840forg.mortbay.util.MultiException[java.net.BindException]junit.framework.TestSuite@dc840forg.mortbay.util.MultiException[java.net.BindException[/email]: Address already in use: JVM_Bind]
    at org.mortbay.http.HttpServer.start(HttpServer.java:640)
    at stubtest.TestWebClientSetup1.setUp(TestWebClientSetup1.java:55)
    at junit.extensions.TestSetup$1.protect(TestSetup.java:22)
    at junit.extensions.TestSetup.run(TestSetup.java:27)
    at stubtest.TestWebClient1.testSuite(TestWebClient1.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at junit.extensions.TestDecorator.basicRun(TestDecorator.java:24)
    at junit.extensions.TestSetup$1.protect(TestSetup.java:23)
    at junit.extensions.TestSetup.run(TestSetup.java:27)
    at stubtest.TestWebClient1.testSuite(TestWebClient1.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    FAILURES!!!
    Tests run: 0,  Failures: 0,  Errors: 1
    ..09:57:38.125 EVENT  Stopped HttpContext[/testGetContentOK]
    09:57:38.125 EVENT  Stopped HttpContext[/testGetContentNotFound]
    09:57:38.125 EVENT  Stopped [email=org.mortbay.http.HttpServer@1a2961b]org.mortbay.http.HttpServer@1a2961b[/email]
    Time: 0.25
    OK (3 tests)
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2018-2-28 12:22
  • 签到天数: 4 天

    连续签到: 1 天

    [LV.2]测试排长

    2#
     楼主| 发表于 2008-2-29 10:46:21 | 只看该作者

    Test suite()方法不是自动运行的吗?

    Test suite()方法不是自动运行的吗?
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2018-2-28 12:22
  • 签到天数: 4 天

    连续签到: 1 天

    [LV.2]测试排长

    3#
     楼主| 发表于 2008-2-29 12:23:52 | 只看该作者
    刚重新建立工程,再导入包,java文件,运行成功
    可在之前那个工程就是不行

    新工程中,没有使用下面public void testSuite() 这个方法,
    public void testSuite() {
      junit.textui.TestRunner.run(
        suite());
    }

    它会自动运行public static Test suite()

    想不通为什么会出现这样的情况!!!!!!!!!
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-9 00:34 , Processed in 0.081973 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表