new httpclient sample project
This commit is contained in:
20
httpclient/src/main/resources/logback.xml
Normal file
20
httpclient/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
6
httpclient/src/main/webapp/WEB-INF/api-servlet.xml
Normal file
6
httpclient/src/main/webapp/WEB-INF/api-servlet.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" >
|
||||
|
||||
</beans>
|
||||
42
httpclient/src/main/webapp/WEB-INF/web.xml
Normal file
42
httpclient/src/main/webapp/WEB-INF/web.xml
Normal file
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
xsi:schemaLocation="
|
||||
http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
id="WebApp_ID" version="3.0">
|
||||
|
||||
<display-name>Spring MVC Application</display-name>
|
||||
|
||||
<!-- Spring root -->
|
||||
<context-param>
|
||||
<param-name>contextClass</param-name>
|
||||
<param-value>
|
||||
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
||||
</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>org.baeldung.config</param-value>
|
||||
</context-param>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<!-- Spring child -->
|
||||
<servlet>
|
||||
<servlet-name>api</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>api</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file />
|
||||
</welcome-file-list>
|
||||
|
||||
</web-app>
|
||||
@@ -0,0 +1,105 @@
|
||||
package org.baeldung.mockito;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
public class MockitoConfigExamplesTest {
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() {
|
||||
final MyList listMock = Mockito.mock(MyList.class);
|
||||
when(listMock.add(anyString())).thenReturn(false);
|
||||
|
||||
final boolean added = listMock.add(randomAlphabetic(6));
|
||||
assertThat(added, is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenMockReturnBehaviorIsConfigured2_thenBehaviorIsVerified() {
|
||||
final MyList listMock = Mockito.mock(MyList.class);
|
||||
doReturn(false).when(listMock).add(anyString());
|
||||
|
||||
final boolean added = listMock.add(randomAlphabetic(6));
|
||||
assertThat(added, is(false));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public final void givenMethodIsConfiguredToThrowException_whenCallingMethod_thenExceptionIsThrown() {
|
||||
final MyList listMock = Mockito.mock(MyList.class);
|
||||
when(listMock.add(anyString())).thenThrow(IllegalStateException.class);
|
||||
|
||||
listMock.add(randomAlphabetic(6));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public final void whenMethodHasNoReturnType_whenConfiguringBehaviorOfMethod_thenPossible() {
|
||||
final MyList listMock = Mockito.mock(MyList.class);
|
||||
doThrow(NullPointerException.class).when(listMock).clear();
|
||||
|
||||
listMock.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingOnlyOnce_thenNoExceptionIsThrown() {
|
||||
final MyList listMock = Mockito.mock(MyList.class);
|
||||
when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
|
||||
|
||||
listMock.add(randomAlphabetic(6));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingTwice_thenExceptionIsThrown() {
|
||||
final MyList listMock = Mockito.mock(MyList.class);
|
||||
when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
|
||||
|
||||
listMock.add(randomAlphabetic(6));
|
||||
listMock.add(randomAlphabetic(6));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenMockMethodCallIsConfiguredToCallTheRealMethod_thenRealMetehodIsCalled() {
|
||||
final MyList listMock = Mockito.mock(MyList.class);
|
||||
when(listMock.size()).thenCallRealMethod();
|
||||
|
||||
assertThat(listMock.size(), equalTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenMockMethodCallIsConfiguredWithCustomAnswer_thenRealMetehodIsCalled() {
|
||||
final MyList listMock = Mockito.mock(MyList.class);
|
||||
doAnswer(new Answer<String>() {
|
||||
@Override
|
||||
public final String answer(final InvocationOnMock invocation) {
|
||||
return "Always the same";
|
||||
}
|
||||
}).when(listMock).get(anyInt());
|
||||
|
||||
final String element = listMock.get(1);
|
||||
assertThat(element, is(equalTo("Always the same")));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public final void givenSpy_whenConfiguringBehaviorOfSpy_thenCorrectlyConfigured() {
|
||||
final MyList instance = new MyList();
|
||||
final MyList spy = Mockito.spy(instance);
|
||||
|
||||
doThrow(NullPointerException.class).when(spy).size();
|
||||
spy.size();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package org.baeldung.mockito;
|
||||
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.atLeast;
|
||||
import static org.mockito.Mockito.atMost;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.exceptions.verification.NoInteractionsWanted;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class MockitoVerifyExamplesTest {
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
mockedList.size();
|
||||
verify(mockedList).size();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
mockedList.size();
|
||||
verify(mockedList, times(1)).size();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
verifyZeroInteractions(mockedList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
verify(mockedList, times(0)).size();
|
||||
}
|
||||
|
||||
@Test(expected = NoInteractionsWanted.class)
|
||||
public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
mockedList.size();
|
||||
mockedList.clear();
|
||||
|
||||
verify(mockedList).size();
|
||||
verifyNoMoreInteractions(mockedList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyingOrderOfInteractions_thenCorrect() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
mockedList.size();
|
||||
mockedList.add("a parameter");
|
||||
mockedList.clear();
|
||||
|
||||
final InOrder inOrder = Mockito.inOrder(mockedList);
|
||||
inOrder.verify(mockedList).size();
|
||||
inOrder.verify(mockedList).add("a parameter");
|
||||
inOrder.verify(mockedList).clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyingAnInteractionHasNotOccurred_thenCorrect() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
mockedList.size();
|
||||
|
||||
verify(mockedList, never()).clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
mockedList.clear();
|
||||
mockedList.clear();
|
||||
mockedList.clear();
|
||||
|
||||
verify(mockedList, atLeast(1)).clear();
|
||||
verify(mockedList, atMost(10)).clear();
|
||||
}
|
||||
|
||||
// with arguments
|
||||
|
||||
@Test
|
||||
public final void whenVerifyingAnInteractionWithExactArgument_thenCorrect() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
mockedList.add("test");
|
||||
|
||||
verify(mockedList).add("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyingAnInteractionWithAnyArgument_thenCorrect() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
mockedList.add("test");
|
||||
|
||||
verify(mockedList).add(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyingAnInteractionWithArgumentCapture_thenCorrect() {
|
||||
final List<String> mockedList = mock(MyList.class);
|
||||
mockedList.addAll(Lists.<String> newArrayList("someElement"));
|
||||
final ArgumentCaptor<List> argumentCaptor = ArgumentCaptor.forClass(List.class);
|
||||
verify(mockedList).addAll(argumentCaptor.capture());
|
||||
final List<String> capturedArgument = argumentCaptor.<List<String>> getValue();
|
||||
assertThat(capturedArgument, hasItem("someElement"));
|
||||
}
|
||||
|
||||
}
|
||||
17
httpclient/src/test/java/org/baeldung/mockito/MyList.java
Normal file
17
httpclient/src/test/java/org/baeldung/mockito/MyList.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package org.baeldung.mockito;
|
||||
|
||||
import java.util.AbstractList;
|
||||
|
||||
public class MyList extends AbstractList<String> {
|
||||
|
||||
@Override
|
||||
public String get(final int index) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
13
httpclient/src/test/resources/.gitignore
vendored
Normal file
13
httpclient/src/test/resources/.gitignore
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
Reference in New Issue
Block a user