51Testing软件测试论坛

标题: 学习《JUnit IN ACTION》一书时,遇到的jetty问题 [打印本页]

作者: fairylly    时间: 2008-2-29 10:40
标题: 学习《JUnit IN ACTION》一书时,遇到的jetty问题
//*********************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)
作者: fairylly    时间: 2008-2-29 10:46
标题: Test suite()方法不是自动运行的吗?
Test suite()方法不是自动运行的吗?
作者: fairylly    时间: 2008-2-29 12:23
刚重新建立工程,再导入包,java文件,运行成功
可在之前那个工程就是不行

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

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

想不通为什么会出现这样的情况!!!!!!!!!




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2