Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3160 : Fix interference between spies when spying on records. #3173

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class InlineDelegateByteBuddyMockMaker
private final DetachedThreadLocal<Map<Class<?>, BiConsumer<Object, MockedConstruction.Context>>>
mockedConstruction = new DetachedThreadLocal<>(DetachedThreadLocal.Cleaner.MANUAL);

private final ThreadLocal<Boolean> mockitoConstruction = ThreadLocal.withInitial(() -> false);
private final ThreadLocal<Class<?>> currentMocking = ThreadLocal.withInitial(() -> null);

private final ThreadLocal<Object> currentSpied = new ThreadLocal<>();

Expand Down Expand Up @@ -272,7 +272,9 @@ class InlineDelegateByteBuddyMockMaker
type -> {
if (isSuspended.get()) {
return false;
} else if (mockitoConstruction.get() || currentConstruction.get() != null) {
} else if ((currentMocking.get() != null
&& type.isAssignableFrom(currentMocking.get()))
|| currentConstruction.get() != null) {
return true;
}
Map<Class<?>, ?> interceptors = mockedConstruction.get();
Expand All @@ -290,7 +292,7 @@ class InlineDelegateByteBuddyMockMaker
};
ConstructionCallback onConstruction =
(type, object, arguments, parameterTypeNames) -> {
if (mockitoConstruction.get()) {
if (currentMocking.get() != null) {
Object spy = currentSpied.get();
if (spy == null) {
return null;
Expand Down Expand Up @@ -647,11 +649,11 @@ public <T> T newInstance(Class<T> cls) throws InstantiationException {
accessor.newInstance(
selected,
callback -> {
mockitoConstruction.set(true);
currentMocking.set(cls);
try {
return callback.newInstance();
} finally {
mockitoConstruction.set(false);
currentMocking.remove();
}
},
arguments);
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2023 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.java21;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;

import java.util.List;

import org.junit.Test;

public class RecordTest {

@Test
public void given_list_is_already_spied__when_spying_record__then_record_fields_are_correctly_populated() {
var ignored = spy(List.of());

record MyRecord(String name) {
}
MyRecord spiedRecord = spy(new MyRecord("something"));

assertThat(spiedRecord.name()).isEqualTo("something");
}
}