Skip to content

Commit 664e86c

Browse files
committed
create tests
1 parent 35b8eb9 commit 664e86c

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dependencies {
2222
testImplementation(platform("org.junit:junit-bom:5.11.4"))
2323
testImplementation("org.junit.jupiter:junit-jupiter")
2424

25-
implementation("io.javalin:javalin:6.7.0")
25+
implementation("io.javalin:javalin-bundle:6.7.0")
2626
implementation("org.slf4j:slf4j-simple:2.0.17")
2727
implementation("io.javalin:javalin-rendering:6.7.0")
2828
implementation("gg.jte:jte:3.2.1")

app/src/main/java/hexlet/code/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static TemplateEngine createTemplateEngine() {
5858
return templateEngine;
5959
}
6060

61-
private static Javalin getApp() throws SQLException, IOException {
61+
protected static Javalin getApp() throws SQLException, IOException {
6262
var hikariConfig = new HikariConfig();
6363
hikariConfig.setJdbcUrl(getDatabaseUrl());
6464
var dataSource = new HikariDataSource(hikariConfig);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package hexlet.code;
2+
3+
import hexlet.code.model.Url;
4+
import hexlet.code.repository.UrlRepository;
5+
import hexlet.code.util.NamedRoutes;
6+
import io.javalin.Javalin;
7+
import io.javalin.testtools.JavalinTest;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.io.IOException;
12+
import java.sql.SQLException;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
17+
public final class AppTest {
18+
private Javalin app;
19+
20+
@BeforeEach
21+
public void setUp() throws SQLException, IOException {
22+
app = App.getApp();
23+
}
24+
25+
@Test
26+
public void testMainPage() {
27+
JavalinTest.test(app, (server, client) -> {
28+
var response = client.get(NamedRoutes.rootPath());
29+
assertEquals(200, response.code());
30+
assertTrue((response.body().string()).contains("Главная страница"));
31+
});
32+
}
33+
34+
@Test
35+
public void testUrlsPage() {
36+
JavalinTest.test(app, (server, client) -> {
37+
var response = client.get(NamedRoutes.urlsPath());
38+
assertEquals(200, response.code());
39+
assertTrue((response.body().string()).contains("Добавленные сайты"));
40+
});
41+
}
42+
43+
@Test
44+
public void testUrlPage() {
45+
JavalinTest.test(app, (server, client) -> {
46+
47+
var url1 = new Url("https://one.com");
48+
var url2 = new Url("https://two.com:7070");
49+
50+
UrlRepository.save(url1);
51+
UrlRepository.save(url2);
52+
53+
var response1 = client.get(NamedRoutes.urlPath(url1.getId()));
54+
var response2 = client.get(NamedRoutes.urlPath(url2.getId()));
55+
56+
assertEquals(200, response1.code());
57+
assertTrue((response1.body().string()).contains("Сайт"));
58+
59+
assertEquals(200, response2.code());
60+
assertTrue((response2.body().string()).contains("Сайт"));
61+
});
62+
}
63+
64+
@Test
65+
void testUrlNotFound() {
66+
JavalinTest.test(app, (server, client) -> {
67+
var response = client.get("/urls/1");
68+
assertEquals(404, response.code());
69+
});
70+
}
71+
}

0 commit comments

Comments
 (0)