Skip to content

Commit a131006

Browse files
committed
improve tests
Signed-off-by: Kirill Mokevnin <mokevnin@gmail.com>
1 parent 7044c72 commit a131006

File tree

6 files changed

+74
-117
lines changed

6 files changed

+74
-117
lines changed

.github/workflows/java.yml

+9-41
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,22 @@
11
name: Java CI
22

33
on:
4-
push:
5-
branches:
6-
- main
7-
pull_request:
8-
branches:
9-
- main
4+
- push
5+
- pull_request
106

117
jobs:
128
build:
139
runs-on: ubuntu-latest
1410

1511
steps:
16-
- uses: actions/checkout@v2
17-
- uses: actions/setup-java@v2
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-java@v3
1814
with:
15+
java-version: '20'
1916
distribution: 'temurin'
20-
java-version: '17'
21-
- name: Build with Gradle
22-
run: make build
23-
- name: Publish code coverage
24-
uses: paambaati/codeclimate-action@v3.0.0
25-
env:
26-
CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
27-
JACOCO_SOURCE_PATH: src/main/java
17+
- uses: gradle/gradle-build-action@v2
2818
with:
29-
coverageCommand: make report
30-
coverageLocations: ${{github.workspace}}/build/reports/jacoco/test/jacocoTestReport.xml:jacoco
19+
gradle-version: 8.3
20+
- run: ./gradlew checkstyleMain
21+
- run: ./gradlew test
3122

32-
33-
deploy:
34-
needs: build
35-
runs-on: ubuntu-latest
36-
if: ${{ github.event_name == 'push' }}
37-
38-
steps:
39-
- uses: actions/checkout@v2
40-
41-
- uses: docker/setup-buildx-action@v1
42-
43-
- uses: docker/login-action@v1
44-
with:
45-
username: ${{ secrets.DOCKER_USERNAME }}
46-
password: ${{ secrets.DOCKER_PASSWORD }}
47-
48-
- uses: docker/build-push-action@v2
49-
with:
50-
context: .
51-
push: true
52-
cache-from: hexletcomponents/java-servlet-gradle:latest
53-
cache-to: type=inline
54-
tags: hexletcomponents/java-servlet-gradle:latest

build.gradle.kts

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
2+
import org.gradle.api.tasks.testing.logging.TestLogEvent
3+
14
plugins {
25
id("java")
36
id("war")
@@ -20,25 +23,33 @@ dependencies {
2023
implementation("jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api:3.0.0")
2124
implementation("org.glassfish.web:jakarta.servlet.jsp.jstl:3.0.1")
2225

23-
// implementation("jakarta.servlet.jsp:jakarta.servlet.jsp-api:3.1.1")
24-
// implementation("ch.qos.logback:logback-access:1.4.11")
25-
implementation("org.slf4j:slf4j-api:2.0.7")
2626
implementation("org.slf4j:slf4j-simple:2.0.7")
2727

2828
implementation("org.zalando:logbook-core:3.3.0")
2929
implementation("org.zalando:logbook-servlet:3.3.0")
3030

31-
// testImplementation(platform("org.junit:junit-bom:5.10.0"))
32-
// testImplementation("org.junit.jupiter:junit-jupiter")
31+
testImplementation("com.konghq:unirest-java-core:4.0.4")
32+
testImplementation("com.konghq:unirest-java-bom:4.0.4")
33+
34+
testImplementation("org.assertj:assertj-core:3.23.1")
35+
testImplementation(platform("org.junit:junit-bom:5.10.0"))
36+
testImplementation("org.junit.jupiter:junit-jupiter")
3337
}
3438

3539
gretty {
36-
// servletContainer = "jetty9"
37-
// httpsEnabled = true
38-
// managedClassReload = true
40+
integrationTestTask = "test"
3941
contextPath = '/'
40-
// loggingLevel = ""
41-
// managedClassReload = true
42-
// loggingLevel = 'ALL'
43-
// debug = true
42+
servletContainer = "tomcat10"
43+
}
44+
45+
tasks.test {
46+
useJUnitPlatform()
47+
// https://technology.lastminute.com/junit5-kotlin-and-gradle-dsl/
48+
testLogging {
49+
exceptionFormat = TestExceptionFormat.FULL
50+
events = mutableSetOf(TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED)
51+
// showStackTraces = true
52+
// showCauses = true
53+
showStandardStreams = true
54+
}
4455
}

src/main/java/io/hexlet/servlet/MainServlet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class MainServlet extends HttpServlet {
1515

1616
@Override
1717
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
18-
log.info("TEST");
18+
log.info("TEST!");
1919
resp.setContentType("text/html");
2020
var writer = resp.getWriter();
2121
writer.println("<html><body>");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.slf4j.simpleLogger.defaultLogLevel=trace

src/main/webapp/WEB-INF/web.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
5-
version="6.0">
4+
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
5+
version="5.0">
66

77
<filter>
88
<filter-name>LogbookFilter</filter-name>

src/test/java/io/hexlet/AppTest.java

+38-61
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,38 @@
1-
// package io.hexlet;
2-
//
3-
// import org.junit.jupiter.api.BeforeAll;
4-
// import org.junit.jupiter.api.AfterAll;
5-
// import org.junit.jupiter.api.Test;
6-
//
7-
// import static org.assertj.core.api.Assertions.assertThat;
8-
//
9-
// import org.apache.catalina.startup.Tomcat;
10-
// import org.apache.catalina.LifecycleException;
11-
// import kong.unirest.HttpResponse;
12-
// import kong.unirest.Unirest;
13-
//
14-
// class AppTest {
15-
//
16-
// private static Tomcat app;
17-
// private static String baseUrl;
18-
//
19-
// @BeforeAll
20-
// public static void beforeAll() throws LifecycleException {
21-
// app = App.getApp(0);
22-
// app.start();
23-
// int port = app.getConnector().getLocalPort();
24-
// String hostname = app.getHost().getName();
25-
// String scheme = app.getConnector().getScheme();
26-
// baseUrl = scheme + "://" + hostname + ":" + port;
27-
// }
28-
//
29-
// @AfterAll
30-
// public static void afterAll() throws LifecycleException {
31-
// app.stop();
32-
// }
33-
//
34-
// @Test
35-
// void testHelloWorld() {
36-
// HttpResponse<String> response = Unirest
37-
// .get(baseUrl)
38-
// .asString();
39-
// assertThat(response.getStatus()).isEqualTo(200);
40-
// assertThat(response.getBody()).contains("Hello World");
41-
// }
42-
//
43-
// @Test
44-
// void testGreetingWithoutName() {
45-
// HttpResponse<String> response = Unirest
46-
// .get(baseUrl + "/greeting")
47-
// .asString();
48-
// assertThat(response.getStatus()).isEqualTo(200);
49-
// assertThat(response.getBody()).contains("What is your name?");
50-
// }
51-
//
52-
// @Test
53-
// void testGreetingWithName() {
54-
// HttpResponse<String> response = Unirest
55-
// .get(baseUrl + "/greeting")
56-
// .queryString("name", "Tirion")
57-
// .asString();
58-
// assertThat(response.getStatus()).isEqualTo(200);
59-
// assertThat(response.getBody()).contains("Your name is Tirion");
60-
// }
61-
// }
1+
package io.hexlet;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
8+
import kong.unirest.core.Unirest;
9+
10+
class AppTest {
11+
static String baseUrl;
12+
13+
@BeforeAll
14+
static void setup() {
15+
baseUrl = System.getProperty("gretty.httpBaseURI");
16+
}
17+
18+
@Test
19+
void testMainPage() {
20+
var response = Unirest.get(baseUrl).asString();
21+
assertThat(response.getStatus()).isEqualTo(200);
22+
assertThat(response.getBody()).contains("users");
23+
}
24+
25+
@Test
26+
void testAboutPage() {
27+
var response = Unirest.get(baseUrl + "/about").asString();
28+
assertThat(response.getStatus()).isEqualTo(200);
29+
assertThat(response.getBody()).contains("Jakarta");
30+
}
31+
32+
@Test
33+
void testUsersPage() {
34+
var response = Unirest.get(baseUrl + "/users").asString();
35+
assertThat(response.getStatus()).isEqualTo(200);
36+
assertThat(response.getBody()).contains("Nina");
37+
}
38+
}

0 commit comments

Comments
 (0)