Skip to content

Instantly share code, notes, and snippets.

@cristianmiranda
Last active October 22, 2015 00:36
Show Gist options
  • Select an option

  • Save cristianmiranda/a1decdf7222852f852cd to your computer and use it in GitHub Desktop.

Select an option

Save cristianmiranda/a1decdf7222852f852cd to your computer and use it in GitHub Desktop.
Mocking static classes
@RunWith(PowerMockRunner.class)
@PrepareForTest(JS.class)
public class FhirServiceTest {
private FhirService service;
@Before
public void setup() {
GWTMockUtilities.disarm();
this.service = new FhirServiceImpl();
}
@Test
public void getPatientByName() {
// mock all the static methods in a class called "Static"
PowerMockito.mockStatic(JS.class);
// use Mockito to set up your expectation
Mockito.when(JS.get(any(JavaScriptObject.class), eq("PREVVY_FHIR_URL"))).thenReturn("http://fhir.develop.prevvy.co:9091/prevvy/");
service.read(ResourceType.Patient, "2923c288-7f59-4b44-b980-fb584650f91a", new FhirService.ResourceCallback() {
@Override
public void onResource(Resource r) {
System.out.println();
}
});
}
}
<!-- Testing Frameworks -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment