Skip to content

Commit

Permalink
Using ModuleFactory to mock test module
Browse files Browse the repository at this point in the history
  • Loading branch information
Male Michael committed Dec 19, 2017
1 parent 0a625a8 commit e69df60
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
33 changes: 29 additions & 4 deletions omod-1.8/pom.xml
Expand Up @@ -20,15 +20,15 @@
<artifactId>${project.parent.artifactId}-omod-common</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>${project.parent.artifactId}-omod-common</artifactId>
<version>${project.parent.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
Expand All @@ -41,13 +41,13 @@
<classifier>tests</classifier>
<version>${openmrs.version.1.8}</version><!--$NO-MVN-MAN-VER$-->
</dependency>

<dependency>
<groupId>org.openmrs.web</groupId>
<artifactId>openmrs-web</artifactId>
<version>${openmrs.version.1.8}</version><!--$NO-MVN-MAN-VER$-->
</dependency>

<dependency>
<groupId>org.openmrs.web</groupId>
<artifactId>openmrs-web</artifactId>
Expand All @@ -61,6 +61,31 @@
<type>pom</type>
<version>${openmrs.version.1.8}</version><!--$NO-MVN-MAN-VER$-->
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5</version>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5</version>
<exclusions>
<exclusion>
<artifactId>mockito-all</artifactId>
<groupId>org.mockito</groupId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

<build>
Expand Down
Expand Up @@ -14,13 +14,18 @@
import java.net.URL;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Before;
import org.openmrs.module.Module;
import org.openmrs.module.ModuleFactory;
import org.openmrs.module.ModuleUtil;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.openmrs.module.webservices.helper.ModuleInstall;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.MockModuleFactoryWrapper;
Expand All @@ -31,6 +36,8 @@
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;

// @RunWith(PowerMockRunner.class)
// @PrepareForTest(ModuleUtil.class)
public class ModuleInstallResource1_8Test extends MainResourceControllerTest {

@Autowired
Expand All @@ -43,6 +50,10 @@ public class ModuleInstallResource1_8Test extends MainResourceControllerTest {

MockModuleFactoryWrapper mockModuleFactory = new MockModuleFactoryWrapper();

public ModuleInstallResource1_8Test() {
ModuleFactory.getStartedModulesMap().put("mockModule", mockModuleToLoad);
}

@Before
public void setUp() throws Exception {
ModuleInstallResource1_8 resource = (ModuleInstallResource1_8) restService
Expand All @@ -52,12 +63,12 @@ public void setUp() throws Exception {

@Test
public void shouldLoadModule() throws Exception {
List<Module> modules = mockModuleFactory.loadedModules;
Assert.assertEquals(modules.size(), 0);
URL url = new URL(getInstallUri());
ModuleUtil moduleUtil = Mockito.mock(ModuleUtil.class);
byte[] fileData = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("mockModule.omod"));
Mockito.when(moduleUtil.getURLStream(url)).thenReturn(new ByteArrayInputStream(fileData));
// List<Module> modules = mockModuleFactory.loadedModules;
// Assert.assertEquals(modules.size(), 0);
// URL url = new URL(getInstallUri());
// byte[] fileData = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("mockModule.omod"));
// PowerMockito.mockStatic(ModuleUtil.class);
// PowerMockito.when(ModuleUtil.getURLStream(url)).thenReturn(new ByteArrayInputStream(fileData));
SimpleObject simpleObject = deserialize(handle(newPostRequest(getURI(),
"{\"installuri\":\"" + getInstallUri() + "\", \"moduleuuid\":\"" + getUuid()
+ "\"}")));
Expand Down
29 changes: 27 additions & 2 deletions omod/pom.xml
Expand Up @@ -107,11 +107,36 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5</version>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5</version>
<exclusions>
<exclusion>
<artifactId>mockito-all</artifactId>
<groupId>org.mockito</groupId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

<build>
<finalName>${project.parent.artifactId}-${project.parent.version}.${revisionNumber}</finalName>

<plugins>
<plugin>
<groupId>org.openmrs.maven.plugins</groupId>
Expand Down

0 comments on commit e69df60

Please sign in to comment.