Skip to content

Instantly share code, notes, and snippets.

@unclebean
Last active March 10, 2025 05:11
Show Gist options
  • Save unclebean/9807c8711066d0683df740855a335cf3 to your computer and use it in GitHub Desktop.
Save unclebean/9807c8711066d0683df740855a335cf3 to your computer and use it in GitHub Desktop.
cucumber
package com.example.app.cucumber.stepdefs;
import io.cucumber.java.en.Given;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.*;
import java.util.HashMap;
import java.util.Map;
public class CommonSteps {
private static String jwtToken;
@Autowired
private RestTemplate restTemplate;
@Given("I authenticate with valid credentials")
public void authenticateWithValidCredentials() {
if (jwtToken == null) {
String authUrl = "http://localhost:8080/api/auth/login";
Map<String, String> credentials = new HashMap<>();
credentials.put("username", "testuser");
credentials.put("password", "password");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, String>> request = new HttpEntity<>(credentials, headers);
ResponseEntity<Map> response = restTemplate.exchange(authUrl, HttpMethod.POST, request, Map.class);
jwtToken = (String) response.getBody().get("token");
}
}
public static String getJwtToken() {
return jwtToken;
}
}
import com.hotels.search.solr.EmbeddedSolrServerFactory;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.junit.jupiter.api.*;
import java.io.File;
public class EmbeddedSolrTest {
private static SolrClient solrClient;
@BeforeAll
static void startSolr() throws Exception {
solrClient = EmbeddedSolrServerFactory.create(new File("src/test/resources/solr"), "test_core");
}
@Test
void testSolrQuery() throws Exception {
SolrQuery query = new SolrQuery("*:*"); // Fetch all docs
QueryResponse response = solrClient.query(query);
SolrDocumentList results = response.getResults();
Assertions.assertNotNull(results);
}
@AfterAll
static void stopSolr() throws Exception {
solrClient.close();
}
}
Feature: User Login
Scenario: Successful Login
Given the user is on the login page
When the user enters valid credentials
Then the user should be logged in
package com.example.stepdefs;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest
public class LoginStepDefinitions {
private boolean loggedIn = false;
@Given("the user is on the login page")
public void userIsOnLoginPage() {
System.out.println("User is on the login page");
}
@When("the user enters valid credentials")
public void userEntersValidCredentials() {
loggedIn = true; // Simulate login logic
}
@Then("the user should be logged in")
public void userShouldBeLoggedIn() {
assertTrue(loggedIn, "User is not logged in");
}
}
<dependencies>
<!-- Cucumber Dependencies -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
<!-- JUnit 5 (Cucumber works with JUnit 5) -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Test Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.hotels</groupId>
<artifactId>otj-solr-embedded</artifactId>
<version>1.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/features",
glue = "com.example.stepdefs",
plugin = {"pretty", "html:target/cucumber-report.html"}
)
public class CucumberTest {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment