Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-41876] Replaced with original correct test.
- Loading branch information
Showing
1 changed file
with
29 additions
and
20 deletions.
There are no files selected for viewing
49 changes: 29 additions & 20 deletions
49
src/test/java/jenkins/plugins/nodejs/NodeJSInstallerTest.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 |
---|---|---|
@@ -1,41 +1,50 @@ | ||
package jenkins.plugins.nodejs; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import static org.mockito.Mockito.*; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import hudson.FilePath; | ||
import hudson.model.Node; | ||
import hudson.model.TaskListener; | ||
import hudson.tools.DownloadFromUrlInstaller; | ||
import hudson.tools.ToolInstallation; | ||
import jenkins.plugins.nodejs.tools.CPU; | ||
import jenkins.plugins.nodejs.tools.NodeJSInstaller; | ||
import jenkins.plugins.nodejs.tools.Platform; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest(NodeJSInstaller.class) | ||
public class NodeJSInstallerTest { | ||
|
||
@Issue("JENKINS-41876") | ||
@Test | ||
public void test_skip_install_global_packages_when_empty() throws Exception { | ||
MockNodeJSInstaller mock = new MockNodeJSInstaller("test-id", " ", NodeJSInstaller.DEFAULT_NPM_PACKAGES_REFRESH_HOURS); | ||
mock.performInstallation(null, null, null); | ||
} | ||
|
||
private class MockNodeJSInstaller extends NodeJSInstaller { | ||
|
||
public MockNodeJSInstaller(String id, String npmPackages, long npmPackagesRefreshHours) { | ||
super(id, npmPackages, npmPackagesRefreshHours); | ||
} | ||
|
||
@Override | ||
public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException { | ||
FilePath expected = new FilePath(new File("/home/tools")); | ||
refreshGlobalPackages(node, log, expected); | ||
return expected; | ||
} | ||
public void testMethodThatCallsStaticMethod() throws Exception { | ||
String expectedPackages = " "; | ||
int expectedRefreshHours = 72; | ||
Node currentNode = mock(Node.class); | ||
|
||
// mock all the static methods in a class called "Static" | ||
PowerMockito.mockStatic(NodeJSInstaller.class); | ||
|
||
// create partial mock | ||
NodeJSInstaller installer = new NodeJSInstaller("test-id", expectedPackages, expectedRefreshHours); | ||
NodeJSInstaller spy = PowerMockito.spy(installer); | ||
|
||
// use Mockito to set up your expectation | ||
when(NodeJSInstaller.areNpmPackagesUpToDate(null, expectedPackages, expectedRefreshHours)).thenThrow(new AssertionError()); | ||
PowerMockito.suppress(PowerMockito.methodsDeclaredIn(DownloadFromUrlInstaller.class)); | ||
PowerMockito.doReturn(null).when(spy).getInstallable(); | ||
PowerMockito.doReturn(Platform.LINUX).when(spy, "getPlatform", currentNode); | ||
PowerMockito.doReturn(CPU.amd64).when(spy, "getCPU", currentNode); | ||
when(spy.getNpmPackages()).thenReturn(expectedPackages); | ||
|
||
// execute test | ||
spy.performInstallation(mock(ToolInstallation.class), currentNode, mock(TaskListener.class)); | ||
} | ||
|
||
} |