-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
johan.haleby
committed
Sep 22, 2009
1 parent
865530c
commit 32b5d26
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
api/mockito/src/main/java/org/powermock/api/mockito/expectation/PowerMockitoStubber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2009 the original author or authors. | ||
* | ||
* 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 org.powermock.api.mockito.expectation; | ||
|
||
import org.mockito.Mockito; | ||
import org.mockito.stubbing.Answer; | ||
import org.mockito.stubbing.Stubber; | ||
|
||
/** | ||
*Setup stubbing for void methods in final class, final void methods, or static | ||
* (final) methods. Note that for private void methods you should use the | ||
* standard <code>when</code> method in <code>PowerMockito</code>. | ||
*/ | ||
public interface PowerMockitoStubber extends Stubber { | ||
/** | ||
* Allows to choose a static void method when stubbing in | ||
* doThrow()|doAnswer()|doNothing()|doReturn() style | ||
* <p> | ||
* Example: | ||
* | ||
* <pre> | ||
* doThrow(new RuntimeException()).when(); | ||
* StaticList.clear(); | ||
* | ||
* //following throws RuntimeException: | ||
* StaticList.clear(); | ||
* </pre> | ||
* | ||
* Read more about those methods: | ||
* <p> | ||
* {@link Mockito#doThrow(Throwable)} | ||
* <p> | ||
* {@link Mockito#doAnswer(Answer)} | ||
* <p> | ||
* {@link Mockito#doNothing()} | ||
* <p> | ||
* {@link Mockito#doReturn(Object)} | ||
* <p> | ||
* | ||
* See examples in javadoc for {@link Mockito} | ||
*/ | ||
void when(Class<?> classMock); | ||
} |
37 changes: 37 additions & 0 deletions
37
api/mockito/src/main/java/org/powermock/api/mockito/internal/PowerMockitoCore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2009 the original author or authors. | ||
* | ||
* 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 org.powermock.api.mockito.internal; | ||
|
||
import org.mockito.Mockito; | ||
import org.mockito.internal.progress.MockingProgress; | ||
import org.mockito.stubbing.Answer; | ||
import org.powermock.api.mockito.expectation.PowerMockitoStubber; | ||
import org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl; | ||
import org.powermock.reflect.Whitebox; | ||
|
||
public class PowerMockitoCore { | ||
@SuppressWarnings("unchecked") | ||
public PowerMockitoStubber doAnswer(Answer answer) { | ||
getMockingProgress().stubbingStarted(); | ||
getMockingProgress().resetOngoingStubbing(); | ||
return (PowerMockitoStubber) new PowerMockitoStubberImpl().doAnswer(answer); | ||
} | ||
|
||
private MockingProgress getMockingProgress() { | ||
return Whitebox.getInternalState(Mockito.class, MockingProgress.class); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...src/main/java/org/powermock/api/mockito/internal/expectation/PowerMockitoStubberImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2009 the original author or authors. | ||
* | ||
* 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 org.powermock.api.mockito.internal.expectation; | ||
|
||
import java.util.List; | ||
|
||
import org.mockito.internal.stubbing.StubberImpl; | ||
import org.mockito.stubbing.Stubber; | ||
import org.powermock.api.mockito.expectation.PowerMockitoStubber; | ||
import org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl; | ||
import org.powermock.core.MockRepository; | ||
import org.powermock.reflect.Whitebox; | ||
|
||
/** | ||
* Extension of the standard Mocktio stubber implementation that also support | ||
* PowerMockito created mocks. | ||
*/ | ||
public class PowerMockitoStubberImpl extends StubberImpl implements PowerMockitoStubber { | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public void when(Class<?> classMock) { | ||
MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository | ||
.getStaticMethodInvocationControl(classMock); | ||
addAnswersForStubbing(invocationControl); | ||
} | ||
|
||
/** | ||
* Supports PowerMockito mocks. If <code>mock</code> is not a PowerMockito | ||
* mock it will delegate to Mockito. | ||
* | ||
* @see Stubber#when(Object) | ||
*/ | ||
@Override | ||
public <T> T when(T instanceMock) { | ||
MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository | ||
.getInstanceMethodInvocationControl(instanceMock); | ||
final T returnValue; | ||
if (invocationControl == null) { | ||
returnValue = super.when(instanceMock); | ||
} else { | ||
addAnswersForStubbing(invocationControl); | ||
returnValue = instanceMock; | ||
} | ||
return returnValue; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void addAnswersForStubbing(MockitoMethodInvocationControl invocationControl) { | ||
invocationControl.getInvocationHandler().getMockHandler().setAnswersForStubbing(Whitebox.getInternalState(this, List.class)); | ||
} | ||
} |