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

MockingProgress: replaced getVerificationListeners() with fireVerificationEvent(..) #965

Closed
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
6 changes: 1 addition & 5 deletions src/main/java/org/mockito/internal/MockitoCore.java
Expand Up @@ -23,11 +23,9 @@

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.mockito.InOrder;
import org.mockito.MockSettings;
import org.mockito.MockingDetails;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.misusing.NotAMockException;
import org.mockito.internal.creation.MockSettingsImpl;
import org.mockito.internal.invocation.finder.VerifiableInvocationsFinder;
Expand All @@ -43,7 +41,6 @@
import org.mockito.internal.verification.api.VerificationDataInOrder;
import org.mockito.internal.verification.api.VerificationDataInOrderImpl;
import org.mockito.invocation.Invocation;
import org.mockito.listeners.VerificationListener;
import org.mockito.mock.MockCreationSettings;
import org.mockito.stubbing.OngoingStubbing;
import org.mockito.stubbing.Stubber;
Expand All @@ -70,7 +67,6 @@ public <T> T mock(Class<T> typeToMock, MockSettings settings) {
public <T> OngoingStubbing<T> when(T methodCall) {
MockingProgress mockingProgress = mockingProgress();
mockingProgress.stubbingStarted();
@SuppressWarnings("unchecked")
OngoingStubbing<T> stubbing = (OngoingStubbing<T>) mockingProgress.pullOngoingStubbing();
if (stubbing == null) {
mockingProgress.reset();
Expand All @@ -88,7 +84,7 @@ public <T> T verify(T mock, VerificationMode mode) {
}
MockingProgress mockingProgress = mockingProgress();
VerificationMode actualMode = mockingProgress.maybeVerifyLazily(mode);
mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, actualMode, mockingProgress.verificationListeners()));
mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, actualMode, mockingProgress));
return mock;
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/mockito/internal/progress/MockingProgress.java
Expand Up @@ -5,11 +5,10 @@

package org.mockito.internal.progress;

import java.util.Set;
import org.mockito.listeners.MockitoListener;
import org.mockito.listeners.VerificationListener;
import org.mockito.mock.MockCreationSettings;
import org.mockito.stubbing.OngoingStubbing;
import org.mockito.verification.VerificationEvent;
import org.mockito.verification.VerificationMode;
import org.mockito.verification.VerificationStrategy;

