Skip to content

Commit

Permalink
Created EasyMock support for bytebuddy
Browse files Browse the repository at this point in the history
  • Loading branch information
johanhaleby committed Sep 4, 2015
1 parent d73e4de commit 70cb2a7
Show file tree
Hide file tree
Showing 9 changed files with 562 additions and 649 deletions.
1,042 changes: 412 additions & 630 deletions api/easymock/src/main/java/org/powermock/api/easymock/PowerMock.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Change log next version
* Upgraded Javassist dependency to version 3.20.0-GA.
* Using soft reference in classloader cache
* MockClassloader now extends Javassist Loader classloader to implement findClass etc
* PowerMockito now works with ByteBuddy (issue 579)
* PowerMock now works better with ByteBuddy (issue 579)

Change log 1.6.2 (2015-01-03)
----------------------------
Expand Down
40 changes: 40 additions & 0 deletions examples/byte-buddy-easymock/pom.xml
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>
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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,36 @@
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.easymock.PowerMock;
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.easymock.SampleClass;

import java.util.Map;

import static org.easymock.EasyMock.createMock;
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 ByteBuddyWithPowerMockTest {

@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);
@Ignore("Unfortunately this doesn't work")
public void assertThatPowerMockAndByteBuddyWorksTogetherWhenCallingMockFromEasyMock() throws Exception {
SampleClass sample = createMock(SampleClass.class);
assertThat(proxy(sample).getClass().getName(), containsString("$ByteBuddy$"));
}

@Test
public void assertThatPowerMockAndByteBuddyWorksTogetherWhenMockIsInjected() throws Exception {
public void assertThatPowerMockAndByteBuddyWorksTogetherWhenCallingMockFromPowerMock() throws Exception {
SampleClass sample = PowerMock.createMock(SampleClass.class);
assertThat(proxy(sample).getClass().getName(), containsString("$ByteBuddy$"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<version>1.6.3-SNAPSHOT</version>
</parent>

<artifactId>powermock-examples-byte-buddy</artifactId>
<artifactId>powermock-examples-byte-buddy-mockito</artifactId>
<name>${project.artifactId}</name>

<description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package powermock.examples.bytebuddy;
package powermock.examples.bytebuddy.mockito;

public class SampleClass {
}
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();
}
}
3 changes: 2 additions & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
<module>spring-mockito-xml-agent</module>
<module>spring-mockito-xml</module>
<module>java8</module>
<module>byte-buddy</module>
<module>byte-buddy-mockito</module>
<module>byte-buddy-easymock</module>
</modules>

<dependencyManagement>
Expand Down

0 comments on commit 70cb2a7

Please sign in to comment.