Add junit tests

This commit is contained in:
Anton Romanov 2025-05-21 11:56:15 +04:00
parent 99af17cacc
commit dc0fffb3a2
4 changed files with 98 additions and 5 deletions

View File

@ -14,11 +14,9 @@ java {
} }
repositories { repositories {
maven { mavenCentral()
url = uri("http://repo.athene.tech/repository/maven-central/")
allowInsecureProtocol(true)
}
} }
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
@ -47,10 +45,11 @@ dependencies {
implementation group: 'org.webjars', name: 'bootstrap-glyphicons', version: 'bdd2cbfba0' implementation group: 'org.webjars', name: 'bootstrap-glyphicons', version: 'bdd2cbfba0'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation("org.springframework.security:spring-security-test")
testRuntimeOnly 'org.junit.platform:junit-platform-launcher' testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
} }
tasks.named('test') { test {
useJUnitPlatform() useJUnitPlatform()
} }

View File

@ -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"));
}
}

View File

@ -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("Период отчетности")));
}
}

View File

@ -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();
}
}