From dc0fffb3a24e2f6b2b450e470f33c62e77a8bef2 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 21 May 2025 11:56:15 +0400 Subject: [PATCH] Add junit tests --- build.gradle | 9 ++--- .../java/ru/ulstu/PeriodControllerTest.java | 40 +++++++++++++++++++ .../java/ru/ulstu/ReportControllerTest.java | 31 ++++++++++++++ src/test/java/ru/ulstu/ReportTest.java | 23 +++++++++++ 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 src/test/java/ru/ulstu/PeriodControllerTest.java create mode 100644 src/test/java/ru/ulstu/ReportControllerTest.java create mode 100644 src/test/java/ru/ulstu/ReportTest.java diff --git a/build.gradle b/build.gradle index eb41045..68c233b 100644 --- a/build.gradle +++ b/build.gradle @@ -14,11 +14,9 @@ java { } repositories { - maven { - url = uri("http://repo.athene.tech/repository/maven-central/") - allowInsecureProtocol(true) - } + mavenCentral() } + dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' @@ -47,10 +45,11 @@ dependencies { implementation group: 'org.webjars', name: 'bootstrap-glyphicons', version: 'bdd2cbfba0' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation("org.springframework.security:spring-security-test") testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } -tasks.named('test') { +test { useJUnitPlatform() } diff --git a/src/test/java/ru/ulstu/PeriodControllerTest.java b/src/test/java/ru/ulstu/PeriodControllerTest.java new file mode 100644 index 0000000..54f4200 --- /dev/null +++ b/src/test/java/ru/ulstu/PeriodControllerTest.java @@ -0,0 +1,40 @@ +package ru.ulstu; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; + +@SpringBootTest +@AutoConfigureMockMvc +public class PeriodControllerTest { + @Autowired() + private MockMvc mockMvc; + + @Test + /* + Тест сохранения нового периода отчетности + */ + void testAddPeriod() throws Exception { + String json = "{" + + "'startDate' : '2025-05-21'," + + "'endDate' : '2025-05-21'" + + "}"; + this.mockMvc.perform( + post("/admin/saveReportPeriod") + .with( + user("admin") + .password("admin")) + .content(json) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .param("save", "true")) + .andExpect(redirectedUrl("/admin/reportPeriodList")); + } +} diff --git a/src/test/java/ru/ulstu/ReportControllerTest.java b/src/test/java/ru/ulstu/ReportControllerTest.java new file mode 100644 index 0000000..8fe973e --- /dev/null +++ b/src/test/java/ru/ulstu/ReportControllerTest.java @@ -0,0 +1,31 @@ +package ru.ulstu; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; + +import static org.hamcrest.Matchers.containsString; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +class ReportControllerTest { + + @Autowired() + private MockMvc mockMvc; + + @Test + /* + Тест просмотра страницы + */ + void shouldReturnDefaultMessage() throws Exception { + this.mockMvc.perform(get("/report/reportList").with(user("admin").password("admin"))) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("Период отчетности"))); + } +} diff --git a/src/test/java/ru/ulstu/ReportTest.java b/src/test/java/ru/ulstu/ReportTest.java new file mode 100644 index 0000000..0a991b8 --- /dev/null +++ b/src/test/java/ru/ulstu/ReportTest.java @@ -0,0 +1,23 @@ +package ru.ulstu; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.ulstu.report.controller.ReportController; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +public class ReportTest { + + @Autowired + private ReportController reportController; + + @Test + /* + Тест наличия инициализированного контроллера + */ + void controllerIsPresent() throws Exception { + assertThat(reportController).isNotNull(); + } +} \ No newline at end of file