-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathBeerControllerTest.java
More file actions
66 lines (56 loc) · 2.36 KB
/
BeerControllerTest.java
File metadata and controls
66 lines (56 loc) · 2.36 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
package com.example.beer;
import com.example.DemoApplication;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Test class for the Beer REST controller.
*
* @see BeerController
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class BeerControllerTest {
@Autowired
private BeerRepository beerRepository;
@Autowired
private EntityManager em;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
BeerController beerController = new BeerController(beerRepository);
this.mockMvc = MockMvcBuilders.standaloneSetup(beerController).build();
}
@Test
@Transactional
public void getGoodBeers() throws Exception {
// Initialize the database
Beer beer1 = new Beer("White Rascal");
Beer beer2 = new Beer("Budweiser");
beerRepository.saveAndFlush(beer1);
beerRepository.saveAndFlush(beer2);
// Get all the blogList
mockMvc.perform(get("/good-beers"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(beer1.getId().intValue())))
.andExpect(jsonPath("$.[*].name").value(hasItem(beer1.getName())))
.andExpect(jsonPath("$.[*].name").value(not(beer2.getName())));
}
}