Skip to content

Commit fdbe1bd

Browse files
Alex Vargaspivovarit
authored andcommitted
Example of article "WebAppConfiguration in Spring Tests" (eugenp#633)
* An example of a test using a WebAppConfiguration annotation * Giving the test the appropriate formatting * A example of a test that uses WebAppConfiguration without the need of a MockMvc object
1 parent 19ee1fe commit fdbe1bd

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.web.controller;
2+
3+
import org.junit.Before;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.web.WebAppConfiguration;
11+
12+
import com.baeldung.model.Employee;
13+
import com.baeldung.spring.web.config.WebConfig;
14+
15+
@RunWith(SpringJUnit4ClassRunner.class)
16+
@WebAppConfiguration
17+
@ContextConfiguration(classes = WebConfig.class)
18+
public class EmployeeTestWithoutMockMvc {
19+
20+
@Autowired
21+
private EmployeeController employeeController;
22+
23+
@Before
24+
public void setup() {
25+
employeeController.initEmployees();
26+
}
27+
28+
@Test
29+
public void whenInitEmployees_thenVerifyValuesInitiation() {
30+
31+
Employee employee1 = employeeController.employeeMap.get(1L);
32+
Employee employee2 = employeeController.employeeMap.get(2L);
33+
Employee employee3 = employeeController.employeeMap.get(3L);
34+
35+
Assert.assertTrue(employee1.getId() == 1L);
36+
Assert.assertTrue(employee1.getName().equals("John"));
37+
Assert.assertTrue(employee1.getContactNumber().equals("223334411"));
38+
Assert.assertTrue(employee1.getWorkingArea().equals("rh"));
39+
40+
Assert.assertTrue(employee2.getId() == 2L);
41+
Assert.assertTrue(employee2.getName().equals("Peter"));
42+
Assert.assertTrue(employee2.getContactNumber().equals("22001543"));
43+
Assert.assertTrue(employee2.getWorkingArea().equals("informatics"));
44+
45+
Assert.assertTrue(employee3.getId() == 3L);
46+
Assert.assertTrue(employee3.getName().equals("Mike"));
47+
Assert.assertTrue(employee3.getContactNumber().equals("223334411"));
48+
Assert.assertTrue(employee3.getWorkingArea().equals("admin"));
49+
}
50+
51+
}

0 commit comments

Comments
 (0)