org.mockito.internal.progress.ThreadSafeMockingProgress Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-core Show documentation
Show all versions of mockito-core Show documentation
Mockito mock objects library core API and implementation
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.progress;
/**
* Provides access to the {@link MockingProgress} of a corresponding {@link Thread}. Every {@link Thread} in Mockito has it s own {@link MockingProgress} to avoid data races while stubbing.
*/
public class ThreadSafeMockingProgress {
private static final ThreadLocal MOCKING_PROGRESS_PROVIDER =
new ThreadLocal() {
@Override
protected MockingProgress initialValue() {
return new MockingProgressImpl();
}
};
private ThreadSafeMockingProgress() {}
/**
* Returns the {@link MockingProgress} for the current Thread.
*
* IMPORTANT: Never assign and access the returned {@link MockingProgress} to an instance or static field. Thread safety can not be guaranteed in this case, cause the Thread that wrote the field might not be the same that read it. In other words multiple threads will access the same {@link MockingProgress}.
*
* @return never null
*/
public static final MockingProgress mockingProgress() {
return MOCKING_PROGRESS_PROVIDER.get();
}
}