|
| 1 | +package feature; |
| 2 | + |
| 3 | +import com.xpeho.spring_boot_java_random_user.SpringBootJavaRandomUserApplication; |
| 4 | +import io.cucumber.spring.CucumberContextConfiguration; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.resttestclient.TestRestTemplate; |
| 7 | +import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 10 | +import org.springframework.http.HttpEntity; |
| 11 | +import org.springframework.http.HttpHeaders; |
| 12 | +import org.springframework.http.MediaType; |
| 13 | +import org.springframework.http.ResponseEntity; |
| 14 | +import org.springframework.test.context.ActiveProfiles; |
| 15 | + |
| 16 | +@CucumberContextConfiguration |
| 17 | +@ActiveProfiles("test") |
| 18 | +@AutoConfigureTestRestTemplate |
| 19 | +@SpringBootTest( |
| 20 | + classes = SpringBootJavaRandomUserApplication.class, |
| 21 | + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, |
| 22 | + properties = { |
| 23 | + "spring.sql.init.mode=always", |
| 24 | + "spring.sql.init.schema-locations=classpath:schema-h2.sql" |
| 25 | + } |
| 26 | +) |
| 27 | +public class SpringIntegrationTest { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + protected TestRestTemplate restTemplate; |
| 31 | + |
| 32 | + @LocalServerPort |
| 33 | + protected int port; |
| 34 | + |
| 35 | + protected ResponseEntity<String> latestResponse; |
| 36 | + |
| 37 | + protected void executePost(String path, Object payload) { |
| 38 | + String url = "http://localhost:" + port + path; |
| 39 | + HttpHeaders headers = new HttpHeaders(); |
| 40 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 41 | + HttpEntity<Object> request = new HttpEntity<>(payload, headers); |
| 42 | + latestResponse = restTemplate.postForEntity(url, request, String.class); |
| 43 | + } |
| 44 | +} |
0 commit comments