1 /* 2 * Copyright (c) 2017 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitointegration; 6 7 import net.bytebuddy.ByteBuddy; 8 import net.bytebuddy.agent.ByteBuddyAgent; 9 import org.hamcrest.Matcher; 10 import org.junit.Assume; 11 import org.junit.Test; 12 import org.mockito.Mockito; 13 import org.mockito.internal.configuration.plugins.Plugins; 14 import org.mockitoutil.ClassLoaders; 15 import org.objenesis.Objenesis; 16 17 import java.util.Set; 18 19 import static org.mockitoutil.ClassLoaders.coverageTool; 20 21 public class NoJUnitDependenciesTest { 22 23 @Test pure_mockito_should_not_depend_JUnit___ByteBuddy()24 public void pure_mockito_should_not_depend_JUnit___ByteBuddy() throws Exception { 25 Assume.assumeTrue("ByteBuddyMockMaker".equals(Plugins.getMockMaker().getClass().getSimpleName())); 26 27 ClassLoader classLoader_without_JUnit = ClassLoaders.excludingClassLoader() 28 .withCodeSourceUrlOf( 29 Mockito.class, 30 Matcher.class, 31 ByteBuddy.class, 32 ByteBuddyAgent.class, 33 Objenesis.class 34 ) 35 .withCodeSourceUrlOf(coverageTool()) 36 .without("junit", "org.junit") 37 .build(); 38 39 Set<String> pureMockitoAPIClasses = ClassLoaders.in(classLoader_without_JUnit).omit("runners", "junit", "JUnit").listOwnedClasses(); 40 41 // The later class is required to be initialized before any inline mock maker classes can be loaded. 42 checkDependency(classLoader_without_JUnit, "org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker"); 43 pureMockitoAPIClasses.remove("org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker"); 44 45 for (String pureMockitoAPIClass : pureMockitoAPIClasses) { 46 checkDependency(classLoader_without_JUnit, pureMockitoAPIClass); 47 } 48 } 49 checkDependency(ClassLoader classLoader_without_JUnit, String pureMockitoAPIClass)50 private void checkDependency(ClassLoader classLoader_without_JUnit, String pureMockitoAPIClass) throws ClassNotFoundException { 51 try { 52 Class.forName(pureMockitoAPIClass, true, classLoader_without_JUnit); 53 } catch (Throwable e) { 54 e.printStackTrace(); 55 throw new AssertionError(String.format("'%s' has some dependency to JUnit", pureMockitoAPIClass)); 56 } 57 } 58 } 59