2

I'm experimenting with Junit 4 it's been a while now. Part of my unit testing requires deploying a war application that exposes some web services. I'm using Eclipse Helios, and I've added two different jars to my project's build path which are: 1- jetty-all-8.1.3.v20120416.jar 2- servlet-api-3.0.jar

Now in my test class, I'm running the following code in order to start jetty and in order to deploy my axis2.war webapp that contains my exposed web services:

System.out.println("Initializing server");
       Server server = new Server(8181);
       System.out.println("Server initialized");

        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath("/");
        webapp.setParentLoaderPriority(true);
        webapp.setWar("C:\\axis2.war")

        server.setHandler(webapp);

        System.out.println("Starting server");
        server.start();

        System.out.println("Joining server");

        server.join();
        System.out.println("Finished starting Jetty...");

The output of the above code is as follows:

Server initialized
Starting server
Joining server

Trying to join the server is running indefinitely and it doesn't throw any exceptions whatsoever.

In another attempt, I have downloaded jetty 8.1.3 web server on my machine, and placed the axis.war under the webapps directory, and then started jetty using the start.jar from the command line. It worked perfectly from there, and I was able to reach my exposed web service methods.

However, I need to be able to start and stop jetty programmatically and deploy my web application programmatically as well. Could anyone tell me what could the problem be?

Thanks in advance.

Mouhammed Soueidane
  • 1,056
  • 2
  • 24
  • 42

1 Answers1

7

Don't join the thread.

see: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()

jesse mcconnell
  • 7,102
  • 1
  • 22
  • 33
  • The problem is that I was running my test case right after I join the server which was wrong. Joining, and based on the javadocs, would wait on a thread till it dies. This is however not the behavior that I wished for, as I wanted my test case to run right after jetty finishes starting. Getting rid of "server.join()" has actually fixed my problem.Thanks for the help man. – Mouhammed Soueidane May 31 '12 at 14:52
  • You can also call server.start() and server.join() in a separate thread and make the new thread as a daemon thread(setDeamon->true) and start it. – Pradeep Jawahar Aug 01 '13 at 02:57