本文整理匯總了Java中org.powermock.core.classloader.annotations.PrepareForTest類的典型用法代碼示例。如果您正苦於以下問題:Java PrepareForTest類的具體用法?Java PrepareForTest怎麽用?Java PrepareForTest使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PrepareForTest類屬於org.powermock.core.classloader.annotations包,在下文中一共展示了PrepareForTest類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testEncodingExceptions
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest({Converter.class, QppOutputEncoder.class})
public void testEncodingExceptions() throws Exception {
QppOutputEncoder encoder = mock(QppOutputEncoder.class);
whenNew(QppOutputEncoder.class).withAnyArguments().thenReturn(encoder);
EncodeException ex = new EncodeException("mocked", new RuntimeException());
doThrow(ex).when(encoder).encode();
Path path = Paths.get("src/test/resources/converter/defaultedNode.xml");
Converter converter = new Converter(new PathSource(path));
converter.getContext().setDoDefaults(false);
converter.getContext().setDoValidation(false);
try {
converter.transform();
fail();
} catch (TransformException exception) {
checkup(exception, ErrorCode.NOT_VALID_XML_DOCUMENT);
}
}
示例2: testUnexpectedError
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest({Converter.class, XmlUtils.class})
public void testUnexpectedError() {
mockStatic(XmlUtils.class);
when(XmlUtils.fileToStream(any(Path.class))).thenReturn(null);
Path path = Paths.get("../qrda-files/valid-QRDA-III.xml");
Converter converter = new Converter(new PathSource(path));
try {
converter.transform();
fail();
} catch (TransformException exception) {
checkup(exception, ErrorCode.UNEXPECTED_ERROR);
}
}
示例3: redeemScript
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@PrepareForTest({ ScriptBuilder.class })
@Test
public void redeemScript() {
final List<Integer> calls = new ArrayList<>();
PowerMockito.mockStatic(ScriptBuilder.class);
PowerMockito.when(ScriptBuilder.createRedeemScript(any(int.class), any(List.class))).thenAnswer((invocationOnMock) -> {
calls.add(1);
int numberOfSignaturesRequired = invocationOnMock.getArgumentAt(0, int.class);
List<BtcECKey> publicKeys = invocationOnMock.getArgumentAt(1, List.class);
Assert.assertEquals(4, numberOfSignaturesRequired);
Assert.assertEquals(6, publicKeys.size());
for (int i = 0; i < sortedPublicKeys.size(); i++) {
Assert.assertTrue(Arrays.equals(sortedPublicKeys.get(i).getPubKey(), publicKeys.get(i).getPubKey()));
}
return new Script(new byte[]{(byte)0xaa});
});
Assert.assertTrue(Arrays.equals(federation.getRedeemScript().getProgram(), new byte[]{(byte) 0xaa}));
Assert.assertTrue(Arrays.equals(federation.getRedeemScript().getProgram(), new byte[]{(byte) 0xaa}));
// Make sure the script creation happens only once
Assert.assertEquals(1, calls.size());
}
示例4: P2SHScript
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@PrepareForTest({ ScriptBuilder.class })
@Test
public void P2SHScript() {
final List<Integer> calls = new ArrayList<>();
PowerMockito.mockStatic(ScriptBuilder.class);
PowerMockito.when(ScriptBuilder.createP2SHOutputScript(any(int.class), any(List.class))).thenAnswer((invocationOnMock) -> {
calls.add(0);
int numberOfSignaturesRequired = invocationOnMock.getArgumentAt(0, int.class);
List<BtcECKey> publicKeys = invocationOnMock.getArgumentAt(1, List.class);
Assert.assertEquals(4, numberOfSignaturesRequired);
Assert.assertEquals(6, publicKeys.size());
for (int i = 0; i < sortedPublicKeys.size();i ++) {
Assert.assertTrue(Arrays.equals(sortedPublicKeys.get(i).getPubKey(), publicKeys.get(i).getPubKey()));
}
return new Script(new byte[]{(byte)0xaa});
});
Assert.assertTrue(Arrays.equals(federation.getP2SHScript().getProgram(), new byte[]{(byte) 0xaa}));
Assert.assertTrue(Arrays.equals(federation.getP2SHScript().getProgram(), new byte[]{(byte) 0xaa}));
// Make sure the script creation happens only once
Assert.assertEquals(1, calls.size());
}
示例5: Address
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@PrepareForTest({ ScriptBuilder.class })
@Test
public void Address() {
// Since we can't mock both Address and ScriptBuilder at the same time (due to PowerMockito limitations)
// we use a well known P2SH and its corresponding address
// and just mock the ScriptBuilder
// a914896ed9f3446d51b5510f7f0b6ef81b2bde55140e87 => 2N5muMepJizJE1gR7FbHJU6CD18V3BpNF9p
final List<Integer> calls = new ArrayList<>();
PowerMockito.mockStatic(ScriptBuilder.class);
PowerMockito.when(ScriptBuilder.createP2SHOutputScript(any(int.class), any(List.class))).thenAnswer((invocationOnMock) -> {
calls.add(0);
int numberOfSignaturesRequired = invocationOnMock.getArgumentAt(0, int.class);
List<BtcECKey> publicKeys = invocationOnMock.getArgumentAt(1, List.class);
Assert.assertEquals(4, numberOfSignaturesRequired);
Assert.assertEquals(6, publicKeys.size());
for (int i = 0; i < sortedPublicKeys.size();i ++) {
Assert.assertTrue(Arrays.equals(sortedPublicKeys.get(i).getPubKey(), publicKeys.get(i).getPubKey()));
}
return new Script(Hex.decode("a914896ed9f3446d51b5510f7f0b6ef81b2bde55140e87"));
});
Assert.assertEquals("2N5muMepJizJE1gR7FbHJU6CD18V3BpNF9p", federation.getAddress().toBase58());
Assert.assertEquals("2N5muMepJizJE1gR7FbHJU6CD18V3BpNF9p", federation.getAddress().toBase58());
// Make sure the address creation happens only once
Assert.assertEquals(1, calls.size());
}
示例6: testGetItemFile
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest({Environment.class})
public void testGetItemFile() throws Exception {
File file = new File("/dir/");
when(mockContext.getExternalFilesDir(null)).thenReturn(file);
Configuration.Item item = new Configuration.Item();
item.location = "http://example.com/";
assertEquals(new File("/dir/http%3A%2F%2Fexample.com%2F"), FileHelper.getItemFile(mockContext, item));
item.location = "https://example.com/";
assertEquals(new File("/dir/https%3A%2F%2Fexample.com%2F"), FileHelper.getItemFile(mockContext, item));
item.location = "file:/myfile";
assertNull(FileHelper.getItemFile(mockContext, item));
mockStatic(Environment.class);
when(Environment.getExternalStorageDirectory()).thenReturn(new File("/sdcard/"));
item.location = "file:myfile";
assertNull(null, FileHelper.getItemFile(mockContext, item));
item.location = "ahost.com";
assertNull(FileHelper.getItemFile(mockContext, item));
}
示例7: testPoll_retryInterrupt
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest({Log.class, Os.class})
public void testPoll_retryInterrupt() throws Exception {
mockStatic(Log.class);
mockStatic(Os.class);
when(Os.poll(any(StructPollfd[].class), anyInt())).then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
// First try fails with EINTR, seconds returns success.
if (testResult++ == 0) {
// Android actually sets all OsConstants to 0 when running the
// unit tests, so this works, but another constant would have
// exactly the same result.
throw new ErrnoException("poll", OsConstants.EINTR);
}
return 0;
}
});
// poll() will be interrupted first time, so called a second time.
assertEquals(0, FileHelper.poll(null, 0));
assertEquals(2, testResult);
}
示例8: testCloseOrWarn_fileDescriptor
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest({Log.class, Os.class})
public void testCloseOrWarn_fileDescriptor() throws Exception {
FileDescriptor fd = mock(FileDescriptor.class);
mockStatic(Log.class);
mockStatic(Os.class);
when(Log.e(anyString(), anyString(), any(Throwable.class))).then(new CountingAnswer(null));
// Closing null should work just fine
testResult = 0;
assertNull(FileHelper.closeOrWarn((FileDescriptor) null, "tag", "msg"));
assertEquals(0, testResult);
// Successfully closing the file should not log.
testResult = 0;
assertNull(FileHelper.closeOrWarn(fd, "tag", "msg"));
assertEquals(0, testResult);
// If closing fails, it should log.
testResult = 0;
doThrow(new ErrnoException("close", 0)).when(Os.class, "close", any(FileDescriptor.class));
assertNull(FileHelper.closeOrWarn(fd, "tag", "msg"));
assertEquals(1, testResult);
}
示例9: testCloseOrWarn_closeable
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest(Log.class)
public void testCloseOrWarn_closeable() throws Exception {
Closeable closeable = mock(Closeable.class);
mockStatic(Log.class);
when(Log.e(anyString(), anyString(), any(Throwable.class))).then(new CountingAnswer(null));
// Closing null should work just fine
testResult = 0;
assertNull(FileHelper.closeOrWarn((Closeable) null, "tag", "msg"));
assertEquals(0, testResult);
// Successfully closing the file should not log.
testResult = 0;
assertNull(FileHelper.closeOrWarn(closeable, "tag", "msg"));
assertEquals(0, testResult);
// If closing fails, it should log.
when(closeable).thenThrow(new IOException("Foobar"));
testResult = 0;
assertNull(FileHelper.closeOrWarn(closeable, "tag", "msg"));
assertEquals(1, testResult);
}
示例10: testInitialize_disabled
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@PrepareForTest({Log.class, FileHelper.class})
public void testInitialize_disabled() throws Exception {
RuleDatabase ruleDatabase = spy(new RuleDatabase());
Configuration.Item item = new Configuration.Item();
item.location = "ahost.com";
item.state = Configuration.Item.STATE_DENY;
Configuration configuration = new Configuration();
configuration.hosts = new Configuration.Hosts();
configuration.hosts.enabled = false;
configuration.hosts.items = new ArrayList<>();
configuration.hosts.items.add(item);
Context context = mock(Context.class);
mockStatic(FileHelper.class);
when(FileHelper.loadCurrentSettings(context)).thenReturn(configuration);
when(FileHelper.openItemFile(context, item)).thenReturn(null);
ruleDatabase.initialize(context);
assertFalse(ruleDatabase.isBlocked("ahost.com"));
assertTrue(ruleDatabase.isEmpty());
}
示例11: testInitialize_fileNotFound
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest({Log.class, FileHelper.class})
public void testInitialize_fileNotFound() throws Exception {
RuleDatabase ruleDatabase = spy(new RuleDatabase());
Configuration.Item item = new Configuration.Item();
item.location = "protocol://some-weird-file-uri";
item.state = Configuration.Item.STATE_DENY;
Configuration configuration = new Configuration();
configuration.hosts = new Configuration.Hosts();
configuration.hosts.enabled = true;
configuration.hosts.items = new ArrayList<>();
configuration.hosts.items.add(item);
Context context = mock(Context.class);
mockStatic(FileHelper.class);
when(FileHelper.loadCurrentSettings(context)).thenReturn(configuration);
when(FileHelper.openItemFile(context, item)).thenThrow(new FileNotFoundException("foobar"));
ruleDatabase.initialize(context);
assertTrue(ruleDatabase.isEmpty());
}
示例12: testGetArtifactRootDir
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest(SystemDirectory.class)
public void testGetArtifactRootDir() throws Exception {
final File file = new File("bamboo-artifacts/");
final ArtifactStorage storage = PowerMockito.mock(ArtifactStorage.class);
PowerMockito.mockStatic(SystemDirectory.class);
PowerMockito.when(SystemDirectory.getArtifactStorage()).thenReturn(storage);
final PlanResultKey key = createResultKey();
final ArtifactDefinitionContext artifact = createArtifact();
PowerMockito.when(storage.getArtifactDirectory(key)).thenReturn(file);
final BambooFileStorageHelper storageHelper = new BambooFileStorageHelper();
storageHelper.setResultKey(key);
storageHelper.setArtifactDefinition(artifact);
final File reportFile = storageHelper.buildArtifactRootDirectory();
final String path = file.getCanonicalPath() + "/Hub_Risk_Report";
final String reportFilePath = reportFile.getCanonicalPath();
assertEquals(key, storageHelper.getResultKey());
assertEquals(artifact, storageHelper.getArtifactDefinition());
assertEquals(path, reportFilePath);
}
示例13: testMainJarCopied
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest(value = {Utils.class})
public void testMainJarCopied() throws Exception {
prepareJetBuildDir();
mockCopying("copyFile");
File mainJarSpy = Tests.fileSpy(Tests.mainJar);
ExcelsiorJet excelsiorJet = Tests.excelsiorJet();
JetProject prj = Mockito.spy(Tests.testProject(ApplicationType.PLAIN).
mainJar(mainJarSpy));
prj.processDependencies();
Mockito.doNothing().when(prj).validate(excelsiorJet, true);
Mockito.when(excelsiorJet.compile(Tests.jetBuildDir.toFile(), "=p", "test.prj", "-jetvmprop=")).thenReturn(0);
JetBuildTask buildTask = Mockito.spy(new JetBuildTask(excelsiorJet, prj, false));
buildTask.execute();
Mockito.verify(excelsiorJet).compile(Tests.jetBuildDir.toFile(), "=p", "test.prj", "-jetvmprop=");
Path from = fromCaptor.getValue();
Path to = toCaptor.getValue();
assertEquals(Tests.mainJar.toString(), from.toFile().getAbsolutePath());
assertEquals(Tests.jetBuildDir.resolve("test.jar").toString(), to.toFile().getAbsolutePath());
}
示例14: testExternalJarCopied
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest(value = {Utils.class})
public void testExternalJarCopied() throws Exception {
mockCopying("copyFile");
ExcelsiorJet excelsiorJet = Tests.excelsiorJet();
File externalJar = Tests.fileSpy(Tests.externalJarAbs);
File mainJarSpy = Tests.fileSpy(Tests.mainJar);
JetProject prj = Mockito.spy(Tests.testProject(ApplicationType.PLAIN).
dependencies(singletonList(DependencyBuilder.testExternalDependency(externalJar).pack(ClasspathEntry.PackType.ALL).asDependencySettings())).
mainJar(mainJarSpy));
prj.processDependencies();
Mockito.doNothing().when(prj).validate(excelsiorJet, true);
Mockito.when(excelsiorJet.compile(null, "=p", "test.prj", "-jetvmprop=")).thenReturn(0);
JetBuildTask buildTask = new JetBuildTask(excelsiorJet, prj, false);
buildTask.execute();
Mockito.verify(excelsiorJet).compile(Tests.jetBuildDir.toFile(), "=p", "test.prj", "-jetvmprop=");
Path externalFrom = fromCaptor.getAllValues().get(1);
Path externalTo = toCaptor.getAllValues().get(1);
assertEquals(Tests.externalJarAbs.toString(), externalFrom.toFile().getAbsolutePath());
assertEquals(Tests.jetBuildDir.resolve(Tests.externalJarRel).toString(), externalTo.toFile().getAbsolutePath());
}
示例15: testMainJarNotCopiedForTomcatApp
import org.powermock.core.classloader.annotations.PrepareForTest; //導入依賴的package包/類
@Test
@PrepareForTest(value = {Utils.class})
public void testMainJarNotCopiedForTomcatApp() throws Exception {
prepareJetBuildDir();
mockUtilsClass();
ExcelsiorJet excelsiorJet = Tests.excelsiorJet();
JetProject prj = Mockito.spy(Tests.testProject(ApplicationType.TOMCAT));
prj.processDependencies();
prj.tomcatConfiguration().tomcatHome = "/tomcat-home";
Mockito.doNothing().when(prj).validate(excelsiorJet, true);
Mockito.when(excelsiorJet.compile(null, "=p", "test.prj", "-jetvmprop=")).thenReturn(0);
JetBuildTask buildTask = new JetBuildTask(excelsiorJet, prj, false);
buildTask.execute();
PowerMockito.verifyStatic(Mockito.never());
Utils.copy(anyObject(), anyObject());
}