diff --git a/build.gradle b/build.gradle index f79e90183dbad9dc569e8071107ac978a3768a62..f6413758768f4ed0a53143a1c38789bf88d601a3 100644 --- a/build.gradle +++ b/build.gradle @@ -34,20 +34,23 @@ dependencies { // implementation 'org.mariadb.jdbc:mariadb-java-client:2.1.2' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.mariadb.jdbc:mariadb-java-client' - + implementation 'org.springframework.boot:spring-boot-starter-validation' - implementation 'org.springframework.boot:spring-boot-starter-security' + implementation 'org.springframework.boot:spring-boot-starter-security' - compileOnly 'org.projectlombok:lombok' + compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' testImplementation 'org.springframework.security:spring-security-test' - testImplementation("io.github.bonigarcia:webdrivermanager:5.1.0") - //selenium + testImplementation("io.github.bonigarcia:webdrivermanager:5.2.0") testImplementation group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.32' testImplementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.0' +// testImplementation "io.github.bonigarcia:webdrivermanager:5.1.0" +// testImplementation group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.53.0' +// testImplementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.5.0' + } tasks.named('test') { diff --git a/src/test/java/uk/ac/cf/spring/demo/ChromeDriverTest.java b/src/test/java/uk/ac/cf/spring/demo/ChromeDriverTest.java index a4fc1668d1d89606a31b47375bea44942ed8a668..d7473ca4bc054d458c65f9cdf37fb2ce1bd2282c 100644 --- a/src/test/java/uk/ac/cf/spring/demo/ChromeDriverTest.java +++ b/src/test/java/uk/ac/cf/spring/demo/ChromeDriverTest.java @@ -28,10 +28,12 @@ public class ChromeDriverTest { WebDriver webDriver; @BeforeEach void setupTest() { - ChromeOptions options = new ChromeOptions(); - options.addArguments("--remote-debugging-port=42227"); +// ChromeOptions options = new ChromeOptions(); + FirefoxOptions options = new FirefoxOptions(); +// options.addArguments("--remote-debugging-port=42227"); options.addArguments("--headless"); - webDriver = new ChromeDriver(options); +// webDriver = new ChromeDriver(options); + webDriver = new FirefoxDriver(options); } @AfterEach diff --git a/src/test/java/uk/ac/cf/spring/demo/HTTPConnectionTest.java b/src/test/java/uk/ac/cf/spring/demo/HTTPConnectionTest.java index 265883fc274c00c404197f72e07d620fbc68f5aa..0029d1cb920e9313f871b7a28ec24a2b87b1b1a0 100644 --- a/src/test/java/uk/ac/cf/spring/demo/HTTPConnectionTest.java +++ b/src/test/java/uk/ac/cf/spring/demo/HTTPConnectionTest.java @@ -13,37 +13,52 @@ import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -// System Test Full container and servlet public class HTTPConnectionTest { - @Value(value="${local.server.port}") + + // 通过 Spring 注入的端口 + @Value(value = "${local.server.port}") private int port; + // RestTemplate 用于发送 HTTP 请求 @Autowired private TestRestTemplate restTemplate; - // Full container and servlet and no database + + // 提取常量,避免在每个测试方法中重复 + private String baseUrl() { + return "http://localhost:" + port; + } + + // 测试页面加载是否包含指定内容 @Test public void greetingShouldReturnDefaultMessage() throws Exception { - assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/html/matchSchedule.html", - String.class)).contains("Sport"); + String url = baseUrl() + "/html/matchSchedule.html"; + + // 使用 RestTemplate 发送 GET 请求并检查返回内容 + String response = this.restTemplate.getForObject(url, String.class); + + // 检查响应中是否包含 "Sport" 字样 + assertThat(response).contains("Sport"); } -// Full container and servlet with Test db + + // 测试数据库接口(模拟用户登录并检查后续请求) @Test public void dbMatchTest() throws Exception { + String loginUrl = baseUrl() + "/api/users/login"; + String matchUrl = baseUrl() + "/match"; + // 准备登录数据 Map<String, String> loginData = new HashMap<>(); loginData.put("email", "shy@creditsafe.com"); loginData.put("password", "shy"); - // 向登录接口发送 POST 请求,模拟登录 - ResponseEntity<String> loginResponse = this.restTemplate.postForEntity( - "http://localhost:" + port + "/api/users/login", - loginData, - String.class); + // 发送 POST 请求模拟登录 + ResponseEntity<String> loginResponse = this.restTemplate.postForEntity(loginUrl, loginData, String.class); - // 确认登录成功 + // 验证登录成功消息 assertThat(loginResponse.getBody()).contains("Login successful"); - // 检查接口是否有pools字段 - assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/match", - String.class)).contains("Pools"); -} + + // 检查 match 接口返回的内容是否包含 "Pools" 字段 + String matchResponse = this.restTemplate.getForObject(matchUrl, String.class); + assertThat(matchResponse).contains("Pools"); + } }