Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.10.0'
classpath 'com.android.tools.build:gradle:8.13.2'
}
}

Expand Down
2 changes: 1 addition & 1 deletion dexmaker-mockito-inline-extended-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'

androidTestImplementation 'org.mockito:mockito-core:2.28.2', { exclude group: 'net.bytebuddy' }
androidTestImplementation libs.mockito.core, { exclude group: 'net.bytebuddy' }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.reset;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.staticMockMarker;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyZeroInteractions;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyNoInteractions;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -401,7 +401,7 @@ public void clearInvocationsRemovedInvocations() throws Exception {
try {
SuperClass.returnB();
clearInvocations(staticMockMarker(SuperClass.class));
verifyZeroInteractions(staticMockMarker(SuperClass.class));
verifyNoInteractions(staticMockMarker(SuperClass.class));
} finally {
session.finishMocking();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.Silent.class)
public class StaticMockitoSessionVsMockitoJUnitRunner {
@Test
public void simpleStubbing() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static com.android.dx.mockito.inline.extended.ExtendedMockito.staticMockMarker;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyNoMoreInteractions;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyZeroInteractions;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyNoInteractions;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -180,7 +180,7 @@ public void zeroInvocationsThrowsIfThereWasAnInvocation() throws Exception {
MockitoSession session = mockitoSession().mockStatic(EchoClass.class).startMocking();
try {
EchoClass.echo("marco!");
verifyZeroInteractions(staticMockMarker(EchoClass.class));
verifyNoInteractions(staticMockMarker(EchoClass.class));
fail();
} finally {
session.finishMocking();
Expand Down
2 changes: 1 addition & 1 deletion dexmaker-mockito-inline-extended/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ dependencies {

implementation project(':dexmaker-mockito-inline')

api 'org.mockito:mockito-core:2.28.2', { exclude group: 'net.bytebuddy' }
api libs.mockito.core, { exclude group: 'net.bytebuddy' }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.android.dx.mockito.inline.extended;

import org.mockito.InOrder;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.verification.VerificationMode;

Expand Down Expand Up @@ -141,6 +142,12 @@ public void verify(MockedMethod method, VerificationMode mode) {
verify((MockedVoidMethod) method::get, mode);
}

@Override
public void verify(MockedStatic<?> mockedStatic, MockedStatic.Verification verification,
VerificationMode mode) {
instanceInOrder.verify(mockedStatic, verification, mode);
}

@Override
public void verifyNoMoreInteractions() {
instanceInOrder.verifyNoMoreInteractions();
Expand Down
2 changes: 1 addition & 1 deletion dexmaker-mockito-inline-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ dependencies {

androidTestImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'org.mockito:mockito-core:2.28.2', { exclude group: 'net.bytebuddy' }
androidTestImplementation libs.mockito.core, { exclude group: 'net.bytebuddy' }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* 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 com.android.dx.mockito.inline.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mockSingleton;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedSingleton;

@RunWith(AndroidJUnit4.class)
public class MockSingleton {

enum SingleEnum {
VALUE_A {
@Override
String greeting() {
return "hello";
}
},
VALUE_B {
@Override
String greeting() {
return "world";
}
};

abstract String greeting();

int compute(int x) {
return x + 1;
}
}

static class SimpleSingleton {
static final SimpleSingleton INSTANCE = new SimpleSingleton();

String getValue() {
return "original";
}

int add(int a, int b) {
return a + b;
}
}

@Test
public void mockEnumSingleton() {
assertEquals("hello", SingleEnum.VALUE_A.greeting());

try (MockedSingleton<SingleEnum> mocked = mockSingleton(SingleEnum.VALUE_A)) {
when(SingleEnum.VALUE_A.greeting()).thenReturn("mocked");
assertEquals("mocked", SingleEnum.VALUE_A.greeting());
}

assertEquals("hello", SingleEnum.VALUE_A.greeting());
}

@Test
public void mockEnumDoesNotAffectOtherValues() {
try (MockedSingleton<SingleEnum> mocked = mockSingleton(SingleEnum.VALUE_A)) {
when(SingleEnum.VALUE_A.greeting()).thenReturn("mocked");

assertEquals("mocked", SingleEnum.VALUE_A.greeting());
assertEquals("world", SingleEnum.VALUE_B.greeting());
}
}

@Test
public void mockSingletonInstance() {
assertEquals("original", SimpleSingleton.INSTANCE.getValue());

try (MockedSingleton<SimpleSingleton> mocked = mockSingleton(SimpleSingleton.INSTANCE)) {
when(SimpleSingleton.INSTANCE.getValue()).thenReturn("mocked");
assertEquals("mocked", SimpleSingleton.INSTANCE.getValue());
}

assertEquals("original", SimpleSingleton.INSTANCE.getValue());
}

@Test
public void getInstanceReturnsSameObject() {
try (MockedSingleton<SingleEnum> mocked = mockSingleton(SingleEnum.VALUE_A)) {
assertSame(SingleEnum.VALUE_A, mocked.getInstance());
}
}

@Test
public void resetClearsStubs() {
try (MockedSingleton<SingleEnum> mocked = mockSingleton(SingleEnum.VALUE_A)) {
when(SingleEnum.VALUE_A.greeting()).thenReturn("mocked");
assertEquals("mocked", SingleEnum.VALUE_A.greeting());

reset(SingleEnum.VALUE_A);
// After reset, default mock behavior returns null for objects
assertEquals(null, SingleEnum.VALUE_A.greeting());
}
}

@Test
public void verifyInteraction() {
try (MockedSingleton<SimpleSingleton> mocked = mockSingleton(SimpleSingleton.INSTANCE)) {
SimpleSingleton.INSTANCE.getValue();

verify(SimpleSingleton.INSTANCE).getValue();
}
}

@Test
public void stubMethodWithArgs() {
try (MockedSingleton<SimpleSingleton> mocked = mockSingleton(SimpleSingleton.INSTANCE)) {
when(SimpleSingleton.INSTANCE.add(2, 3)).thenReturn(99);

assertEquals(99, SimpleSingleton.INSTANCE.add(2, 3));
assertEquals(0, SimpleSingleton.INSTANCE.add(1, 1));
}

assertEquals(2, SimpleSingleton.INSTANCE.add(1, 1));
}
}
2 changes: 1 addition & 1 deletion dexmaker-mockito-inline/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ dependencies {

implementation project(':dexmaker')

api 'org.mockito:mockito-core:2.28.2', { exclude group: 'net.bytebuddy' }
api libs.mockito.core, { exclude group: 'net.bytebuddy' }
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ class ClassTransformer {
* mocked or not.
*/
ClassTransformer(JvmtiAgent agent, Class dispatcherClass,
Map<Object, InvocationHandlerAdapter> mocks) {
Map<Object, InvocationHandlerAdapter> mocks,
ThreadLocal<Map<Object, InvocationHandlerAdapter>> singletonMocks) {
this.agent = agent;
mockedTypes = Collections.synchronizedSet(new HashSet<Class<?>>());
identifier = String.valueOf(System.identityHashCode(this));
MockMethodAdvice advice = new MockMethodAdvice(mocks);
MockMethodAdvice advice = new MockMethodAdvice(mocks, singletonMocks);

try {
dispatcherClass.getMethod("set", String.class, Object.class).invoke(null, identifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
import java.lang.reflect.Proxy;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -167,6 +169,9 @@ public final class InlineDexmakerMockMaker implements InlineMockMaker {
*/
private final Map<Object, InvocationHandlerAdapter> mocks;

private final ThreadLocal<Map<Object, InvocationHandlerAdapter>> singletonMocks =
new ThreadLocal<>();

/**
* Class doing the actual byte code transformation.
*/
Expand All @@ -185,7 +190,7 @@ public InlineDexmakerMockMaker() {
}

mocks = new MockMap();
classTransformer = new ClassTransformer(AGENT, DISPATCHER_CLASS, mocks);
classTransformer = new ClassTransformer(AGENT, DISPATCHER_CLASS, mocks, singletonMocks);
}

/**
Expand Down Expand Up @@ -362,6 +367,54 @@ public MockHandler getHandler(Object mock) {
return adapter != null ? adapter.getHandler() : null;
}

@Override
public <T> SingletonMockControl<T> createSingletonMock(T instance, MockCreationSettings<T> settings, MockHandler handler) {
Class<T> typeToMock = settings.getTypeToMock();
Set<Class<?>> interfacesSet = settings.getExtraInterfaces();

classTransformer.mockClass(MockFeatures.withMockFeatures(typeToMock, interfacesSet));

InvocationHandlerAdapter handlerAdapter = new InvocationHandlerAdapter(handler);

return new InlineSingletonMockControl<>(instance, handlerAdapter);
}

private class InlineSingletonMockControl<T> implements SingletonMockControl<T> {
private final T instance;
private final InvocationHandlerAdapter handlerAdapter;

InlineSingletonMockControl(T instance, InvocationHandlerAdapter handlerAdapter) {
this.instance = instance;
this.handlerAdapter = handlerAdapter;
}

@Override
public T getInstance() {
return instance;
}

@Override
public void enable() {
Map<Object, InvocationHandlerAdapter> singletons = singletonMocks.get();
if (singletons == null) {
singletons = new IdentityHashMap<>();
singletonMocks.set(singletons);
}
singletons.put(instance, handlerAdapter);
}

@Override
public void disable() {
Map<Object, InvocationHandlerAdapter> singletons = singletonMocks.get();
if (singletons != null) {
singletons.remove(instance);
if (singletons.isEmpty()) {
singletonMocks.remove();
}
}
}
}

/**
* Get the {@link InvocationHandlerAdapter} registered for a mock.
*
Expand All @@ -374,7 +427,14 @@ private InvocationHandlerAdapter getInvocationHandlerAdapter(Object instance) {
return null;
}

return mocks.get(instance);
InvocationHandlerAdapter adapter = mocks.get(instance);
if (adapter == null) {
Map<Object, InvocationHandlerAdapter> singletons = singletonMocks.get();
if (singletons != null) {
adapter = singletons.get(instance);
}
}
return adapter;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static String resolveAgentPath(ClassLoader cl) throws IOException {
copiedAgent.deleteOnExit();

InputStream is;
if (path != null) {
if (path != null && new File(path).isFile()) {
is = new FileInputStream(path);
} else {
// findLibrary returned null — try loading from APK resources
Expand Down
Loading
Loading