cleanup and testing work

This commit is contained in:
eugenp 2016-10-12 08:00:02 +03:00
parent 2aa2f43c6e
commit eb7650eead
24 changed files with 259 additions and 271 deletions

View File

@ -17,7 +17,7 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.13</version> <version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -34,7 +34,6 @@
<!-- <module>gatling</module> --> <!-- not meant to run as part of the standard build --> <!-- <module>gatling</module> --> <!-- not meant to run as part of the standard build -->
<module>gson</module> <module>gson</module>
<module>gson-jackson-performance</module>
<module>guava</module> <module>guava</module>
<module>guava18</module> <module>guava18</module>
<module>guava19</module> <module>guava19</module>

View File

@ -80,6 +80,11 @@
<artifactId>commons-fileupload</artifactId> <artifactId>commons-fileupload</artifactId>
<version>1.3.1</version> <version>1.3.1</version>
</dependency> </dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>${net.sourceforge.htmlunit}</version>
</dependency>
<!-- Thymeleaf --> <!-- Thymeleaf -->
<dependency> <dependency>
@ -196,7 +201,7 @@
<version>${maven-surefire-plugin.version}</version> <version>${maven-surefire-plugin.version}</version>
<configuration> <configuration>
<excludes> <excludes>
<!-- <exclude>**/*ProductionTest.java</exclude> --> <exclude>**/*IntegrationTest.java</exclude>
</excludes> </excludes>
<systemPropertyVariables> <systemPropertyVariables>
<!-- <provPersistenceTarget>h2</provPersistenceTarget> --> <!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
@ -255,6 +260,7 @@
<httpclient.version>4.5</httpclient.version> <httpclient.version>4.5</httpclient.version>
<rest-assured.version>2.9.0</rest-assured.version> <rest-assured.version>2.9.0</rest-assured.version>
<net.sourceforge.htmlunit>2.23</net.sourceforge.htmlunit> <net.sourceforge.htmlunit>2.23</net.sourceforge.htmlunit>
<!-- maven plugins --> <!-- maven plugins -->
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version> <maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.6</maven-war-plugin.version> <maven-war-plugin.version>2.6</maven-war-plugin.version>

View File

