How to use InlineByteBuddyMockMakerTest class of org.mockito.internal.creation.bytebuddy package

Best Mockito code snippet using org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMakerTest

Source:InlineByteBuddyMockMakerTest.java Github

copy

Full Screen

...25import org.mockito.internal.util.collections.Sets;26import org.mockito.mock.MockCreationSettings;27import org.mockito.mock.SerializableMode;28import org.mockito.plugins.MockMaker;29public class InlineByteBuddyMockMakerTest extends AbstractByteBuddyMockMakerTest<InlineByteBuddyMockMaker> {30 public InlineByteBuddyMockMakerTest() {31 super(new InlineByteBuddyMockMaker());32 }33 @Test34 public void should_create_mock_from_final_class() throws Exception {35 MockCreationSettings<InlineByteBuddyMockMakerTest.FinalClass> settings = InlineByteBuddyMockMakerTest.settingsFor(InlineByteBuddyMockMakerTest.FinalClass.class);36 InlineByteBuddyMockMakerTest.FinalClass proxy = mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.FinalClass>(settings));37 assertThat(proxy.foo()).isEqualTo("bar");38 }39 @Test40 public void should_create_mock_from_final_class_in_the_JDK() throws Exception {41 MockCreationSettings<Pattern> settings = InlineByteBuddyMockMakerTest.settingsFor(Pattern.class);42 Pattern proxy = mockMaker.createMock(settings, new MockHandlerImpl<Pattern>(settings));43 assertThat(proxy.pattern()).isEqualTo("bar");44 }45 @Test46 public void should_create_mock_from_abstract_class_with_final_method() throws Exception {47 MockCreationSettings<InlineByteBuddyMockMakerTest.FinalMethodAbstractType> settings = InlineByteBuddyMockMakerTest.settingsFor(InlineByteBuddyMockMakerTest.FinalMethodAbstractType.class);48 InlineByteBuddyMockMakerTest.FinalMethodAbstractType proxy = mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.FinalMethodAbstractType>(settings));49 assertThat(proxy.foo()).isEqualTo("bar");50 assertThat(proxy.bar()).isEqualTo("bar");51 }52 @Test53 public void should_create_mock_from_final_class_with_interface_methods() throws Exception {54 MockCreationSettings<InlineByteBuddyMockMakerTest.FinalMethod> settings = InlineByteBuddyMockMakerTest.settingsFor(InlineByteBuddyMockMakerTest.FinalMethod.class, InlineByteBuddyMockMakerTest.SampleInterface.class);55 InlineByteBuddyMockMakerTest.FinalMethod proxy = mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.FinalMethod>(settings));56 assertThat(proxy.foo()).isEqualTo("bar");57 assertThat(((InlineByteBuddyMockMakerTest.SampleInterface) (proxy)).bar()).isEqualTo("bar");58 }59 @Test60 public void should_detect_non_overridden_generic_method_of_supertype() throws Exception {61 MockCreationSettings<InlineByteBuddyMockMakerTest.GenericSubClass> settings = InlineByteBuddyMockMakerTest.settingsFor(InlineByteBuddyMockMakerTest.GenericSubClass.class);62 InlineByteBuddyMockMakerTest.GenericSubClass proxy = mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.GenericSubClass>(settings));63 assertThat(proxy.value()).isEqualTo("bar");64 }65 @Test66 public void should_create_mock_from_hashmap() throws Exception {67 MockCreationSettings<HashMap> settings = InlineByteBuddyMockMakerTest.settingsFor(HashMap.class);68 HashMap proxy = mockMaker.createMock(settings, new MockHandlerImpl<HashMap>(settings));69 assertThat(proxy.get(null)).isEqualTo("bar");70 }71 @Test72 @SuppressWarnings("unchecked")73 public void should_throw_exception_redefining_unmodifiable_class() {74 MockCreationSettings settings = InlineByteBuddyMockMakerTest.settingsFor(int.class);75 try {76 mockMaker.createMock(settings, new MockHandlerImpl(settings));77 fail("Expected a MockitoException");78 } catch (MockitoException e) {79 e.printStackTrace();80 assertThat(e).hasMessageContaining("Could not modify all classes");81 }82 }83 @Test84 @SuppressWarnings("unchecked")85 public void should_throw_exception_redefining_array() {86 int[] array = new int[5];87 MockCreationSettings<? extends int[]> settings = InlineByteBuddyMockMakerTest.settingsFor(array.getClass());88 try {89 mockMaker.createMock(settings, new MockHandlerImpl(settings));90 fail("Expected a MockitoException");91 } catch (MockitoException e) {92 assertThat(e).hasMessageContaining("Arrays cannot be mocked");93 }94 }95 @Test96 public void should_create_mock_from_enum() throws Exception {97 MockCreationSettings<InlineByteBuddyMockMakerTest.EnumClass> settings = InlineByteBuddyMockMakerTest.settingsFor(InlineByteBuddyMockMakerTest.EnumClass.class);98 InlineByteBuddyMockMakerTest.EnumClass proxy = mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.EnumClass>(settings));99 assertThat(proxy.foo()).isEqualTo("bar");100 }101 @Test102 public void should_fail_at_creating_a_mock_of_a_final_class_with_explicit_serialization() throws Exception {103 MockCreationSettings<InlineByteBuddyMockMakerTest.FinalClass> settings = new CreationSettings<InlineByteBuddyMockMakerTest.FinalClass>().setTypeToMock(InlineByteBuddyMockMakerTest.FinalClass.class).setSerializableMode(SerializableMode.BASIC);104 try {105 mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.FinalClass>(settings));106 fail("Expected a MockitoException");107 } catch (MockitoException e) {108 assertThat(e).hasMessageContaining("Unsupported settings").hasMessageContaining("serialization").hasMessageContaining("extra interfaces");109 }110 }111 @Test112 public void should_fail_at_creating_a_mock_of_a_final_class_with_extra_interfaces() throws Exception {113 MockCreationSettings<InlineByteBuddyMockMakerTest.FinalClass> settings = new CreationSettings<InlineByteBuddyMockMakerTest.FinalClass>().setTypeToMock(InlineByteBuddyMockMakerTest.FinalClass.class).setExtraInterfaces(Sets.<Class<?>>newSet(List.class));114 try {115 mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.FinalClass>(settings));116 fail("Expected a MockitoException");117 } catch (MockitoException e) {118 assertThat(e).hasMessageContaining("Unsupported settings").hasMessageContaining("serialization").hasMessageContaining("extra interfaces");119 }120 }121 @Test122 public void should_mock_interface() {123 MockSettingsImpl<Set> mockSettings = new MockSettingsImpl<Set>();124 mockSettings.setTypeToMock(Set.class);125 mockSettings.defaultAnswer(new Returns(10));126 Set<?> proxy = mockMaker.createMock(mockSettings, new MockHandlerImpl<Set>(mockSettings));127 assertThat(proxy.size()).isEqualTo(10);128 }129 @Test130 public void should_mock_interface_to_string() {131 MockSettingsImpl<Set> mockSettings = new MockSettingsImpl<Set>();132 mockSettings.setTypeToMock(Set.class);133 mockSettings.defaultAnswer(new Returns("foo"));134 Set<?> proxy = mockMaker.createMock(mockSettings, new MockHandlerImpl<Set>(mockSettings));135 assertThat(proxy.toString()).isEqualTo("foo");136 }137 @Test138 public void should_remove_recursive_self_call_from_stack_trace() throws Exception {139 StackTraceElement[] stack = new StackTraceElement[]{ new StackTraceElement("foo", "", "", (-1)), new StackTraceElement(InlineByteBuddyMockMakerTest.SampleInterface.class.getName(), "", "", (-1)), new StackTraceElement("qux", "", "", (-1)), new StackTraceElement("bar", "", "", (-1)), new StackTraceElement("baz", "", "", (-1)) };140 Throwable throwable = new Throwable();141 throwable.setStackTrace(stack);142 throwable = MockMethodAdvice.hideRecursiveCall(throwable, 2, InlineByteBuddyMockMakerTest.SampleInterface.class);143 assertThat(throwable.getStackTrace()).isEqualTo(new StackTraceElement[]{ new StackTraceElement("foo", "", "", (-1)), new StackTraceElement("bar", "", "", (-1)), new StackTraceElement("baz", "", "", (-1)) });144 }145 @Test146 public void should_handle_missing_or_inconsistent_stack_trace() throws Exception {147 Throwable throwable = new Throwable();148 throwable.setStackTrace(new StackTraceElement[0]);149 assertThat(MockMethodAdvice.hideRecursiveCall(throwable, 0, InlineByteBuddyMockMakerTest.SampleInterface.class)).isSameAs(throwable);150 }151 @Test152 public void should_provide_reason_for_wrapper_class() {153 MockMaker.TypeMockability mockable = mockMaker.isTypeMockable(Integer.class);154 assertThat(mockable.nonMockableReason()).isEqualTo("Cannot mock wrapper types, String.class or Class.class");155 }156 @Test157 public void should_provide_reason_for_vm_unsupported() {158 MockMaker.TypeMockability mockable = mockMaker.isTypeMockable(int[].class);159 assertThat(mockable.nonMockableReason()).isEqualTo("VM does not not support modification of given type");160 }161 @Test162 public void should_mock_method_of_package_private_class() throws Exception {163 MockCreationSettings<InlineByteBuddyMockMakerTest.NonPackagePrivateSubClass> settings = InlineByteBuddyMockMakerTest.settingsFor(InlineByteBuddyMockMakerTest.NonPackagePrivateSubClass.class);164 InlineByteBuddyMockMakerTest.NonPackagePrivateSubClass proxy = mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.NonPackagePrivateSubClass>(settings));165 assertThat(proxy.value()).isEqualTo("bar");166 }167 @Test168 public void is_type_mockable_excludes_String() {169 MockMaker.TypeMockability mockable = mockMaker.isTypeMockable(String.class);170 assertThat(mockable.mockable()).isFalse();171 assertThat(mockable.nonMockableReason()).contains("Cannot mock wrapper types, String.class or Class.class");172 }173 @Test174 public void is_type_mockable_excludes_Class() {175 MockMaker.TypeMockability mockable = mockMaker.isTypeMockable(Class.class);176 assertThat(mockable.mockable()).isFalse();177 assertThat(mockable.nonMockableReason()).contains("Cannot mock wrapper types, String.class or Class.class");178 }179 @Test180 public void is_type_mockable_excludes_primitive_classes() {181 MockMaker.TypeMockability mockable = mockMaker.isTypeMockable(int.class);182 assertThat(mockable.mockable()).isFalse();183 assertThat(mockable.nonMockableReason()).contains("primitive");184 }185 @Test186 public void is_type_mockable_allows_anonymous() {187 Observer anonymous = new Observer() {188 @Override189 public void update(Observable o, Object arg) {190 }191 };192 MockMaker.TypeMockability mockable = mockMaker.isTypeMockable(anonymous.getClass());193 assertThat(mockable.mockable()).isTrue();194 assertThat(mockable.nonMockableReason()).contains("");195 }196 @Test197 public void is_type_mockable_give_empty_reason_if_type_is_mockable() {198 MockMaker.TypeMockability mockable = mockMaker.isTypeMockable(AbstractByteBuddyMockMakerTest.SomeClass.class);199 assertThat(mockable.mockable()).isTrue();200 assertThat(mockable.nonMockableReason()).isEqualTo("");201 }202 @Test203 public void is_type_mockable_give_allow_final_mockable_from_JDK() {204 MockMaker.TypeMockability mockable = mockMaker.isTypeMockable(Pattern.class);205 assertThat(mockable.mockable()).isTrue();206 assertThat(mockable.nonMockableReason()).isEqualTo("");207 }208 @Test209 public void test_parameters_retention() throws Exception {210 Assume.assumeTrue(ClassFileVersion.ofThisVm().isAtLeast(ClassFileVersion.JAVA_V8));211 Class<?> typeWithParameters = new ByteBuddy().subclass(Object.class).defineMethod("foo", void.class, Visibility.PUBLIC).withParameter(String.class, "bar").intercept(StubMethod.INSTANCE).make().load(null).getLoaded();212 MockCreationSettings<?> settings = InlineByteBuddyMockMakerTest.settingsFor(typeWithParameters);213 @SuppressWarnings("unchecked")214 Object proxy = mockMaker.createMock(settings, new MockHandlerImpl(settings));215 assertThat(proxy.getClass()).isEqualTo(typeWithParameters);216 assertThat(new TypeDescription.ForLoadedType(typeWithParameters).getDeclaredMethods().filter(ElementMatchers.named("foo")).getOnly().getParameters().getOnly().getName()).isEqualTo("bar");217 }218 @Test219 public void test_clear_mock_clears_handler() {220 MockCreationSettings<InlineByteBuddyMockMakerTest.GenericSubClass> settings = InlineByteBuddyMockMakerTest.settingsFor(InlineByteBuddyMockMakerTest.GenericSubClass.class);221 InlineByteBuddyMockMakerTest.GenericSubClass proxy = mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.GenericSubClass>(settings));222 assertThat(mockMaker.getHandler(proxy)).isNotNull();223 // when224 clearMock(proxy);225 // then226 assertThat(mockMaker.getHandler(proxy)).isNull();227 }228 @Test229 public void test_clear_all_mock_clears_handler() {230 MockCreationSettings<InlineByteBuddyMockMakerTest.GenericSubClass> settings = InlineByteBuddyMockMakerTest.settingsFor(InlineByteBuddyMockMakerTest.GenericSubClass.class);231 InlineByteBuddyMockMakerTest.GenericSubClass proxy1 = mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.GenericSubClass>(settings));232 assertThat(mockMaker.getHandler(proxy1)).isNotNull();233 settings = InlineByteBuddyMockMakerTest.settingsFor(InlineByteBuddyMockMakerTest.GenericSubClass.class);234 InlineByteBuddyMockMakerTest.GenericSubClass proxy2 = mockMaker.createMock(settings, new MockHandlerImpl<InlineByteBuddyMockMakerTest.GenericSubClass>(settings));235 assertThat(mockMaker.getHandler(proxy1)).isNotNull();236 // when237 clearAllMocks();238 // then239 assertThat(mockMaker.getHandler(proxy1)).isNull();240 assertThat(mockMaker.getHandler(proxy2)).isNull();241 }242 @Test243 public void testMockDispatcherIsRelocated() throws Exception {244 assertThat(InlineByteBuddyMockMaker.class.getClassLoader().getResource("org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.raw")).isNotNull();245 }246 private static final class FinalClass {247 public String foo() {248 return "foo";249 }250 }251 private enum EnumClass {252 INSTANCE;253 public String foo() {254 return "foo";255 }256 }257 private abstract static class FinalMethodAbstractType {258 public final String foo() {259 return "foo";260 }261 public abstract String bar();262 }263 private static class FinalMethod {264 public final String foo() {265 return "foo";266 }267 }268 private interface SampleInterface {269 String bar();270 }271 /* package-private */272 abstract class PackagePrivateSuperClass {273 public abstract String indirect();274 public String value() {275 return (indirect()) + "qux";276 }277 }278 public class NonPackagePrivateSubClass extends InlineByteBuddyMockMakerTest.PackagePrivateSuperClass {279 @Override280 public String indirect() {281 return "foo";282 }283 }284 public static class GenericClass<T> {285 public T value() {286 return null;287 }288 }289 public static class GenericSubClass extends InlineByteBuddyMockMakerTest.GenericClass<String> {}290}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in InlineByteBuddyMockMakerTest

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful