Currently, Spring-boot has become the mainstream framework for Java development. Cucumber is an automated testing tool that supports DSL, which is very suitable for users to write DSL-optimized unit tests and other test cases. This article will explain how to configure Cucumber in SpringBoot for automated testing.
The directory structure is as follows:
The gradle-related configuration is as follows:
testCompile('org.springframework.boot:spring-boot-starter-test', "info.cukes:cucumber-jvm:1.1.8", "info.cukes:cucumber-core:1.1.8", "info.cukes:cucumber-java:1.1.8", "info.cukes:cucumber-junit:1.1.8", "info.cukes:cucumber-spring:1.1.8", 'commons-io:commons-io:2.4')Src is a common spring code structure, and we will not explain it in detail. The above in the test folder is the SpringBootTest test framework that comes with it, and the content in the demo is the specific configuration of the Cucumber framework.
1. First we need to configure the basic Cucumber framework CucumberTest.java:
@RunWith(Cucumber.class)@CucumberOptions(features = "src/test/resources")public class CucumberTest {}2. Next, we need to configure the configuration about Spring-boot, because our testing framework also needs to complete AbstractDefs.java under the running of springboot:
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = TestSpringMongoApplication.class)@WebAppConfiguration@SpringBootTestpublic class AbstractDefs{}3. Then, we start writing our DSL parsing syntax, which can be used to test the specific test process TestSteps.java:
import com.tiandi.service.FaultInjectionInfoService;import cucumber.api.java.en.And;import cucumber.api.java.en.Given;import cucumber.api.java.en.Then;import cucumber.api.java.en.When;import org.springframework.beans.factory.annotation.Autowired;public class TestSteps extends AbstractDefs { @Autowired private FaultInjectionInfoService faultInjectionInfoService; private String faultNodeId; @Given("^Generate testCase for /"([^/"]*)/"$") public void generateTestCase(String id) { faultNodeId = id; } @Then("^finish$") public void finish() { faultInjectionInfoService.generateTestCase(faultNodeId); }}//Here, we injected the service in spring through Autowired, and wrote user-defined DSL to call the method in the service to execute, obtain the test results.4. Finally, we fill in the specific cucumber test cases in resources and execute them.
Feature: test cucumber Scenario: system out print something Given Generate testCase for "COM-COM-F" Then finish
5. The test results are shown in the figure
Green represents test pass
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.