Skip to content

Commit

Permalink
Everything from the bootstrap class loader will be moved to another p…
Browse files Browse the repository at this point in the history
…ackage (close #356)
  • Loading branch information
henri-tremblay committed Jan 8, 2023
1 parent 8bd27e9 commit e9c369e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ private <T> ClassLoader classLoader(Class<T> toMock) {
}

private static <T> boolean isJdkClassOrWithoutPackage(Class<T> toMock) {
Package aPackage = toMock.getPackage();
return aPackage == null || aPackage.getName().startsWith("java."); // we need to verify for null since some dynamic classes have no package
// null class loader means we are from the bootstrap class loader, the mocks will go in another package in class loader
// we need to verify for null since some dynamic classes have no package
return toMock.getPackage() == null || toMock.getClassLoader() == null;
}

private ClassLoadingStrategy<ClassLoader> classLoadingStrategy() {
Expand Down
48 changes: 25 additions & 23 deletions core/src/test/java/org/easymock/tests2/PartialMockingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import java.lang.reflect.Constructor;
import java.util.ArrayList;

import org.easymock.ConstructorArgs;
import org.easymock.EasyMock;
import org.junit.Test;

import javax.swing.JTable;

/**
* @author Henri Tremblay
*/
Expand All @@ -46,7 +47,6 @@ private A(int i) {
protected abstract int foo();
}

@SuppressWarnings("unchecked")
@Test
public void testPartialMock_PublicConstructor() {
ArrayList<String> list = createMockBuilder(ArrayList.class).withConstructor(3).createMock();
Expand All @@ -65,29 +65,31 @@ public void testPartialMock_ProtectedConstructor() {
verify(a);
}

@Test(expected = RuntimeException.class)
public void testPartialMock_ConstructorNotFound() throws Exception {
Constructor<?> cstr = ArrayList.class.getConstructor(Integer.TYPE);
ConstructorArgs constructorArgs = new ConstructorArgs(cstr, 2.0);
try {
createMockBuilder(ArrayList.class).withConstructor(Integer.TYPE).withArgs(2.0).createMock();
} catch (RuntimeException e) {
assertEquals("Failed to find constructor for param types", e.getMessage());
throw e;
}
@Test
public void testPartialMock_ConstructorNotFound() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> createMockBuilder(ArrayList.class).withConstructor(Float.TYPE).withArgs(2.0).createMock());
assertEquals("No constructor matching arguments can be found", ex.getMessage());
}

@Test(expected = IllegalArgumentException.class)
public void testPartialMock_InvalidParams() throws Exception {
Constructor<?> cstr = ArrayList.class.getConstructor(Integer.TYPE);
ConstructorArgs constructorArgs = new ConstructorArgs(cstr, "test");
createMockBuilder(ArrayList.class).withConstructor(Integer.TYPE).withArgs("test");
@Test
public void testPartialMock_InvalidParams() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> createMockBuilder(ArrayList.class).withConstructor(Integer.TYPE).withArgs("test"));
assertEquals("test isn't of type int", ex.getMessage());
}

@Test(expected = RuntimeException.class)
public void testPartialMock_ExceptionInConstructor() throws Exception {
Constructor<?> cstr = ArrayList.class.getConstructor(Integer.TYPE);
ConstructorArgs constructorArgs = new ConstructorArgs(cstr, -5);
createMockBuilder(ArrayList.class).withConstructor(-5).createMock();
@Test
public void testPartialMock_ExceptionInConstructor() {
RuntimeException ex = assertThrows(RuntimeException.class,
() -> createMockBuilder(ArrayList.class).withConstructor(-5).createMock());
assertEquals("Failed to instantiate mock calling constructor: Exception in constructor", ex.getMessage());
}

@Test
public void partiallyMockedSwingComponent_which_are_in_the_javax_package() {
JTable table = EasyMock.partialMockBuilder(JTable.class).createMock();
assertNotNull(table);
}

}

0 comments on commit e9c369e

Please sign in to comment.