IfGradleThe constructed project isWebProject may contain someIntegration TestAnd functional testing. These tests andUnit TestThe difference is that before running the service, you must start the Web Service locally and automatically close the Web service after testing.
To start the Web Service locally, we can use the built-in jetty plug-in of gradle. Jetty is a lightweight Web Container with fast execution, simple configuration, and convenient remote debugging. To enable jetty, you only need to add reference to this plug-in build. gradle.
Apply Plugin: 'jetty'
You can configure the war package name, startup port, and other attributes.
Apply Plugin: 'jetty'
Httpport = 9876
[Jettyrun, jettyrunwar, jettystop] *. stopport = 9966
[Jettyrun, jettyrunwar, jettystop] *. stopkey = 'stopkey'
We need to separate integration testing from general unit testing. Because the unit test does not need to start the Web service in advance, so that it can be executed quickly and get faster feedback. Generally, the extension name of the unit test isTest. Java, with the extension integrationtest. java.
Configure the test set for unit test execution.
Test {
Include '**/* test. class'
Exclude '**/* integrationtest. class'
}
Create a new task to run the integration test.
Build. gradle Task inttest (type: Test, dependson: Test ){ Include '**/* integrationtest. class' Dofirst { Jettyrun. Daemon = true Jettyrun.exe cute () } Dolast { Jettystop.exe cute () } } |
The above code first creates a task named inttest, which is of the test Type and depends on the test task. This integration test only runs the test class named integrationtest. Before running the test, start the jetty server in the background mode and call the jettystop task to stop the jetty service.
To enable inttest task to run during gradle build, you can add dependency on inttest.
Build. dependson inttest
In this way, the integration test is also run when gradle build is run. Web services are automatically started and closed before and after the integration test.