Skip to content

Commit

Permalink
Fixed #726 Mockito Public API Support
Browse files Browse the repository at this point in the history
Clearing code and fixing test.
  • Loading branch information
Arthur Zagretdinov authored and thekingn0thing committed Sep 10, 2017
1 parent 955b5ec commit edb1411
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.txt
Expand Up @@ -575,7 +575,7 @@ Change log 0.9 (released 2008-11-10)
* Fixed a serious issue when the suppress static constructor state disappeared between chunking sessions. Fixed this for all versions of JUnit 4. This means that chunking and suppression of static initializers now work as expected.
* The PowerMock API methods with a syntax like createPartialMockX are renamed to createXPartialMock, e.g. createPartialMockNice is renamed to createNicePartialMock to be more consistent with the normal createMock methods.
* Added createNicePartialMockAndInvokeDefaultConstructor and createStrictPartialMockAndInvokeDefaultConstructor to the PowerMock API.
* Updated the tutorial projects with TODO's. It should now be much easier to understand what shall be done.
* Updated the tutorial projects with
* Changed the clean-up mechanisms of PowerMock.replay(..) and PowerMock.verify(..). Should no longer clear unnecessary state.
* Implemented two new PowerMock API methods: replayAll and verifyAll(). These can be used to replay and verify all classes and mocks created by the PowerMock API without having to explicitly specify each and everyone.

Expand Down
18 changes: 9 additions & 9 deletions gradle/modules.gradle
Expand Up @@ -14,7 +14,7 @@ def skippedTasksForUnpublishableModules = [
'bintrayUpload'
]

def unpublishableModules = allprojects - publishableModules - project(":powermock-release").subprojects
def unpublishableModules = allprojects - publishableModules// - project(":powermock-release").subprojects
configure(unpublishableModules) { project ->
project.tasks.all {
if(skippedTasksForUnpublishableModules.contains(it.name)) {
Expand All @@ -32,14 +32,14 @@ def skippedTasksForGenericModules = [
'sourcesJar'
]

def genericModules = project(":powermock-release").subprojects
configure(genericModules) { project ->
project.tasks.all {
if(skippedTasksForGenericModules.contains(it.name)) {
it.enabled = false
}
}
}
//def genericModules = project(":powermock-release").subprojects
//configure(genericModules) { project ->
// project.tasks.all {
// if(skippedTasksForGenericModules.contains(it.name)) {
// it.enabled = false
// }
// }
//}

if (project.hasProperty('checkJava6Compatibility') && !System.getenv("SKIP_RELEASE")) {
//if we're skipping release, let's also skip checking compatibility (faster builds)
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package samples.powermockito.junit4.verify;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
Expand All @@ -26,9 +27,15 @@
import samples.singleton.StaticHelper;
import samples.singleton.StaticService;

import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.*;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyNew;
import static org.powermock.api.mockito.PowerMockito.verifyNoMoreInteractions;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.whenNew;
import static org.powermock.api.support.membermodification.MemberMatcher.constructor;

/**
Expand All @@ -53,15 +60,13 @@ public void verifyNoMoreInteractionsOnMethodThrowsAssertionErrorWhenMoreInteract
mockStatic(StaticService.class);
assertNull(StaticService.say("hello"));

try {
verifyNoMoreInteractions(StaticService.class);
fail("Should throw exception!");
} catch (MockitoAssertionError e) {
assertTrue(e
.getMessage()
.startsWith(
"\nNo interactions wanted here:\n-> at samples.powermockito.junit4.verifynomoreinteractions.VerifyNoMoreInteractionsTest.verifyNoMoreInteractionsOnMethodThrowsAssertionErrorWhenMoreInteractionsTookPlace(VerifyNoMoreInteractionsTest.java"));
}
assertThatThrownBy(new ThrowingCallable() {
@Override
public void call() throws Throwable {
verifyNoMoreInteractions(StaticService.class);
}
}).hasMessageStartingWith(
"\nNo interactions wanted here:\n-> at samples.powermockito.junit4.verify.VerifyNoMoreInteractionsTest$1.call");
}

@Test
Expand All @@ -73,16 +78,14 @@ public void verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreIn
whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);

tested.simpleMultipleNew();

try {
verifyNoMoreInteractions(MyClass.class);
fail("Should throw exception!");
} catch (MockitoAssertionError e) {
assertTrue(e
.getMessage()
.startsWith(
"\nNo interactions wanted here:\n-> at samples.powermockito.junit4.verifynomoreinteractions.VerifyNoMoreInteractionsTest.verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreInteractionsTookPlace(VerifyNoMoreInteractionsTest.java:"));
}

assertThatThrownBy(new ThrowingCallable() {
@Override
public void call() throws Throwable {
verifyNoMoreInteractions(MyClass.class);
}
}).hasMessageStartingWith(
"\nNo interactions wanted here:\n-> at samples.powermockito.junit4.verify.VerifyNoMoreInteractionsTest$2.call");
}

@Test
Expand Down Expand Up @@ -115,19 +118,15 @@ public void verifyNoMoreInteractionsOnNewInstancesWorksWhenUsingConstructorToExp

@Test
public void verifyNoMoreInteractionsDelegatesToPlainMockitoWhenMockIsNotAPowerMockitoMock() throws Exception {
MyClass myClassMock = Mockito.mock(MyClass.class);
myClassMock.getMessage();

try {
verifyNoMoreInteractions(myClassMock);
fail("Should throw exception!");
} catch (AssertionError e) {
/*
* This string would have been deleted by PowerMockito but should
* exists if delegation took place.
*/
final String expectedTextThatProvesDelegation = "But found this interaction";
assertTrue(e.getMessage().contains(expectedTextThatProvesDelegation));
}
final MyClass myClassMock = Mockito.mock(MyClass.class);
myClassMock.getMessage();

final String expectedTextThatProvesDelegation = "But found this interaction";
assertThatThrownBy(new ThrowingCallable() {
@Override
public void call() throws Throwable {
verifyNoMoreInteractions(myClassMock);
}
}).hasMessageContaining(expectedTextThatProvesDelegation);
}
}
2 changes: 1 addition & 1 deletion version.properties
@@ -1,5 +1,5 @@
#Currently building version
version=2.0.0-RC.1
version=2.0.0-beta.1

#Prevoius version used to generate release notes delta
previousVersion=1.7.1

0 comments on commit edb1411

Please sign in to comment.