-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change usecase to a more logical multiplication and adder
- Loading branch information
Saeid Mirzaei
committed
Dec 2, 2014
1 parent
19d83cb
commit 5d648bd
Showing
7 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Simple Demo project for the usage of mockito to mock a Webservice implemenation. | ||
|
||
See http://techblog.saeidmirzaei.com/?p=213 | ||
|
||
|
||
|
||
Here we are going to test LogicalOperation class. This class has one function for doing logical and. | ||
It relies on third party web service with Interface given in EvaluatorServiceInterface. | ||
Instead of implementing a real fixed web service, third party web service is mocked. | ||
|
||
During the test, we check if the truth table holds, and more important, if web service is called correctly | ||
Specially important is that, if the first arguement is false, the second one should not be evaluated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#Tue Dec 02 09:49:30 CET 2014 | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.source=1.7 | ||
org.eclipse.jdt.core.compiler.compliance=1.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.worldline.samples</groupId> | ||
<artifactId>wsmockdemo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<properties> | ||
<cxf.version>2.7.10</cxf.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<artifactId>junit</artifactId> | ||
<groupId>junit</groupId> | ||
<version>4.10</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.9.5</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-rt-frontend-jaxws</artifactId> | ||
<version>${cxf.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-rt-transports-http</artifactId> | ||
<version>${cxf.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-rt-transports-http-jetty</artifactId> | ||
<version>${cxf.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-api</artifactId> | ||
<version>${cxf.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.0</version> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
13 changes: 13 additions & 0 deletions
13
webServiceMockDemo/src/main/java/wsmockdemo/EvaluatorServiceInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package wsmockdemo; | ||
|
||
import javax.jws.WebMethod; | ||
import javax.jws.WebResult; | ||
import javax.jws.WebService; | ||
|
||
@WebService(targetNamespace = "urn:net:atos:demo") | ||
public interface EvaluatorServiceInterface { | ||
@WebMethod | ||
@WebResult(name = "output", targetNamespace = "urn:net:atos:demo") | ||
boolean evaluate(String expr); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
webServiceMockDemo/src/main/java/wsmockdemo/LogicalOperations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package wsmockdemo; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import javax.xml.namespace.QName; | ||
import javax.xml.ws.Service; | ||
|
||
/** | ||
* This is the class we want to test, it relies on a webservice. | ||
*/ | ||
public class LogicalOperations { | ||
private EvaluatorServiceInterface evaluator; | ||
|
||
|
||
// this web class is depending on external web services for evaluating given string. | ||
// in reality this would be injected instead of being looked up locally | ||
public LogicalOperations() throws MalformedURLException { | ||
URL wsdlURL = new URL("http://localhost:8081/ws?wsdl"); | ||
QName SERVICE_NAME = new QName("http://www.atos.net", "EvaluatorService"); | ||
Service service = Service.create(wsdlURL, SERVICE_NAME); | ||
evaluator = service.getPort(EvaluatorServiceInterface.class); | ||
} | ||
|
||
|
||
public boolean and(String a, String b) { | ||
if (!evaluator.evaluate(a)) | ||
return false; | ||
return evaluator.evaluate(b); | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
webServiceMockDemo/src/test/java/wsmockdemo/LogicalOperationsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package wsmockdemo; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.reset; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.never; | ||
|
||
import javax.xml.namespace.QName; | ||
import javax.xml.ws.Endpoint; | ||
|
||
import org.apache.cxf.jaxws.EndpointImpl; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
public class LogicalOperationsTest { | ||
|
||
// this is the mock for evaluator service | ||
@Mock | ||
EvaluatorServiceInterface evaluatorServiceMock; | ||
|
||
|
||
void resetMocks() { | ||
reset(evaluatorServiceMock); | ||
|
||
// define mock behavior | ||
when(evaluatorServiceMock.evaluate("true")).thenReturn(true); | ||
when(evaluatorServiceMock.evaluate("false")).thenReturn(false); | ||
|
||
} | ||
|
||
// test functionality of logical operator and | ||
@Test | ||
public void testLogicalOperationsWithMock() throws Exception { | ||
|
||
// wrap the evaluator mock in proxy | ||
EvaluatorServiceInterface serviceInterface = ServiceProxy.newInstance(evaluatorServiceMock); | ||
|
||
// publish the mock | ||
String url = "http://localhost:8081/ws"; | ||
org.apache.cxf.jaxws.EndpointImpl endpoint = (EndpointImpl) Endpoint.create(serviceInterface); | ||
|
||
// we have to use the following CXF dependent code, to specify qname, so that it resource locator finds it | ||
QName serviceName = new QName("http://www.atos.net", "EvaluatorService"); | ||
endpoint.setServiceName(serviceName); | ||
endpoint.publish(url); | ||
|
||
|
||
LogicalOperations logicalOperator = new LogicalOperations(); | ||
try { | ||
// test if true and true yields true. Make sure that the evaluator function has been called two times with "true" argument | ||
resetMocks(); | ||
assertTrue(logicalOperator.and("true", "true")); | ||
verify(evaluatorServiceMock, times(2)).evaluate("true"); | ||
|
||
|
||
// test if false and true yields false. Make sure the evaluate is called only once with "false" parameter. The second "true" arguement should not be really checked | ||
resetMocks(); | ||
assertFalse(logicalOperator.and("false", "true")); | ||
verify(evaluatorServiceMock).evaluate("false"); | ||
verify(evaluatorServiceMock, never()).evaluate("true"); | ||
|
||
|
||
// test if true and false yields false. Make sure that evaluate function is called twice, one with "true" and other with "false" as argument | ||
resetMocks(); | ||
assertFalse(logicalOperator.and("true", "false")); | ||
verify(evaluatorServiceMock).evaluate("false"); | ||
verify(evaluatorServiceMock).evaluate("true"); | ||
|
||
// test if false and false yields false. Make sure that evaluate function is called only once for the first parameter | ||
resetMocks(); | ||
assertFalse(logicalOperator.and("false", "false")); | ||
verify(evaluatorServiceMock, times(1)).evaluate("false"); | ||
|
||
|
||
|
||
} finally { | ||
endpoint.stop(); | ||
} | ||
|
||
} | ||
|
||
@Before | ||
public void initMocks() { | ||
MockitoAnnotations.initMocks(this); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
webServiceMockDemo/src/test/java/wsmockdemo/ServiceProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package wsmockdemo; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
|
||
/** | ||
* Utility class to wrap the webservice implementation in a mock. | ||
*/ | ||
public class ServiceProxy implements InvocationHandler { | ||
private Object obj; | ||
|
||
public static <T> T newInstance(T obj) { | ||
ServiceProxy proxy = new ServiceProxy(obj); | ||
Class<?> clazz = obj.getClass(); | ||
T res = (T) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), proxy); | ||
return res; | ||
} | ||
|
||
private ServiceProxy(Object obj) { | ||
this.obj = obj; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method m, Object[] args) throws Exception { | ||
try { | ||
return m.invoke(obj, args); | ||
} catch (Exception e) { | ||
throw new RuntimeException("unexpected invocation exception: " + e.getMessage(), e); | ||
} | ||
} | ||
} |