Skip to content

Commit d478d66

Browse files
authored
Add e2e tests for Spring 7 sample (#4753)
1 parent 8294a87 commit d478d66

File tree

8 files changed

+136
-4
lines changed

8 files changed

+136
-4
lines changed

.github/workflows/system-tests-backend.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ jobs:
7878
- sample: "sentry-samples-spring-boot-4-opentelemetry"
7979
agent: "true"
8080
agent-auto-init: "false"
81+
- sample: "sentry-samples-spring-7"
82+
agent: "false"
83+
agent-auto-init: "true"
8184
- sample: "sentry-samples-spring-jakarta"
8285
agent: "false"
8386
agent-auto-init: "true"

sentry-samples/sentry-samples-spring-7/build.gradle.kts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
33
import org.springframework.boot.gradle.plugin.SpringBootPlugin
44

55
plugins {
6+
application
67
alias(libs.plugins.springboot4) apply false
78
alias(libs.plugins.spring.dependency.management)
89
alias(libs.plugins.kotlin.jvm)
@@ -11,6 +12,11 @@ plugins {
1112
alias(libs.plugins.gretty)
1213
}
1314

15+
application { mainClass.set("io.sentry.samples.spring7.Main") }
16+
17+
// Ensure WAR is up to date before run task
18+
tasks.named("run") { dependsOn(tasks.named("war")) }
19+
1420
group = "io.sentry.sample.spring-7"
1521

1622
version = "0.0.1-SNAPSHOT"
@@ -37,13 +43,17 @@ dependencies {
3743
implementation(libs.logback.classic)
3844
implementation(libs.servlet.jakarta.api)
3945
implementation(libs.slf4j2.api)
46+
47+
implementation(libs.tomcat.catalina.jakarta)
48+
implementation(libs.tomcat.embed.jasper.jakarta)
49+
50+
testImplementation(projects.sentrySystemTestSupport)
51+
testImplementation(libs.kotlin.test.junit)
4052
testImplementation(libs.springboot.starter.test) {
4153
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
4254
}
4355
}
4456

45-
tasks.withType<Test>().configureEach { useJUnitPlatform() }
46-
4757
tasks.withType<KotlinCompile>().configureEach {
4858
kotlin {
4959
explicitApi()
@@ -55,3 +65,26 @@ tasks.withType<KotlinCompile>().configureEach {
5565
compilerOptions.apiVersion = org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9
5666
}
5767
}
68+
69+
configure<SourceSetContainer> { test { java.srcDir("src/test/java") } }
70+
71+
tasks.register<Test>("systemTest").configure {
72+
group = "verification"
73+
description = "Runs the System tests"
74+
75+
outputs.upToDateWhen { false }
76+
77+
maxParallelForks = 1
78+
79+
// Cap JVM args per test
80+
minHeapSize = "128m"
81+
maxHeapSize = "1g"
82+
83+
filter { includeTestsMatching("io.sentry.systemtest*") }
84+
}
85+
86+
tasks.named("test").configure {
87+
require(this is Test)
88+
89+
filter { excludeTestsMatching("io.sentry.systemtest.*") }
90+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.sentry.samples.spring7;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import org.apache.catalina.LifecycleException;
6+
import org.apache.catalina.startup.Tomcat;
7+
8+
public class Main {
9+
10+
public static void main(String[] args) throws LifecycleException, IOException {
11+
File webappsDirectory = new File("./tomcat.8080/webapps");
12+
if (!webappsDirectory.exists()) {
13+
boolean didCreateDirectories = webappsDirectory.mkdirs();
14+
if (!didCreateDirectories) {
15+
throw new RuntimeException(
16+
"Failed to create directory required by Tomcat: " + webappsDirectory.getAbsolutePath());
17+
}
18+
}
19+
20+
String pathToWar = "./build/libs";
21+
String warName = "sentry-samples-spring-7-0.0.1-SNAPSHOT";
22+
File war = new File(pathToWar + "/" + warName + ".war");
23+
24+
Tomcat tomcat = new Tomcat();
25+
tomcat.setPort(8080);
26+
tomcat.getConnector();
27+
28+
tomcat.addWebapp("/" + warName, war.getCanonicalPath());
29+
tomcat.start();
30+
tomcat.getServer().await();
31+
}
32+
}

sentry-samples/sentry-samples-spring-7/src/main/java/io/sentry/samples/spring7/web/Person.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package io.sentry.samples.spring7.web;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
36
public class Person {
47
private final String firstName;
58
private final String lastName;
69

7-
public Person(String firstName, String lastName) {
10+
@JsonCreator
11+
public Person(
12+
@JsonProperty("firstName") String firstName, @JsonProperty("lastName") String lastName) {
813
this.firstName = firstName;
914
this.lastName = lastName;
1015
}

sentry-samples/sentry-samples-spring-7/src/main/java/io/sentry/samples/spring7/web/PersonController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public PersonController(PersonService personService) {
2222
}
2323

2424
@GetMapping("{id}")
25-
Person person(@PathVariable Long id) {
25+
Person person(@PathVariable("id") Long id) {
2626
Sentry.logger().warn("warn Sentry logging");
2727
Sentry.logger().error("error Sentry logging");
2828
Sentry.logger().info("hello %s %s", "there", "world!");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.sentry
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertTrue
5+
6+
class DummyTest {
7+
@Test
8+
fun `the only test`() {
9+
// only needed to have more than 0 tests and not fail the build
10+
assertTrue(true)
11+
}
12+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.sentry.systemtest
2+
3+
import io.sentry.systemtest.util.TestHelper
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
import org.junit.Before
7+
8+
class PersonSystemTest {
9+
lateinit var testHelper: TestHelper
10+
11+
@Before
12+
fun setup() {
13+
testHelper = TestHelper("http://localhost:8080/sentry-samples-spring-7-0.0.1-SNAPSHOT")
14+
testHelper.reset()
15+
}
16+
17+
@Test
18+
fun `get person fails`() {
19+
val restClient = testHelper.restClient
20+
restClient.getPerson(11L)
21+
assertEquals(500, restClient.lastKnownStatusCode)
22+
23+
testHelper.ensureTransactionReceived { transaction, envelopeHeader ->
24+
testHelper.doesTransactionHaveOp(transaction, "http.server")
25+
}
26+
27+
Thread.sleep(10000)
28+
29+
testHelper.ensureLogsReceived { logs, envelopeHeader ->
30+
testHelper.doesContainLogWithBody(logs, "warn Sentry logging") &&
31+
testHelper.doesContainLogWithBody(logs, "error Sentry logging") &&
32+
testHelper.doesContainLogWithBody(logs, "hello there world!")
33+
}
34+
}
35+
36+
@Test
37+
fun `create person works`() {
38+
val restClient = testHelper.restClient
39+
val person = Person("firstA", "lastB")
40+
val returnedPerson = restClient.createPerson(person)
41+
assertEquals(200, restClient.lastKnownStatusCode)
42+
43+
assertEquals(person.firstName, returnedPerson!!.firstName)
44+
assertEquals(person.lastName, returnedPerson!!.lastName)
45+
}
46+
}

test/system-test-runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ def get_available_modules(self) -> List[ModuleConfig]:
713713
"""Get list of all available test modules."""
714714
return [
715715
ModuleConfig("sentry-samples-spring", "false", "true", "false"),
716+
ModuleConfig("sentry-samples-spring-7", "false", "true", "false"),
716717
ModuleConfig("sentry-samples-spring-jakarta", "false", "true", "false"),
717718
ModuleConfig("sentry-samples-spring-boot", "false", "true", "false"),
718719
ModuleConfig("sentry-samples-spring-boot-opentelemetry-noagent", "false", "true", "false"),

0 commit comments

Comments
 (0)