Skip to content

Commit

Permalink
#704 PowerMockito 1.6.5 throws java.lang.SecurityException signer inf…
Browse files Browse the repository at this point in the history
…ormation mismatch (#712)

Pass protection domain for mocked classes
  • Loading branch information
thekingn0thing committed Nov 1, 2016
1 parent 85d8d11 commit ed7029a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected Class<?> loadModifiedClass(String s) throws ClassFormatError, ClassNot

Class<?> deferClass = deferTo.loadClass(s);
if (shouldModify(s) && !shouldLoadWithMockClassloaderWithoutModifications(s)) {
loadedClass = loadMockClass(s);
loadedClass = loadMockClass(s, deferClass.getProtectionDomain());
} else {
loadedClass = loadUnmockedClass(s, deferClass.getProtectionDomain());
}
Expand Down Expand Up @@ -253,7 +253,7 @@ private Class<?> loadUnmockedClass(String name, ProtectionDomain protectionDomai
/**
* Load a mocked version of the class.
*/
private Class<?> loadMockClass(String name) {
private Class<?> loadMockClass(String name, ProtectionDomain protectionDomain) {

final byte[] clazz;

Expand Down Expand Up @@ -284,7 +284,7 @@ private Class<?> loadMockClass(String name) {
+ e.getMessage(), e);
}

return defineClass(name, clazz, 0, clazz.length);
return defineClass(name, clazz, 0, clazz.length, protectionDomain);
}

public void setMockTransformerChain(List<MockTransformer> mockTransformerChain) {
Expand Down
6 changes: 6 additions & 0 deletions modules/module-test/mockito/junit4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
<version>0.7.5.201505241946</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>core</artifactId>
<version>3.3.0-v_771</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package samples.powermockito.junit4.bugs.github704;

import org.eclipse.core.runtime.FileLocator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({SomeClassUseSignedClasses.class, FileLocator.class})
public class PrepareForTestSignedTest {

@Test
public void shouldBeAbleMockSignedClasses(){
FileLocator fileLocator = mock(FileLocator.class);

mockStatic(SomeClassUseSignedClasses.class);

when(SomeClassUseSignedClasses.getFileLocator()).thenReturn(fileLocator);

assertThat(SomeClassUseSignedClasses.getFileLocator()).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package samples.powermockito.junit4.bugs.github704;

import org.eclipse.core.runtime.FileLocator;

public class SomeClassUseSignedClasses {

public static FileLocator getFileLocator(){
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* PowerMockito 1.6.5 throws java.lang.SecurityException signer information mismatch if in the PrepareForTest annotation affects also classes which have signatures
* https://github.com/jayway/powermock/issues/704
*/
package samples.powermockito.junit4.bugs.github704;

0 comments on commit ed7029a

Please sign in to comment.