Expand All @@ -19,7 +18,7 @@ public interface MockingProgress {

OngoingStubbing<?> pullOngoingStubbing();

Set<VerificationListener> verificationListeners();
void fireVerificationEvent(VerificationEvent event);

void verificationStarted(VerificationMode verificationMode);

Expand All @@ -34,8 +33,8 @@ public interface MockingProgress {
void reset();

/**
* Removes ongoing stubbing so that in case the framework is misused
* state validation errors are more accurate
* Removes ongoing stubbing so that in case the framework is misused state
* validation errors are more accurate
*/
void resetOngoingStubbing();

Expand All @@ -55,4 +54,5 @@ public interface MockingProgress {
* Removes all listeners added via {@link #addListener(MockitoListener)}.
*/
void clearListeners();

}
Expand Up @@ -15,6 +15,7 @@
import org.mockito.listeners.VerificationListener;
import org.mockito.mock.MockCreationSettings;
import org.mockito.stubbing.OngoingStubbing;
import org.mockito.verification.VerificationEvent;
import org.mockito.verification.VerificationMode;
import org.mockito.verification.VerificationStrategy;

Expand Down Expand Up @@ -58,19 +59,14 @@ public OngoingStubbing<?> pullOngoingStubbing() {
}

@Override
public Set<VerificationListener> verificationListeners() {
final LinkedHashSet<VerificationListener> verificationListeners = new LinkedHashSet<VerificationListener>();

public void fireVerificationEvent(VerificationEvent event) {
for (MockitoListener listener : listeners) {
if (listener instanceof VerificationListener) {
verificationListeners.add((VerificationListener) listener);
((VerificationListener) listener).onVerification(event);
}
}

return verificationListeners;
}


public void verificationStarted(VerificationMode verify) {
validateState();
resetOngoingStubbing();
Expand Down
Expand Up @@ -4,42 +4,39 @@
*/
package org.mockito.internal.verification;

import java.util.Set;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.verification.api.VerificationData;
import org.mockito.listeners.VerificationListener;
import org.mockito.verification.VerificationEvent;
import org.mockito.verification.VerificationMode;

public class MockAwareVerificationMode implements VerificationMode {

private final Object mock;
private final VerificationMode mode;
private final Set<VerificationListener> listeners;
private final MockingProgress mockingProgress;

public MockAwareVerificationMode(Object mock, VerificationMode mode, Set<VerificationListener> listeners) {
public MockAwareVerificationMode(Object mock, VerificationMode mode, MockingProgress mockingProgress) {
this.mock = mock;
this.mode = mode;
this.listeners = listeners;
this.mockingProgress = mockingProgress;
}

public void verify(VerificationData data) {
try {
mode.verify(data);
notifyListeners(new VerificationEventImpl(mock, mode, data, null));
} catch (RuntimeException e) {
notifyListeners(new VerificationEventImpl(mock, mode, data, e));
fireVerificationEvent( data, e);
throw e;
} catch (Error e) {
notifyListeners(new VerificationEventImpl(mock, mode, data, e));
fireVerificationEvent(data, e);
throw e;
}

fireVerificationEvent(data,null);
}


private void notifyListeners(VerificationEvent event) {
for (VerificationListener listener : listeners) {
listener.onVerification(event);
}
private void fireVerificationEvent(VerificationData data, Throwable error) {
mockingProgress.fireVerificationEvent(new VerificationEventImpl(mock, mode, data, error));
}

public Object getMock() {
Expand Down
Expand Up @@ -5,19 +5,21 @@

package org.mockito.internal.progress;

import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertSame;
import static junit.framework.TestCase.fail;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Before;
import org.junit.Test;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.verification.VerificationEventImpl;
import org.mockito.internal.verification.VerificationModeFactory;
import org.mockito.listeners.VerificationListener;
import org.mockito.verification.VerificationEvent;
import org.mockito.verification.VerificationMode;
import org.mockitoutil.TestBase;

import static junit.framework.TestCase.*;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class MockingProgressImplTest extends TestBase {
public class MockingProgressImplTest {

private MockingProgress mockingProgress;

Expand Down Expand Up @@ -45,15 +47,53 @@ public void shouldCheckIfVerificationWasFinished() throws Exception {
try {
mockingProgress.verificationStarted(VerificationModeFactory.atLeastOnce());
fail();
} catch (MockitoException e) {}
} catch (MockitoException e) {
}
}

@Test
public void shouldNotifyListenerSafely() throws Exception {
//when
// when
mockingProgress.addListener(null);

//then no exception is thrown:
// then no exception is thrown:
mockingProgress.mockingStarted(null, null);
}

@Test
public void fireVerificationEvent_noVerificationListenerRegistered_shouldThrowNoException() {
VerificationEvent event = new VerificationEventImpl(null, null, null, null);

mockingProgress.fireVerificationEvent(event);

}

@Test
public void fireVerificationEvent_verificationListenerRegistered_shouldBeNotified() {

AssertableVerificationListener listener = new AssertableVerificationListener();
mockingProgress.addListener(listener);

VerificationEvent event = new VerificationEventImpl(null, null, null, null);

mockingProgress.fireVerificationEvent(event);

listener.assertLastEventIsSameAs(event);

}

private static class AssertableVerificationListener implements VerificationListener {

private VerificationEvent lastEvent;

@Override
public void onVerification(VerificationEvent verificationEvent) {
lastEvent = verificationEvent;
}

public void assertLastEventIsSameAs(VerificationEvent expected) {
assertThat(lastEvent).isSameAs(expected);
}

}
}