Skip to content

Commit 8f9bcec

Browse files
committed
Gradle: inject test resources before the main resources for testing
1 parent 5f89b66 commit 8f9bcec

File tree

14 files changed

+323
-5
lines changed

14 files changed

+323
-5
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.quarkus.gradle;
2+
3+
import java.io.File;
4+
import java.nio.file.Path;
5+
6+
import org.gradle.testkit.runner.BuildResult;
7+
import org.gradle.testkit.runner.GradleRunner;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
13+
public class TestResourcesVsMainResourcesTest extends QuarkusGradleTestBase {
14+
15+
@Test
16+
public void test() throws Exception {
17+
18+
final File projectDir = getProjectDir("test-resources-vs-main-resources");
19+
20+
BuildResult build = GradleRunner.create()
21+
.forwardOutput()
22+
.withPluginClasspath()
23+
.withArguments(arguments("test"))
24+
.withProjectDir(projectDir)
25+
.build();
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
plugins {
2+
id 'java'
3+
id 'io.quarkus'
4+
}
5+
6+
repositories {
7+
mavenLocal()
8+
mavenCentral()
9+
}
10+
11+
dependencies {
12+
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
13+
implementation 'io.quarkus:quarkus-resteasy'
14+
15+
testImplementation 'io.quarkus:quarkus-junit5'
16+
testImplementation 'io.rest-assured:rest-assured'
17+
}
18+
19+
group 'org.acme'
20+
version '1.0.0-SNAPSHOT'
21+
22+
compileJava {
23+
options.encoding = 'UTF-8'
24+
options.compilerArgs << '-parameters'
25+
}
26+
27+
compileTestJava {
28+
options.encoding = 'UTF-8'
29+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
quarkusPlatformArtifactId=quarkus-bom
2+
quarkusPlatformGroupId=io.quarkus
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pluginManagement {
2+
repositories {
3+
mavenLocal()
4+
mavenCentral()
5+
gradlePluginPortal()
6+
}
7+
plugins {
8+
id 'io.quarkus' version "${quarkusPluginVersion}"
9+
}
10+
}
11+
rootProject.name='code-with-quarkus'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.acme;
2+
3+
import io.quarkus.arc.config.ConfigProperties;
4+
5+
@ConfigProperties(prefix = "example")
6+
public class Config {
7+
public String message;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.acme;
2+
3+
import javax.inject.Inject;
4+
import javax.ws.rs.GET;
5+
import javax.ws.rs.Path;
6+
import javax.ws.rs.Produces;
7+
import javax.ws.rs.core.MediaType;
8+
9+
@Path("/hello")
10+
public class ExampleResource {
11+
12+
@Inject
13+
Config config;
14+
15+
@GET
16+
@Produces(MediaType.TEXT_PLAIN)
17+
public String hello() {
18+
return config.message;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.acme;
2+
3+
import javax.inject.Inject;
4+
import javax.ws.rs.GET;
5+
import javax.ws.rs.Path;
6+
import javax.ws.rs.Produces;
7+
import javax.ws.rs.core.MediaType;
8+
9+
import org.eclipse.microprofile.config.inject.ConfigProperty;
10+
11+
@Path("/test-only")
12+
public class TestOnlyResource {
13+
14+
@ConfigProperty(name = "test-only")
15+
String testOnly;
16+
17+
@GET
18+
@Produces(MediaType.TEXT_PLAIN)
19+
public String hello() {
20+
return testOnly;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>code-with-quarkus - 1.0.0-SNAPSHOT</title>
6+
<style>
7+
h1, h2, h3, h4, h5, h6 {
8+
margin-bottom: 0.5rem;
9+
font-weight: 400;
10+
line-height: 1.5;
11+
}
12+
13+
h1 {
14+
font-size: 2.5rem;
15+
}
16+
17+
h2 {
18+
font-size: 2rem
19+
}
20+
21+
h3 {
22+
font-size: 1.75rem
23+
}
24+
25+
h4 {
26+
font-size: 1.5rem
27+
}
28+
29+
h5 {
30+
font-size: 1.25rem
31+
}
32+
33+
h6 {
34+
font-size: 1rem
35+
}
36+
37+
.lead {
38+
font-weight: 300;
39+
font-size: 2rem;
40+
}
41+
42+
.banner {
43+
font-size: 2.7rem;
44+
margin: 0;
45+
padding: 2rem 1rem;
46+
background-color: #00A1E2;
47+
color: white;
48+
}
49+
50+
body {
51+
margin: 0;
52+
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";
53+
}
54+
55+
code {
56+
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
57+
font-size: 87.5%;
58+
color: #e83e8c;
59+
word-break: break-word;
60+
}
61+
62+
.left-column {
63+
padding: .75rem;
64+
max-width: 75%;
65+
min-width: 55%;
66+
}
67+
68+
.right-column {
69+
padding: .75rem;
70+
max-width: 25%;
71+
}
72+
73+
.container {
74+
display: flex;
75+
width: 100%;
76+
}
77+
78+
li {
79+
margin: 0.75rem;
80+
}
81+
82+
.right-section {
83+
margin-left: 1rem;
84+
padding-left: 0.5rem;
85+
}
86+
87+
.right-section h3 {
88+
padding-top: 0;
89+
font-weight: 200;
90+
}
91+
92+
.right-section ul {
93+
border-left: 0.3rem solid #00A1E2;
94+
list-style-type: none;
95+
padding-left: 0;
96+
}
97+
98+
</style>
99+
</head>
100+
<body>
101+
102+
<div class="banner lead">
103+
Your new Cloud-Native application is ready!
104+
</div>
105+
106+
<div class="container">
107+
<div class="left-column">
108+
<p class="lead"> Congratulations, you have created a new Quarkus application.</p>
109+
110+
<h2>Why do you see this?</h2>
111+
112+
<p>This page is served by Quarkus. The source is in
113+
<code>src/main/resources/META-INF/resources/index.html</code>.</p>
114+
115+
<h2>What can I do from here?</h2>
116+
117+
<p>If not already done, run the application in <em>dev mode</em> using: <code>mvn compile quarkus:dev</code>.
118+
</p>
119+
<ul>
120+
<li>Add REST resources, Servlets, functions and other services in <code>src/main/java</code>.</li>
121+
<li>Your static assets are located in <code>src/main/resources/META-INF/resources</code>.</li>
122+
<li>Configure your application in <code>src/main/resources/application.properties</code>.
123+
</li>
124+
</ul>
125+
126+
<h2>How do I get rid of this page?</h2>
127+
<p>Just delete the <code>src/main/resources/META-INF/resources/index.html</code> file.</p>
128+
</div>
129+
<div class="right-column">
130+
<div class="right-section">
131+
<h3>Application</h3>
132+
<ul>
133+
<li>GroupId: org.acme</li>
134+
<li>ArtifactId: code-with-quarkus</li>
135+
<li>Version: 1.0.0-SNAPSHOT</li>
136+
<li>Quarkus Version: 1.3.2.Final</li>
137+
</ul>
138+
</div>
139+
<div class="right-section">
140+
<h3>Next steps</h3>
141+
<ul>
142+
<li><a href="https://quarkus.io/guides/maven-tooling.html" target="_blank">Setup your IDE</a></li>
143+
<li><a href="https://quarkus.io/guides/getting-started.html" target="_blank">Getting started</a></li>
144+
<li><a href="https://quarkus.io" target="_blank">Quarkus Web Site</a></li>
145+
</ul>
146+
</div>
147+
</div>
148+
</div>
149+
150+
151+
</body>
152+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Configuration file
2+
# key = value
3+
example.message=${MY_PROD_ENV_VAR}
4+
app-only=App only
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.acme;
2+
3+
import io.quarkus.test.junit.QuarkusTest;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static io.restassured.RestAssured.given;
7+
import static org.hamcrest.CoreMatchers.is;
8+
9+
@QuarkusTest
10+
public class ExampleResourceTest {
11+
12+
@Test
13+
public void testHelloEndpoint() {
14+
given()
15+
.when().get("/hello")
16+
.then()
17+
.statusCode(200)
18+
.body(is("Hello from Test"));
19+
}
20+
21+
@Test
22+
public void testTestOnly() {
23+
given()
24+
.when().get("test-only")
25+
.then()
26+
.statusCode(200)
27+
.body(is("Test only"));
28+
}
29+
}

0 commit comments

Comments
 (0)