|
| 1 | +package io.github.Hattinger04; |
| 2 | + |
| 3 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 4 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 5 | + |
| 6 | +import org.junit.Before; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.runner.RunWith; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 11 | +import org.springframework.boot.autoconfigure.domain.EntityScan; |
| 12 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 13 | +import org.springframework.boot.test.context.SpringBootTest; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 15 | +import org.springframework.boot.web.server.LocalServerPort; |
| 16 | +import org.springframework.context.annotation.ComponentScan; |
| 17 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 18 | +import org.springframework.http.HttpStatus; |
| 19 | +import org.springframework.security.test.context.support.WithMockUser; |
| 20 | +import org.springframework.test.context.junit4.SpringRunner; |
| 21 | +import org.springframework.test.web.servlet.MockMvc; |
| 22 | +import org.springframework.test.web.servlet.MvcResult; |
| 23 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 24 | + |
| 25 | +import io.github.Hattinger04.user.UserController; |
| 26 | + |
| 27 | +@RunWith(SpringRunner.class) |
| 28 | +@EntityScan("io.github.Hattinger04.*") |
| 29 | +@ComponentScan(basePackages = { "io.github.Hattinger04.*" }) |
| 30 | +@EnableJpaRepositories(basePackages = "io.github.Hattinger04.*") |
| 31 | +@AutoConfigureMockMvc |
| 32 | +@EnableAutoConfiguration |
| 33 | + |
| 34 | +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = { UserController.class }) |
| 35 | +class UserControllerTest { |
| 36 | + |
| 37 | + @LocalServerPort |
| 38 | + private int port; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private MockMvc mockMvc; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setup() { |
| 45 | + this.mockMvc = MockMvcBuilders.standaloneSetup(new UserController()).build(); |
| 46 | + } |
| 47 | + |
| 48 | + public void output(int status) { |
| 49 | + if (status == HttpStatus.OK.value()) { |
| 50 | + System.out.println("\033[32mTest passed!\033[0m"); |
| 51 | + } else { |
| 52 | + System.out.println("\033[31mTest failed!\033[0m"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @WithMockUser(authorities = "ADMIN") |
| 58 | + public void testGetUsers() throws Exception { |
| 59 | + MvcResult result = mockMvc.perform(get("https://localhost:" + port + "/user/users")) |
| 60 | + .andExpect(status().is(HttpStatus.OK.value())).andReturn(); |
| 61 | + |
| 62 | + output(result.getResponse().getStatus()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @WithMockUser(authorities = "ADMIN") |
| 67 | + public void testGetFirstUserByID() throws Exception { |
| 68 | + MvcResult result = mockMvc.perform(get("https://localhost:" + port + "/user/users/1")) |
| 69 | + .andExpect(status().is(HttpStatus.OK.value())).andReturn(); |
| 70 | + output(result.getResponse().getStatus()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @WithMockUser(authorities = "ADMIN") |
| 75 | + public void testGetAdminUserByUsername() throws Exception { |
| 76 | + MvcResult result = mockMvc.perform(get("https://localhost:" + port + "/user/users?username=admin")) |
| 77 | + .andExpect(status().is(HttpStatus.OK.value())).andReturn(); |
| 78 | + output(result.getResponse().getStatus()); |
| 79 | + } |
| 80 | +} |
0 commit comments