Skip to content

Commit

Permalink
Merge pull request #40773 from holly-cummins/test-template-tests
Browse files Browse the repository at this point in the history
Improve coverage for JUnit @testtemplate
  • Loading branch information
gastaldi authored May 22, 2024
2 parents 7fafd32 + da65f40 commit b51310c
Show file tree
Hide file tree
Showing 23 changed files with 824 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
Expand Down Expand Up @@ -588,6 +589,57 @@ public void testRestClientCustomHeadersExtension() throws MavenInvocationExcepti
assertThat(devModeClient.getHttpResponse("/app/frontend")).isEqualTo("CustomValue1 CustomValue2");
}

@Test
public void testThatJUnitTestTemplatesWork() throws MavenInvocationException, IOException {
//we also check continuous testing
testDir = initProject("projects/test-template", "projects/test-template-processed");
runAndCheck();

ContinuousTestingMavenTestUtils testingTestUtils = new ContinuousTestingMavenTestUtils();
ContinuousTestingMavenTestUtils.TestStatus results = testingTestUtils.waitForNextCompletion();

//check that the tests in both modules run
Assertions.assertEquals(2, results.getTestsPassed());

// Re-running the tests when changes happen is covered by testThatChangesTriggerRerunsOfJUnitTestTemplates
}

@Disabled("Not working; tracked by #40770")
@Test
public void testThatChangesTriggerRerunsOfJUnitTestTemplates() throws MavenInvocationException, IOException {
//we also check continuous testing
testDir = initProject("projects/test-template", "projects/test-template-processed");
runAndCheck();

ContinuousTestingMavenTestUtils testingTestUtils = new ContinuousTestingMavenTestUtils();
ContinuousTestingMavenTestUtils.TestStatus results = testingTestUtils.waitForNextCompletion();

//check that the tests in both modules run
Assertions.assertEquals(2, results.getTestsPassed());

// Edit the "Hello" message.
File source = new File(testDir, "src/main/java/org/acme/HelloResource.java");
final String uuid = UUID.randomUUID().toString();
filter(source, Collections.singletonMap("return \"hello\";", "return \"" + uuid + "\";"));

// Wait until we get "uuid"
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/app/hello").contains(uuid));

await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.pollInterval(1, TimeUnit.SECONDS)
.until(source::isFile);

results = testingTestUtils.waitForNextCompletion();

//make sure the test is failing now
Assertions.assertEquals(0, results.getTestsPassed());
Assertions.assertEquals(2, results.getTestsFailed());
}

@Test
public void testThatTheApplicationIsReloadedMultiModule() throws MavenInvocationException, IOException {
//we also check continuous testing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>org.acme</groupId>
<artifactId>quarkus-test-template</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.version>@project.version@</quarkus.platform.version>
<quarkus-plugin.version>@project.version@</quarkus-plugin.version>
<compiler-plugin.version>${compiler-plugin.version}</compiler-plugin.version>
<surefire-plugin.version>${version.surefire.plugin}</surefire-plugin.version>
<maven.compiler.source>${maven.compiler.source}</maven.compiler.source>
<maven.compiler.target>${maven.compiler.target}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>\${quarkus.platform.group-id}</groupId>
<artifactId>\${quarkus.platform.artifact-id}</artifactId>
<version>\${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>\${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>\${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>\${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>\${native.surefire.skip}</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>\${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>\${project.build.directory}/\${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>\${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.acme;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class HelloResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.acme;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath("/app")
public class MyApplication extends Application {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>getting-started - 1.0-SNAPSHOT</title>
<style>
h1, h2, h3, h4, h5, h6 {
margin-bottom: 0.5rem;
font-weight: 400;
line-height: 1.5;
}

h1 {
font-size: 2.5rem;
}

h2 {
font-size: 2rem
}

h3 {
font-size: 1.75rem
}

h4 {
font-size: 1.5rem
}

h5 {
font-size: 1.25rem
}

h6 {
font-size: 1rem
}

.lead {
font-weight: 300;
font-size: 2rem;
}

.banner {
font-size: 2.7rem;
margin: 0;
padding: 2rem 1rem;
background-color: #00A1E2;
color: white;
}

body {
margin: 0;
font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
}

code {
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
font-size: 87.5%;
color: #e83e8c;
word-break: break-word;
}

.left-column {
padding: .75rem;
max-width: 75%;
min-width: 55%;
}

.right-column {
padding: .75rem;
max-width: 25%;
}

.container {
display: flex;
width: 100%;
}

li {
margin: 0.75rem;
}

.right-section {
margin-left: 1rem;
padding-left: 0.5rem;
}

.right-section h3 {
padding-top: 0;
font-weight: 200;
}

.right-section ul {
border-left: 0.3rem solid #00A1E2;
list-style-type: none;
padding-left: 0;
}

</style>
</head>
<body>

<div class="banner lead">
Your new Cloud-Native app is ready!
</div>

<div class="container">
<div class="left-column">
<p class="lead"> Congratulations, you have created a new Quarkus application.</p>

<h2>Why do you see this?</h2>

<p>This page is served by Quarkus. The source is in
<code>src/main/resources/META-INF/resources/index.html</code>.</p>

<h2>What can I do from here?</h2>

<p>If not already done, run the application in <em>dev mode</em> using: <code>mvn compile quarkus:dev</code>.
</p>
<ul>
<li>Add REST resources, Servlets, functions and other services in <code>src/main/java</code>.</li>
<li>Your static assets are located in <code>src/main/resources/META-INF/resources</code>.</li>
<li>Configure your application in <code>src/main/resources/META-INF/microprofile-config.properties</code>.
</li>
</ul>

<h2>Do you like Quarkus?</h2>
<p>Go give it a star on <a href="https://github.com/quarkusio/quarkus">GitHub</a>.</p>

<h2>How do I get rid of this page?</h2>
<p>Just delete the <code>src/main/resources/META-INF/resources/index.html</code> file.</p>
</div>
<div class="right-column">
<div class="right-section">
<h3>Application</h3>
<ul>
<li>GroupId: org.acme</li>
<li>ArtifactId: getting-started</li>
<li>Version: 1.0-SNAPSHOT</li>
</ul>
</div>
<div class="right-section">
<h3>Next steps</h3>
<ul>
<li><a href="https://quarkus.io/guides/maven-tooling">Setup your IDE</a></li>
<li><a href="https://quarkus.io/guides/getting-started">Getting started</a></li>
<li><a href="https://quarkus.io">Quarkus Web Site</a></li>
</ul>
</div>
</div>
</div>


</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.test.continuous-testing=enabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.acme;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class TemplatedTest {

@TestTemplate
@ExtendWith(UserIdGeneratorTestInvocationContextProvider.class)
public void testHelloEndpoint(UserIdGeneratorTestCase testCase) {
given()
.when().get("/app/hello")
.then()
.statusCode(200)
.body(is(testCase.getExpectedBody()));
}
}
Loading

0 comments on commit b51310c

Please sign in to comment.