forked from powermock/powermock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created EasyMock support for bytebuddy
- Loading branch information
1 parent
d73e4de
commit 70cb2a7
Showing
9 changed files
with
562 additions
and
649 deletions.
There are no files selected for viewing
1,042 changes: 412 additions & 630 deletions
1,042
api/easymock/src/main/java/org/powermock/api/easymock/PowerMock.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.powermock.examples</groupId> | ||
<artifactId>powermock-examples</artifactId> | ||
<version>1.6.3-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>powermock-examples-byte-buddy-easymock</artifactId> | ||
<name>${project.artifactId}</name> | ||
|
||
<description> | ||
Example demonstrating that PowerMock EasyMock API and Byte Buddy works together. | ||
</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>net.bytebuddy</groupId> | ||
<artifactId>byte-buddy</artifactId> | ||
<version>0.6.15</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.powermock</groupId> | ||
<artifactId>powermock-api-easymock</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.powermock</groupId> | ||
<artifactId>powermock-module-junit4</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.easymock</groupId> | ||
<artifactId>easymock</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
20 changes: 20 additions & 0 deletions
20
.../byte-buddy-easymock/src/main/java/powermock/examples/bytebuddy/easymock/SampleClass.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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package powermock.examples.bytebuddy.easymock; | ||
|
||
public class SampleClass { | ||
} |
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
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
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
76 changes: 76 additions & 0 deletions
76
...ito/src/test/java/powermock/examples/bytebuddy/mockito/ByteBuddyWithPowerMockitoTest.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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package powermock.examples.bytebuddy.mockito; | ||
|
||
import net.bytebuddy.ByteBuddy; | ||
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.MockClassLoader; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.powermock.reflect.Whitebox; | ||
import powermock.examples.bytebuddy.mockito.SampleClass; | ||
|
||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest(SampleClass.class) | ||
public class ByteBuddyWithPowerMockitoTest { | ||
|
||
@Mock | ||
private SampleClass sample; | ||
|
||
@Test | ||
public void assertThatPowerMockAndByteBuddyWorksTogetherWhenCallingMockFromMockito() throws Exception { | ||
SampleClass sample = mock(SampleClass.class); | ||
assertThat(proxy(sample).getClass().getName(), containsString("$ByteBuddy$")); | ||
} | ||
|
||
@Test | ||
public void assertThatPowerMockAndByteBuddyWorksTogetherWhenCallingMockFromPowerMockito() throws Exception { | ||
SampleClass sample = PowerMockito.mock(SampleClass.class); | ||
assertThat(proxy(sample).getClass().getName(), containsString("$ByteBuddy$")); | ||
} | ||
|
||
@Test | ||
public void assertThatPowerMockAndByteBuddyWorksTogetherWhenMockIsInjected() throws Exception { | ||
assertThat(proxy(sample).getClass().getName(), containsString("$ByteBuddy$")); | ||
} | ||
|
||
@After public void | ||
clearPowerMockClassCacheAfterEachTest() { | ||
MockClassLoader mcl = (MockClassLoader) SampleClass.class.getClassLoader(); | ||
Whitebox.getInternalState(mcl, Map.class).clear(); | ||
} | ||
|
||
private static SampleClass proxy(SampleClass sample) | ||
throws IllegalAccessException, InstantiationException { | ||
return new ByteBuddy() | ||
.subclass(sample.getClass()) | ||
.make() | ||
.load(sample.getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) | ||
.getLoaded().newInstance(); | ||
} | ||
} |
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