@ -29,15 +29,13 @@ public class CompanyController {
return new ModelAndView("companyHome", "company", new Company()); return new ModelAndView("companyHome", "company", new Company());
} }
@RequestMapping(value = "/company/{Id}", produces = { "application/json", @RequestMapping(value = "/company/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
"application/xml" }, method = RequestMethod.GET)
public @ResponseBody Company getCompanyById(@PathVariable final long Id) { public @ResponseBody Company getCompanyById(@PathVariable final long Id) {
return companyMap.get(Id); return companyMap.get(Id);
} }
@RequestMapping(value = "/addCompany", method = RequestMethod.POST) @RequestMapping(value = "/addCompany", method = RequestMethod.POST)
public String submit(@ModelAttribute("company") final Company company, public String submit(@ModelAttribute("company") final Company company, final BindingResult result, final ModelMap model) {
final BindingResult result, final ModelMap model) {
if (result.hasErrors()) { if (result.hasErrors()) {
return "error"; return "error";
} }
@ -51,15 +49,13 @@ public class CompanyController {
@RequestMapping(value = "/companyEmployee/{company}/employeeData/{employee}", method = RequestMethod.GET) @RequestMapping(value = "/companyEmployee/{company}/employeeData/{employee}", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public ResponseEntity<Map<String, String>> getEmployeeDataFromCompany( public ResponseEntity<Map<String, String>> getEmployeeDataFromCompany(@MatrixVariable(pathVar = "employee") final Map<String, String> matrixVars) {
@MatrixVariable(pathVar = "employee") final Map<String, String> matrixVars) {
return new ResponseEntity<>(matrixVars, HttpStatus.OK); return new ResponseEntity<>(matrixVars, HttpStatus.OK);
} }
@RequestMapping(value = "/companyData/{company}/employeeData/{employee}", method = RequestMethod.GET) @RequestMapping(value = "/companyData/{company}/employeeData/{employee}", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public ResponseEntity<Map<String, String>> getCompanyName( public ResponseEntity<Map<String, String>> getCompanyName(@MatrixVariable(value = "name", pathVar = "company") final String name) {
@MatrixVariable(value = "name", pathVar = "company") final String name) {
final Map<String, String> result = new HashMap<String, String>(); final Map<String, String> result = new HashMap<String, String>();
result.put("name", name); result.put("name", name);
return new ResponseEntity<>(result, HttpStatus.OK); return new ResponseEntity<>(result, HttpStatus.OK);

View File

@ -32,9 +32,7 @@ public class EmployeeController {
} }
@RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET) @RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
public public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) {
@ResponseBody
Employee getEmployeeById(@PathVariable final long Id) {
return employeeMap.get(Id); return employeeMap.get(Id);
} }

View File

@ -25,7 +25,7 @@ import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class AopLoggingTest { public class AopLoggingIntegrationTest {
@Before @Before
public void setUp() { public void setUp() {

View File

@ -24,7 +24,7 @@ import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class AopPerformanceTest { public class AopPerformanceIntegrationTest {
@Before @Before
public void setUp() { public void setUp() {

View File

@ -23,7 +23,7 @@ import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class AopPublishingTest { public class AopPublishingIntegrationTest {
@Before @Before
public void setUp() { public void setUp() {

View File

@ -22,7 +22,7 @@ import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/com/baeldung/aop/beans.xml") @ContextConfiguration("/com/baeldung/aop/beans.xml")
public class AopXmlConfigPerformanceTest { public class AopXmlConfigPerformanceIntegrationTest {
@Before @Before
public void setUp() { public void setUp() {

View File

@ -1,22 +0,0 @@
package com.baeldung.htmlunit;
import org.junit.Assert;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class HtmlUnitAndJUnitTest {
@Test
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
try (final WebClient webClient = new WebClient()) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
}
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.htmlunit;
import java.io.IOException;
import java.net.MalformedURLException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
import org.springframework.web.context.WebApplicationContext;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { TestConfig.class })
public class HtmlUnitAndSpringIntegrationTest {
@Autowired
private WebApplicationContext wac;
private WebClient webClient;
@Before
public void setup() {
webClient = MockMvcWebClientBuilder.webAppContextSetup(wac).build();
}
//
@Test
public void givenAMessage_whenSent_thenItShows() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
final String text = "Hello world!";
HtmlPage page = webClient.getPage("http://localhost/message/showForm");
System.out.println(page.asXml());
final HtmlTextInput messageText = page.getHtmlElementById("message");
messageText.setValueAttribute(text);
final HtmlForm form = page.getForms().get(0);
final HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
final HtmlPage newPage = submit.click();
final String receivedText = newPage.getHtmlElementById("received").getTextContent();
Assert.assertEquals(receivedText, text);
System.out.println(newPage.asXml());
}
@Test
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
try (final WebClient client = new WebClient()) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
}
}
}

View File

@ -1,79 +0,0 @@
package com.baeldung.htmlunit;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
import org.springframework.web.context.WebApplicationContext;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { TestConfig.class })
public class HtmlUnitAndSpringTest {
@Autowired
private WebApplicationContext wac;
private WebClient webClient;
@Before
public void setup() {
webClient = MockMvcWebClientBuilder.webAppContextSetup(wac).build();
}
@Test
@Ignore
public void givenAMessage_whenSent_thenItShows() {
final String text = "Hello world!";
HtmlPage page;
try {
page = webClient.getPage("http://localhost/message/showForm");
System.out.println(page.asXml());
final HtmlTextInput messageText = page.getHtmlElementById("message");
messageText.setValueAttribute(text);
final HtmlForm form = page.getForms().get(0);
final HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
final HtmlPage newPage = submit.click();
final String receivedText = newPage.getHtmlElementById("received").getTextContent();
Assert.assertEquals(receivedText, text);
System.out.println(newPage.asXml());
} catch (FailingHttpStatusCodeException | IOException e) {
e.printStackTrace();
}
}
@Test
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
try (final WebClient client = new WebClient()) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
}
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.htmlunit;
import org.junit.Assert;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class HtmlUnitTest {
@Test
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
try (final WebClient webClient = new WebClient()) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
}
}
}

View File

@ -23,7 +23,7 @@ import com.baeldung.spring.web.config.WebConfig;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class) @ContextConfiguration(classes = WebConfig.class)
public class EmployeeTest { public class EmployeeMvcIntegrationTest {
@Autowired @Autowired
private WebApplicationContext webAppContext; private WebApplicationContext webAppContext;

View File

@ -1,7 +1,7 @@
package com.baeldung.web.controller; package com.baeldung.web.controller;
import org.junit.Before;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -15,7 +15,7 @@ import com.baeldung.spring.web.config.WebConfig;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class) @ContextConfiguration(classes = WebConfig.class)
public class EmployeeTestWithoutMockMvc { public class EmployeeNoMvcIntegrationTest {
@Autowired @Autowired
private EmployeeController employeeController; private EmployeeController employeeController;
@ -25,9 +25,10 @@ public class EmployeeTestWithoutMockMvc {
employeeController.initEmployees(); employeeController.initEmployees();
} }
//
@Test @Test
public void whenInitEmployees_thenVerifyValuesInitiation() { public void whenInitEmployees_thenVerifyValuesInitiation() {
Employee employee1 = employeeController.employeeMap.get(1L); Employee employee1 = employeeController.employeeMap.get(1L);
Employee employee2 = employeeController.employeeMap.get(2L); Employee employee2 = employeeController.employeeMap.get(2L);
Employee employee3 = employeeController.employeeMap.get(3L); Employee employee3 = employeeController.employeeMap.get(3L);

View File

@ -1,6 +1,5 @@
package com.baeldung.web.controller; package com.baeldung.web.controller;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
@ -35,7 +34,6 @@ public class GreetControllerIntegrationTest {
private static final String CONTENT_TYPE = "application/json"; private static final String CONTENT_TYPE = "application/json";
@Before @Before
public void setup() throws Exception { public void setup() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
@ -74,8 +72,8 @@ public class GreetControllerIntegrationTest {
@Test @Test
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception { public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()) this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
.andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")); .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
} }
@Test @Test

View File

@ -1,17 +1,19 @@
package com.baeldung.web.controller; package com.baeldung.web.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
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;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; public class GreetControllerUnitTest {
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class GreetControllerTest {
private MockMvc mockMvc; private MockMvc mockMvc;
private static final String CONTENT_TYPE = "application/json"; private static final String CONTENT_TYPE = "application/json";
@ -43,19 +45,17 @@ public class GreetControllerTest {
@Test @Test
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception { public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
this.mockMvc.perform(get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(status().isOk()) this.mockMvc.perform(get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John Doe!!!"));
.andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John Doe!!!"));
} }
@Test @Test
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception { public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)) this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World!!!"));
.andExpect(jsonPath("$.message").value("Hello World!!!"));
} }
@Test @Test
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception { public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(status().isOk()) this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE))
.andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(jsonPath("$.id").value(1)); .andExpect(jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(jsonPath("$.id").value(1));
} }
} }