Fix fluent client search call so that it can be mocked with Mockito

This commit is contained in:
James Agnew 2015-08-06 18:06:32 -04:00
parent 43dd081098
commit c838d651dd
4 changed files with 77 additions and 4 deletions

View File

@ -78,6 +78,11 @@
<version>${commons_io_version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons_codec_version}</version>
</dependency>
<!-- Android does not come with the Servlet API bundled, and MethodUtil
requires it -->
@ -153,17 +158,21 @@
<shadedClassifierName>dstu</shadedClassifierName>
<artifactSet>
<includes>
<!--
<include>commons-codec:commons-codec</include>
-->
<include>ca.uhn.hapi.fhir:hapi-fhir-base</include>
<include>ca.uhn.hapi.fhir:hapi-fhir-structures-dstu</include>
<include>org.glassfish:javax.json</include>
<include>org.codehaus.woodstox:woodstox-core-asl</include>
<include>javax.xml.stream:stax-api</include>
<include>javax.servlet:javax.servlet-api</include>
<!-- <include>javax.servlet:javax.servlet-api</include>-->
<include>org.codehaus.woodstox:stax2-api</include>
<!-- <include>org.slf4j:slf4j*</include> -->
<!--
<include>org.apache.commons:*</include>
<include>org.apache.httpcomponents:*</include>
-->
<include>org.glassfish:javax.json</include>
</includes>
</artifactSet>
@ -201,16 +210,20 @@
<shadedClassifierName>dstu2</shadedClassifierName>
<artifactSet>
<includes>
<!--
<include>commons-codec:commons-codec</include>
-->
<include>ca.uhn.hapi.fhir:hapi-fhir-base</include>
<include>ca.uhn.hapi.fhir:hapi-fhir-structures-dstu2</include>
<include>org.glassfish:javax.json</include>
<include>org.codehaus.woodstox:woodstox-core-asl</include>
<include>javax.xml.stream:stax-api</include>
<include>javax.servlet:javax.servlet-api</include>
<!-- <include>javax.servlet:javax.servlet-api</include>-->
<include>org.codehaus.woodstox:stax2-api</include>
<!--
<include>org.apache.commons:*</include>
<include>org.apache.httpcomponents:*</include>
-->
<include>org.glassfish:javax.json</include>
</includes>
</artifactSet>

View File

@ -20,7 +20,7 @@ package ca.uhn.fhir.rest.gclient;
* #L%
*/
public interface IBaseQuery<T> {
public interface IBaseQuery<T extends IBaseQuery<?>> {
T where(ICriterion<?> theCriterion);

View File

@ -76,4 +76,18 @@ public interface IQuery<T> extends IClientExecutable<IQuery<T>, T>, IBaseQuery<I
*/
<B extends IBaseBundle> IQuery<B> returnBundle(Class<B> theClass);
/**
* {@inheritDoc}
*/
// This is here as an overridden method to allow mocking clients with Mockito to work
@Override
IQuery<T> where(ICriterion<?> theCriterion);
/**
* {@inheritDoc}
*/
// This is here as an overridden method to allow mocking clients with Mockito to work
@Override
IQuery<T> and(ICriterion<?> theCriterion);
}

View File

@ -0,0 +1,46 @@
package ca.uhn.fhir.rest.client;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.junit.Test;
import org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs;
import org.mockito.internal.stubbing.defaultanswers.ReturnsSmartNulls;
import ca.uhn.fhir.model.dstu2.resource.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Condition;
import ca.uhn.fhir.rest.gclient.ICriterion;
import ca.uhn.fhir.rest.gclient.IQuery;
public class ClientMockingTest {
@SuppressWarnings("unchecked")
@Test
public void testMockingDeepStubs() {
IGenericClient client = mock(IGenericClient.class, new ReturnsDeepStubs());
// System.out.println(stub.getClass());
// System.out.println(stub.getClass());
Bundle retVal = new Bundle();
//@formatter:off
when((Object)client
.search()
.forResource(eq(Condition.class))
.where(any(ICriterion.class))
.returnBundle((Class<IBaseBundle>)any())
.execute())
.thenReturn(retVal);
//@formatter:off
Bundle actual = client.search().forResource(Condition.class).where(Condition.ASSERTER.hasId("123")).returnBundle(Bundle.class).execute();
assertSame(retVal, actual);
}
}