-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathBeerControllerIT.java
More file actions
78 lines (64 loc) · 2.78 KB
/
BeerControllerIT.java
File metadata and controls
78 lines (64 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package guru.sfg.brewery.web.controllers;
import guru.sfg.brewery.repositories.BeerInventoryRepository;
import guru.sfg.brewery.repositories.BeerRepository;
import guru.sfg.brewery.repositories.CustomerRepository;
import guru.sfg.brewery.services.BeerService;
import guru.sfg.brewery.services.BreweryService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Created by jt on 6/12/20.
*/
@WebMvcTest
public class BeerControllerIT {
// WebApplicationContext will allow us to leverage filters used by Spring Security in order to test authentication
@Autowired
WebApplicationContext wac;
MockMvc mockMvc;
@MockBean
BeerRepository beerRepository;
@MockBean
BeerInventoryRepository beerInventoryRepository;
@MockBean
BreweryService breweryService;
@MockBean
CustomerRepository customerRepository;
@MockBean
BeerService beerService;
@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
// activates spring security filters
.apply(springSecurity())
.build();
}
// Здесь просто создаем мокового юзера с username spring.
@WithMockUser("spring")
@Test
void findBeers() throws Exception{
mockMvc.perform(get("/beers/find"))
.andExpect(status().isOk())
.andExpect(view().name("beers/findBeers"))
.andExpect(model().attributeExists("beer"));
}
// Здесь с помощью метода with() теперь проверяем базовую аутентификацию.
@Test
void findBeersWithHttpBasic() throws Exception{
mockMvc.perform(get("/beers/find").with(httpBasic("spring", "guru")))
.andExpect(status().isOk())
.andExpect(view().name("beers/findBeers"))
.andExpect(model().attributeExists("beer"));
}
}