Skip to content

Commit 77f38de

Browse files
committed
fix integration test
1 parent 4eccbba commit 77f38de

4 files changed

Lines changed: 41 additions & 18 deletions

File tree

‎spring-boot/src/main/resources/schema.sql‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
drop table if exists USERS;
2+
13
create table USERS(
24
ID int not null AUTO_INCREMENT,
35
NAME varchar(100) not null,

‎spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import static org.hamcrest.Matchers.hasSize;
55
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
66

7+
import java.nio.charset.Charset;
8+
9+
import org.baeldung.config.H2JpaConfig;
710
import org.baeldung.domain.Modes;
811
import org.junit.Before;
912
import org.junit.Test;
@@ -19,10 +22,8 @@
1922
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2023
import org.springframework.web.context.WebApplicationContext;
2124

22-
import java.nio.charset.Charset;
23-
2425
@RunWith(SpringJUnit4ClassRunner.class)
25-
@SpringBootTest(classes = Application.class)
26+
@SpringBootTest(classes = { Application.class, H2JpaConfig.class })
2627
@WebAppConfiguration
2728
public class SpringBootApplicationIntegrationTest {
2829
@Autowired
@@ -31,36 +32,56 @@ public class SpringBootApplicationIntegrationTest {
3132

3233
@Before
3334
public void setupMockMvc() {
34-
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
35+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
36+
.build();
3537
}
3638

3739
@Test
3840
public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception {
39-
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
41+
final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
4042

41-
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$", hasSize(4)));
43+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all"))
44+
.andExpect(MockMvcResultMatchers.status()
45+
.isOk())
46+
.andExpect(MockMvcResultMatchers.content()
47+
.contentType(contentType))
48+
.andExpect(jsonPath("$", hasSize(4)));
4249
}
4350

4451
@Test
4552
public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect() throws Exception {
46-
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
53+
final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
4754

48-
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType))
49-
.andExpect(jsonPath("$.id", equalTo(1)));
55+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30"))
56+
.andExpect(MockMvcResultMatchers.status()
57+
.isOk())
58+
.andExpect(MockMvcResultMatchers.content()
59+
.contentType(contentType))
60+
.andExpect(jsonPath("$.id", equalTo(1)));
5061
}
5162

5263
@Test
5364
public void givenRequestHasBeenMade_whenMeetsFindByModeOfGivenConditions_thenCorrect() throws Exception {
54-
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
65+
final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
5566

56-
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name())).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$.id", equalTo(1)));
67+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name()))
68+
.andExpect(MockMvcResultMatchers.status()
69+
.isOk())
70+
.andExpect(MockMvcResultMatchers.content()
71+
.contentType(contentType))
72+
.andExpect(jsonPath("$.id", equalTo(1)));
5773
}
5874

5975
@Test
6076
public void givenRequestHasBeenMade_whenMeetsFindByVersionOfGivenConditions_thenCorrect() throws Exception {
61-
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
77+
final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
6278

63-
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType))
64-
.andExpect(jsonPath("$.id", equalTo(1)));
79+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion")
80+
.header("Version", "1.0.0"))
81+
.andExpect(MockMvcResultMatchers.status()
82+
.isOk())
83+
.andExpect(MockMvcResultMatchers.content()
84+
.contentType(contentType))
85+
.andExpect(jsonPath("$.id", equalTo(1)));
6586
}
6687
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Insert into Foo values(1,'Foo_Name');
1+
--insert into Foo values(1,'Foo_Name');
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
jdbc.driverClassName=org.h2.Driver
22
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
3-
jdbc.username=sa
4-
jdbc.password=sa
3+
jdbc.user=sa
4+
jdbc.pass=sa
55

66
hibernate.dialect=org.hibernate.dialect.H2Dialect
77
hibernate.show_sql=true
8-
hibernate.hbm2ddl.auto=create-drop
8+
hibernate.hbm2ddl.auto=create-drop

0 commit comments

Comments
 (0)