Skip to content

Commit

Permalink
Trying to add controller test to workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
Niaktes committed Nov 15, 2023
1 parent 1ade0c0 commit 260fdb3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/cars/configuration/ControllerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cars.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@Configuration
public class ControllerConfig {

@Autowired
private WebApplicationContext context;

@Bean
public MockMvc mockMvc() {
return MockMvcBuilders.webAppContextSetup(context).build();
}

}
30 changes: 30 additions & 0 deletions src/test/java/cars/controller/IndexControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cars.controller;

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 ru.job4j.cars.Main;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@SpringBootTest(classes = Main.class)
@AutoConfigureMockMvc
class IndexControllerTest {

@Autowired
private MockMvc mockMvc;

@Test
void whenGetRequestIndexThenReturnAllPostsPage() throws Exception {
this.mockMvc.perform(get("/index"))
.andDo(print())
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/posts/all"));
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
image.directory=files/images

##boot
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp

#datasource
datasource.url=jdbc:h2:./testdb;MODE=PostgreSQL;CASE_INSENSITIVE_IDENTIFIERS=TRUE;
datasource.username=
datasource.password=

0 comments on commit 260fdb3

Please sign in to comment.