Programmatically starting server in one thread and running test in other thread using maven test command

1.3k views Asked by At

I have written cucumber/ selenium test cases for my spring mvc project.

My intention is, when I run mvn test from console, I am starting jetty server with spring(mvc dispatcher) beans loaded in one seperate thread and while normal flow consist of running cucumber test cases.

Like the one below.

RunCukesTest.java

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})
public class RunCukesTest {
    @BeforeClass
    public static void setUpBeforeClass() {
        try {
            UsageUIService.start();
        } catch (Exception e) { }
    }
}

UsageUIService

public class UsageUIService {
    public boolean start() throws Exception {
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(new Handler[] { AppContextBuilder.buildWebAppContext() } );
        jettyServer = new JettyServer();
        jettyServer.setHandler(contexts);
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(8085);
        jettyServer.addConnector(connector);
        Runnable runner = new Runnable() {
            @Override
            public void run() {
                try {
                    jettyServer.start();
                    jettyServer.join();
                } catch (Exception e) {             }
            }
        };
        new Thread(runner).start();
        return jettyServer.isStarted();
    }
    public boolean isStarted()
    {
        return jettyServer.isStarted();
    }
}

CollectionStepdefs

public class CollectionStepdefs {
    private WebDriver driver;
    @Before
    public void setUp() {
        driver = new HtmlUnitDriver();
        driver.get("xyz:8085/usage-ui/collection.htm");
    }
    @Given("^I want to see today collection count$")
    public void I_want_to_see_today_collection_count() {
        // assertion starts
    }
}

When I run UsageUIService using java main method, I can hit the url I mentioned in driver,

xyz:8085/usage-ui/collection.htm

The process still waits until I press Ctrl+C

However when running it as Junit/mvn test, I could not achieve and jetty server is not getting started. The process does not waits and finishes off as soon as executing @Test methods.

What is the difference between running a java program using main and running as junit?

Why while running as Junit it does not hold the prompt and hold the process till I execute ctrl+C?

I want to boot strap my application and it should start listening to port 8085 in one thread when i run through mvn test/Junit and simultaneously continue to execute cucumber test suites.

Is there any solution for this?

1

There are 1 answers

1
Joakim Erdfelt On

Jetty itself is a thread. Simplify your UsageUIService ...

jettyServer.addConnector(connector);
jettyServer.start();
// Don't do this -> jettyServer.join();
return jettyServer.isStarted();