[JAVA-13854] Half list moved (#12598)
* [JAVA-13854] added parent module * [JAVA-13854] moved apache-tapestry(submodule) to web-modules(parent) * [JAVA-13854] moved bootique(submodule) to web-modules(parent) * [JAVA-13854] moved dropwizard(submodule) to web-modules(parent) * [JAVA-13854] moved blade(submodule) to web-modules(parent) * [JAVA-13854] moved java-lite(submodule) to web-modules(parent) * [JAVA-13854] moved jooby(submodule) to web-modules(parent) * [JAVA-13854] moved linkrest(submodule) to web-modules(parent) * [JAVA-13854] moved ninja(submodule) to web-modules(parent) * [JAVA-13854] moved ratpack(submodule) to web-modules(parent) * [JAVA-13854] moved resteasy(submodule) to web-modules(parent) * [JAVA-13854] moved restx(submodule) to web-modules(parent) * [JAVA-13854] moved spark-java(submodule) to web-modules(parent) * [JAVA-13854] moved vraptor(submodule) to web-modules(parent) * [JAVA-13854] delete modules that were moved * [JAVA-13854] * [JAVA-13854] * [JAVA-13854] delete ninja submodule + moved raml(submodule) to web-modules(parent) * [JAVA-13854] moved gwt(submodule) to web-modules(parent) * [JAVA-13854] moved jakarta-ee(submodule) to web-modules(parent) * [JAVA-13854] moved javax-servlets(submodule) to web-modules(parent) * [JAVA-13854] moved javax-servlets-2(submodule) to web-modules(parent) * [JAVA-13854] moved jee-7(submodule) to web-modules(parent) * [JAVA-13854] moved play-framework(not a module) to web-modules * [JAVA-13854] fix failing test * [JAVA-13854] moved struts-2(submodule) to web-modules(parent) * [JAVA-13854] moved wicket(submodule) to web-modules(parent) * [JAVA-13854] deleted modules that were moved to web-modules * JAVA-13854 Removed moved modules from the main pom.xml Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com> Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.servlets;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.baeldung.model.Employee;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class EmployeeServletIntegrationTest {
|
||||
|
||||
@Mock
|
||||
HttpServletRequest httpServletRequest;
|
||||
|
||||
@Mock
|
||||
HttpServletResponse httpServletResponse;
|
||||
|
||||
Employee employee;
|
||||
|
||||
@Test
|
||||
public void whenPostRequestToEmployeeServlet_thenEmployeeReturnedAsJson() throws Exception {
|
||||
|
||||
//Given
|
||||
int id = 1;
|
||||
String name = "Karan Khanna";
|
||||
String department = "IT";
|
||||
long salary = 5000;
|
||||
employee = new Employee(id, name, department, salary);
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
when(httpServletResponse.getWriter()).thenReturn(pw);
|
||||
|
||||
EmployeeServlet employeeServlet = new EmployeeServlet();
|
||||
employeeServlet.doGet(httpServletRequest, httpServletResponse);
|
||||
|
||||
String employeeJsonString = sw.getBuffer().toString().trim();
|
||||
Employee fetchedEmployee = new Gson().fromJson(employeeJsonString, Employee.class);
|
||||
assertEquals(fetchedEmployee, employee);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class FormServletLiveTest {
|
||||
|
||||
@Test
|
||||
public void whenPostRequestUsingHttpClient_thenCorrect() throws Exception {
|
||||
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpPost method = new HttpPost("http://localhost:8080/calculateServlet");
|
||||
|
||||
List<BasicNameValuePair> nvps = new ArrayList<>();
|
||||
nvps.add(new BasicNameValuePair("height", String.valueOf(2)));
|
||||
nvps.add(new BasicNameValuePair("weight", String.valueOf(80)));
|
||||
|
||||
method.setEntity(new UrlEncodedFormEntity(nvps));
|
||||
HttpResponse httpResponse = client.execute(method);
|
||||
|
||||
assertEquals("Success", httpResponse.getHeaders("Test")[0].getValue());
|
||||
assertEquals("20.0", httpResponse.getHeaders("BMI")[0].getValue());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.mockito.Mockito.atLeast;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class UserServletUnitTest {
|
||||
|
||||
private static HttpServletRequest request;
|
||||
private static HttpServletResponse response;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpHttpServletRequestMockInstance() {
|
||||
request = mock(HttpServletRequest.class);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpHttpServletResponsetMockInstance() {
|
||||
response = mock(HttpServletResponse.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHttpServletRequestMockInstance_whenCalledgetParameter_thenCalledAtLeastOnce() {
|
||||
request.getParameter("name");
|
||||
verify(request, atLeast(1)).getParameter("name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHttpServletRequestMockInstance_whenCalledgetParameter_thenOneAssertion() {
|
||||
when(request.getParameter("name")).thenReturn("username");
|
||||
assertThat(request.getParameter("name")).isEqualTo("username");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHttpServletResponseMockInstance_whenCalledgetContentType_thenCalledAtLeastOnce() {
|
||||
response.getContentType();
|
||||
verify(response, atLeast(1)).getContentType();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHttpServletResponseMockInstance_whenCalledgetContentType_thenOneAssertion() {
|
||||
when(response.getContentType()).thenReturn("text/html");
|
||||
assertThat(response.getContentType()).isEqualTo("text/html");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user