Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions account-jsp/src/test/java/com/bobocode/AccountControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,36 @@
import org.springframework.web.context.WebApplicationContext;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@SpringJUnitWebConfig(classes = {RootConfig.class, WebConfig.class})
public class AccountControllerTest {
class AccountControllerTest {
@Autowired
private WebApplicationContext applicationContext;

private MockMvc mockMvc;

@BeforeEach
public void setup() {
void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
}

@Test
public void testAccountControllerAnnotation() {
void testAccountControllerAnnotation() {
Controller controller = AccountController.class.getAnnotation(Controller.class);

assertThat(controller, notNullValue());
}

@Test
public void testAccountControllerRequestMapping() {
void testAccountControllerRequestMapping() {
RequestMapping requestMapping = AccountController.class.getAnnotation(RequestMapping.class);

assertThat(requestMapping, notNullValue());
Expand All @@ -48,30 +52,30 @@ public void testAccountControllerRequestMapping() {
}

@Test
public void testGetAccountsResponseStatusCode() throws Exception {
void testGetAccountsResponseStatusCode() throws Exception {
mockMvc.perform(get("/accounts")).andExpect(status().isOk());
}

@Test
public void testGetAccountsViewName() throws Exception {
void testGetAccountsViewName() throws Exception {
mockMvc.perform(get("/accounts"))
.andExpect(view().name("accounts"));
}

@Test
public void testGetAccountsModelContainsAccountList() throws Exception {
void testGetAccountsModelContainsAccountList() throws Exception {
mockMvc.perform(get("/accounts"))
.andExpect(model().attributeExists("accountList"));
}

@Test
public void testAccountsResponseDefaultListSize() throws Exception {
void testAccountsResponseDefaultListSize() throws Exception {
mockMvc.perform(get("/accounts"))
.andExpect(model().attribute("accountList", hasSize(10)));
}

@Test
public void testAccountsResponseListSize() throws Exception {
void testAccountsResponseListSize() throws Exception {
mockMvc.perform(get("/accounts").param("size", "20"))
.andExpect(model().attribute("accountList", hasSize(20)));
}
Expand Down
34 changes: 19 additions & 15 deletions account-jsp/src/test/java/com/bobocode/WebAppConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,62 @@
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

@SpringJUnitConfig(classes = RootConfig.class)
public class WebAppConfigTest {
class WebAppConfigTest {

@Autowired
private ApplicationContext applicationContext;

@Test
public void testDispatcherServletMapping() {
void testDispatcherServletMapping() {
WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper();

assertThat(webAppInitializerWrapper.getServletMappings(), arrayContaining("/"));
}

@Test
public void testInitializerRootConfigClasses() {
void testInitializerRootConfigClasses() {
WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper();

assertThat(webAppInitializerWrapper.getRootConfigClasses(), arrayContaining(RootConfig.class));
}

@Test
public void testInitializerWebConfigClasses() {
void testInitializerWebConfigClasses() {
WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper();

assertThat(webAppInitializerWrapper.getServletConfigClasses(), arrayContaining(WebConfig.class));
}

@Test
public void testRootConfigClassIsMarkedAsConfiguration() {
void testRootConfigClassIsMarkedAsConfiguration() {
Configuration configuration = RootConfig.class.getAnnotation(Configuration.class);

assertThat(configuration, notNullValue());
}

@Test
public void testRootConfigClassEnablesComponentScan() {
void testRootConfigClassEnablesComponentScan() {
ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class);

assertThat(componentScan, notNullValue());
}

@Test
public void testRootConfigComponentScanPackages() {
void testRootConfigComponentScanPackages() {
ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class);

assertThat(componentScan.basePackages(), arrayContaining("com.bobocode"));
}

@Test
public void testRootConfigComponentScanFilters() {
void testRootConfigComponentScanFilters() {
ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class);
Filter[] filters = componentScan.excludeFilters();
List<Class> filteredClasses = getFilteredClasses(filters);
Expand All @@ -88,42 +92,42 @@ private List<Class> getFilteredClasses(Filter[] filters) {
}

@Test
public void testWebConfigIsMarkedAsConfiguration() {
void testWebConfigIsMarkedAsConfiguration() {
Configuration configuration = WebConfig.class.getAnnotation(Configuration.class);

assertThat(configuration, notNullValue());
}

@Test
public void testWebConfigEnablesComponentScan() {
void testWebConfigEnablesComponentScan() {
ComponentScan componentScan = WebConfig.class.getAnnotation(ComponentScan.class);

assertThat(componentScan, notNullValue());
}

@Test
public void testWebConfigComponentScanPackages() {
void testWebConfigComponentScanPackages() {
ComponentScan componentScan = WebConfig.class.getAnnotation(ComponentScan.class);

assertThat(componentScan.value(), arrayContaining("com.bobocode.web"));
}

@Test
public void testWebConfigEnablesWebMvc() {
void testWebConfigEnablesWebMvc() {
EnableWebMvc enableWebMvc = WebConfig.class.getAnnotation(EnableWebMvc.class);

assertThat(enableWebMvc, notNullValue());
}

@Test
public void testDataGeneratorBeanName() {
void testDataGeneratorBeanName() {
TestDataGenerator dataGenerator = applicationContext.getBean("dataGenerator", TestDataGenerator.class);

assertThat(dataGenerator, notNullValue());
}

@Test
public void testDataGeneratorBeanNameIsNotSpecifiedExplicitly() {
void testDataGeneratorBeanNameIsNotSpecifiedExplicitly() {
Method[] methods = RootConfig.class.getMethods();
Method testDataGeneratorBeanMethod = findTestDataGeneratorBeanMethod(methods);
Bean bean = testDataGeneratorBeanMethod.getDeclaredAnnotation(Bean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@SpringJUnitWebConfig(classes = {RootConfig.class, WebConfig.class})
public class WelcomeControllerTest {
class WelcomeControllerTest {
@Autowired
private WebApplicationContext applicationContext;

private MockMvc mockMvc;

@BeforeEach
public void setup() {
void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
}

@Test
public void testWelcomeUrl() throws Exception {
void testWelcomeUrl() throws Exception {
mockMvc.perform(get("/welcome"))
.andExpect(status().isOk());
}

@Test
public void testRootUrl() throws Exception {
void testRootUrl() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk());
}

@Test
public void testWelcomeViewName() throws Exception {
void testWelcomeViewName() throws Exception {
mockMvc.perform(get("/"))
.andExpect(view().name("welcome"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
import org.springframework.web.context.WebApplicationContext;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringJUnitWebConfig(classes = {RootConfig.class, WebConfig.class})
public class AccountRestControllerTest {
class AccountRestControllerTest {
@Autowired
private WebApplicationContext applicationContext;

Expand All @@ -34,20 +39,20 @@ public class AccountRestControllerTest {
private MockMvc mockMvc;

@BeforeEach
public void setup() {
void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
accountDao.clear();
}

@Test
public void testAccountRestControllerAnnotation() {
void testAccountRestControllerAnnotation() {
RestController restController = AccountRestController.class.getAnnotation(RestController.class);

assertThat(restController, notNullValue());
}

@Test
public void testAccountRestControllerRequestMapping() {
void testAccountRestControllerRequestMapping() {
RequestMapping requestMapping = AccountRestController.class.getAnnotation(RequestMapping.class);

assertThat(requestMapping, notNullValue());
Expand All @@ -56,7 +61,7 @@ public void testAccountRestControllerRequestMapping() {
}

@Test
public void testHttpStatusCodeOnCreate() throws Exception {
void testHttpStatusCodeOnCreate() throws Exception {
mockMvc.perform(
post("/accounts")
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -65,7 +70,7 @@ public void testHttpStatusCodeOnCreate() throws Exception {
}

@Test
public void testCreateAccountReturnsAssignedId() throws Exception {
void testCreateAccountReturnsAssignedId() throws Exception {
mockMvc.perform(
post("/accounts")
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -74,13 +79,13 @@ public void testCreateAccountReturnsAssignedId() throws Exception {
}

@Test
public void testGetAccountsResponseStatusCode() throws Exception {
void testGetAccountsResponseStatusCode() throws Exception {
mockMvc.perform(get("/accounts").accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
}

@Test
public void testGetAllAccounts() throws Exception {
void testGetAllAccounts() throws Exception {
Account account1 = create("Johnny", "Boy", "jboy@gmail.com");
Account account2 = create("Okko", "Bay", "obay@gmail.com");
accountDao.save(account1);
Expand All @@ -91,7 +96,7 @@ public void testGetAllAccounts() throws Exception {
.andExpect(jsonPath("$.[*].email").value(hasItems("jboy@gmail.com", "obay@gmail.com")));
}

private Account create(String firstName, String lastName, String email){
private Account create(String firstName, String lastName, String email) {
Account account = new Account();
account.setFirstName(firstName);
account.setLastName(lastName);
Expand All @@ -100,7 +105,7 @@ private Account create(String firstName, String lastName, String email){
}

@Test
public void testGetById() throws Exception {
void testGetById() throws Exception {
Account account = create("Johnny", "Boy", "jboy@gmail.com");
accountDao.save(account);

Expand All @@ -113,7 +118,7 @@ public void testGetById() throws Exception {
}

@Test
public void testRemoveAccount() throws Exception {
void testRemoveAccount() throws Exception {
Account account = create("Johnny", "Boy", "jboy@gmail.com");
accountDao.save(account);

Expand All @@ -122,7 +127,7 @@ public void testRemoveAccount() throws Exception {
}

@Test
public void testUpdateAccount() throws Exception {
void testUpdateAccount() throws Exception {
Account account = create("Johnny", "Boy", "jboy@gmail.com");
accountDao.save(account);

Expand Down
Loading