Skip to content

Add E2E tests for the spring sample #4640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/system-tests-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ jobs:
- sample: "sentry-samples-spring-jakarta"
agent: "false"
agent-auto-init: "true"
- sample: "sentry-samples-spring"
agent: "false"
agent-auto-init: "true"
steps:
- uses: actions/checkout@v4
with:
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ feign-gson = { module = "io.github.openfeign:feign-gson", version.ref = "feign"
graphql-java17 = { module = "com.graphql-java:graphql-java", version = "17.3" }
graphql-java22 = { module = "com.graphql-java:graphql-java", version = "22.1" }
graphql-java24 = { module = "com.graphql-java:graphql-java", version = "24.0" }
jackson-bom = { module = "com.fasterxml.jackson:jackson-bom", version.ref = "jackson" }
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
jackson-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" }
jetbrains-annotations = { module = "org.jetbrains:annotations", version = "23.0.0" }
kotlin-bom = { module = "org.jetbrains.kotlin:kotlin-bom", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
Expand Down Expand Up @@ -153,6 +155,8 @@ springboot3-starter-actuator = { module = "org.springframework.boot:spring-boot-
timber = { module = "com.jakewharton.timber:timber", version = "4.7.1" }

# tomcat libraries
tomcat-catalina = { module = "org.apache.tomcat:tomcat-catalina", version = "9.0.108" }
tomcat-embed-jasper = { module = "org.apache.tomcat.embed:tomcat-embed-jasper", version = "9.0.108" }
tomcat-catalina-jakarta = { module = "org.apache.tomcat:tomcat-catalina", version = "11.0.10" }
tomcat-embed-jasper-jakarta = { module = "org.apache.tomcat.embed:tomcat-embed-jasper", version = "11.0.10" }

Expand Down
110 changes: 68 additions & 42 deletions sentry-samples/sentry-samples-spring/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,89 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES

plugins {
alias(libs.plugins.springboot2) apply false
alias(libs.plugins.spring.dependency.management)
kotlin("jvm")
alias(libs.plugins.kotlin.spring)
id("war")
alias(libs.plugins.gretty)
application
alias(libs.plugins.springboot2) apply false
alias(libs.plugins.spring.dependency.management)
kotlin("jvm")
alias(libs.plugins.kotlin.spring)
id("war")
alias(libs.plugins.gretty)
}

application { mainClass.set("io.sentry.samples.spring.Main") }

// Ensure WAR is up to date before run task
tasks.named("run") { dependsOn(tasks.named("war")) }

group = "io.sentry.sample.spring"

version = "0.0.1-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

spotless {
kotlinGradle {
// This file throws an unclear error
targetExclude("build.gradle.kts")
}
}

repositories {
mavenCentral()
}
repositories { mavenCentral() }

dependencyManagement {
imports {
mavenBom(BOM_COORDINATES)
}
imports {
mavenBom(BOM_COORDINATES)
mavenBom(libs.kotlin.bom.get().toString())
mavenBom(libs.jackson.bom.get().toString())
}
}

dependencies {
implementation(Config.Libs.springWeb)
implementation(Config.Libs.springAop)
implementation(Config.Libs.aspectj)
implementation(Config.Libs.springSecurityWeb)
implementation(Config.Libs.springSecurityConfig)
implementation(Config.Libs.kotlinReflect)
implementation(kotlin(Config.kotlinStdLib, KotlinCompilerVersion.VERSION))
implementation(projects.sentrySpring)
implementation(projects.sentryLogback)
implementation(libs.logback.classic)
implementation(libs.servlet.api)
testImplementation(libs.springboot.starter.test) {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
}
implementation(Config.Libs.springWeb)
implementation(Config.Libs.springAop)
implementation(Config.Libs.aspectj)
implementation(Config.Libs.springSecurityWeb)
implementation(Config.Libs.springSecurityConfig)
implementation(Config.Libs.kotlinReflect)
implementation(kotlin(Config.kotlinStdLib, KotlinCompilerVersion.VERSION))
implementation(projects.sentrySpring)
implementation(projects.sentryLogback)
implementation(libs.jackson.databind)
implementation(libs.logback.classic)
implementation(libs.servlet.api)

tasks.withType<Test>().configureEach {
useJUnitPlatform()
implementation(libs.tomcat.catalina)
implementation(libs.tomcat.embed.jasper)

testImplementation(projects.sentrySystemTestSupport)
testImplementation(libs.kotlin.test.junit)
testImplementation(libs.springboot.starter.test) {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = JavaVersion.VERSION_17.toString()
}
}

configure<SourceSetContainer> { test { java.srcDir("src/test/java") } }

tasks.register<Test>("systemTest").configure {
group = "verification"
description = "Runs the System tests"

outputs.upToDateWhen { false }

maxParallelForks = 1

// Cap JVM args per test
minHeapSize = "128m"
maxHeapSize = "1g"

filter { includeTestsMatching("io.sentry.systemtest*") }
}

tasks.named("test").configure {
require(this is Test)

filter { excludeTestsMatching("io.sentry.systemtest.*") }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.sentry.samples.spring;

import java.io.File;
import java.io.IOException;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

public class Main {

public static void main(String[] args) throws LifecycleException, IOException {
File webappsDirectory = new File("./tomcat.8080/webapps");
if (!webappsDirectory.exists()) {
boolean didCreateDirectories = webappsDirectory.mkdirs();
if (!didCreateDirectories) {
throw new RuntimeException(
"Failed to create directory required by Tomcat: " + webappsDirectory.getAbsolutePath());
}
}

String pathToWar = "./build/libs";
String warName = "sentry-samples-spring-0.0.1-SNAPSHOT";
File war = new File(pathToWar + "/" + warName + ".war");

Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
tomcat.getConnector();

tomcat.addWebapp("/" + warName, war.getCanonicalPath());
tomcat.start();
tomcat.getServer().await();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package io.sentry.samples.spring.web;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {
private final String firstName;
private final String lastName;

public Person(String firstName, String lastName) {
@JsonCreator
public Person(
@JsonProperty("firstName") String firstName, @JsonProperty("lastName") String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public PersonController(PersonService personService) {
}

@GetMapping("{id}")
Person person(@PathVariable Long id) {
Person person(@PathVariable("id") Long id) {
Sentry.logger().warn("warn Sentry logging");
Sentry.logger().error("error Sentry logging");
Sentry.logger().info("hello %s %s", "there", "world!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.sentry

import kotlin.test.Test
import kotlin.test.assertTrue

class DummyTest {
@Test
fun `the only test`() {
// only needed to have more than 0 tests and not fail the build
assertTrue(true)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.sentry.systemtest

import io.sentry.systemtest.util.TestHelper
import kotlin.test.Test
import kotlin.test.assertEquals
import org.junit.Before

class PersonSystemTest {
lateinit var testHelper: TestHelper

@Before
fun setup() {
testHelper = TestHelper("http://localhost:8080/sentry-samples-spring-0.0.1-SNAPSHOT")
testHelper.reset()
}

@Test
fun `get person fails`() {
val restClient = testHelper.restClient
restClient.getPerson(11L)
assertEquals(500, restClient.lastKnownStatusCode)

testHelper.ensureTransactionReceived { transaction, envelopeHeader ->
testHelper.doesTransactionHaveOp(transaction, "http.server")
}

Thread.sleep(10000)

testHelper.ensureLogsReceived { logs, envelopeHeader ->
testHelper.doesContainLogWithBody(logs, "warn Sentry logging") &&
testHelper.doesContainLogWithBody(logs, "error Sentry logging") &&
testHelper.doesContainLogWithBody(logs, "hello there world!")
}
}

@Test
fun `create person works`() {
val restClient = testHelper.restClient
val person = Person("firstA", "lastB")
val returnedPerson = restClient.createPerson(person)
assertEquals(200, restClient.lastKnownStatusCode)

assertEquals(person.firstName, returnedPerson!!.firstName)
assertEquals(person.lastName, returnedPerson!!.lastName)
}
}
1 change: 1 addition & 0 deletions test/system-test-runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ def run_manual_test_mode(self, sample_module: str, java_agent: str,
def get_available_modules(self) -> List[ModuleConfig]:
"""Get list of all available test modules."""
return [
ModuleConfig("sentry-samples-spring", "false", "true", "false"),
ModuleConfig("sentry-samples-spring-jakarta", "false", "true", "false"),
ModuleConfig("sentry-samples-spring-boot", "false", "true", "false"),
ModuleConfig("sentry-samples-spring-boot-opentelemetry-noagent", "false", "true", "false"),
Expand Down
Loading