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
rainerw
committed
Feb 1, 2012
1 parent
ff40e6a
commit 49c626a
Showing
8 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
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,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>bn-testing</artifactId> | ||
<groupId>de.bitnoise.testing</groupId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>bn-testing-common</artifactId> | ||
<name>Bitnoise Testing Common Helpers</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.10</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.easytesting</groupId> | ||
<artifactId>fest-assert</artifactId> | ||
<version>1.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>1.9.0</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
|
||
</project> |
59 changes: 59 additions & 0 deletions
59
bn-testing-common/src/main/java/de/bitnoise/testing/junit/rules/BeforeAfterRule.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,59 @@ | ||
package de.bitnoise.testing.junit.rules; | ||
|
||
import org.junit.rules.MethodRule; | ||
import org.junit.runners.model.FrameworkMethod; | ||
import org.junit.runners.model.Statement; | ||
|
||
public class BeforeAfterRule implements MethodRule | ||
{ | ||
|
||
@Override | ||
public Statement apply(final Statement base, final FrameworkMethod method, | ||
final Object target) | ||
{ | ||
return new Statement() | ||
{ | ||
@Override | ||
public void evaluate() throws Throwable | ||
{ | ||
RuleDescriptor descriptor = new RuleDescriptorImpl(method, target); | ||
onBefore(descriptor); | ||
try | ||
{ | ||
base.evaluate(); | ||
onSuccess(descriptor); | ||
} | ||
catch (Throwable t) | ||
{ | ||
if (onError(descriptor, t)) | ||
{ | ||
throw t; | ||
} | ||
} | ||
finally | ||
{ | ||
onFinally(descriptor); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
protected void onFinally(RuleDescriptor descriptor) throws Throwable | ||
{ | ||
} | ||
|
||
protected boolean onError(RuleDescriptor descriptor, Throwable t) | ||
throws Throwable | ||
{ | ||
return true; | ||
} | ||
|
||
protected void onSuccess(RuleDescriptor descriptor) throws Throwable | ||
{ | ||
} | ||
|
||
protected void onBefore(RuleDescriptor descriptor) throws Throwable | ||
{ | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
bn-testing-common/src/main/java/de/bitnoise/testing/junit/rules/RuleDescriptor.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,8 @@ | ||
package de.bitnoise.testing.junit.rules; | ||
|
||
public interface RuleDescriptor | ||
{ | ||
|
||
Object getTarget(); | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
bn-testing-common/src/main/java/de/bitnoise/testing/junit/rules/RuleDescriptorImpl.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,23 @@ | ||
package de.bitnoise.testing.junit.rules; | ||
|
||
import org.junit.runners.model.FrameworkMethod; | ||
|
||
public class RuleDescriptorImpl implements RuleDescriptor | ||
{ | ||
FrameworkMethod _method; | ||
|
||
Object _target; | ||
|
||
public RuleDescriptorImpl(FrameworkMethod method, Object target) | ||
{ | ||
_method = method; | ||
_target = target; | ||
} | ||
|
||
@Override | ||
public Object getTarget() | ||
{ | ||
return _target; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
bn-testing-common/src/main/java/de/bitnoise/testing/mockito/MockitoRule.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,20 @@ | ||
package de.bitnoise.testing.mockito; | ||
|
||
import org.mockito.MockitoAnnotations; | ||
|
||
import de.bitnoise.testing.junit.rules.BeforeAfterRule; | ||
import de.bitnoise.testing.junit.rules.RuleDescriptor; | ||
|
||
/** | ||
* Rule to run process @Mock with Mockito | ||
*/ | ||
public class MockitoRule extends BeforeAfterRule | ||
{ | ||
|
||
@Override | ||
protected void onBefore(RuleDescriptor descriptor) throws Throwable | ||
{ | ||
MockitoAnnotations.initMocks(descriptor.getTarget()); | ||
} | ||
|
||
} |
123 changes: 123 additions & 0 deletions
123
bn-testing-common/src/test/java/de/bitnoise/testing/junit/rules/BeforeAfterRuleTest.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,123 @@ | ||
package de.bitnoise.testing.junit.rules; | ||
|
||
import org.fest.assertions.Fail; | ||
import org.junit.Rule; | ||
import static org.fest.assertions.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
import org.junit.Test; | ||
import org.junit.runners.model.FrameworkMethod; | ||
import org.junit.runners.model.Statement; | ||
import org.mockito.Mock; | ||
|
||
import de.bitnoise.testing.mockito.MockitoRule; | ||
|
||
public class BeforeAfterRuleTest | ||
{ | ||
int _onBefore; | ||
|
||
int _onError; | ||
|
||
int _onFinally; | ||
|
||
int _onSuccess; | ||
|
||
boolean _errorReturn = true; | ||
|
||
BeforeAfterRule sut = new BeforeAfterRule() | ||
{ | ||
|
||
protected void onBefore(RuleDescriptor descriptor) throws Throwable | ||
{ | ||
_onBefore++; | ||
super.onBefore(descriptor); | ||
}; | ||
|
||
protected boolean onError(RuleDescriptor descriptor, Throwable t) | ||
throws Throwable | ||
{ | ||
_onError++; | ||
super.onError(descriptor, t); | ||
return _errorReturn; | ||
}; | ||
|
||
protected void onFinally(RuleDescriptor descriptor) throws Throwable | ||
{ | ||
_onFinally++; | ||
super.onFinally(descriptor); | ||
}; | ||
|
||
protected void onSuccess(RuleDescriptor descriptor) throws Throwable | ||
{ | ||
_onSuccess++; | ||
super.onSuccess(descriptor); | ||
}; | ||
}; | ||
|
||
@Rule | ||
public MockitoRule mockito = new MockitoRule(); | ||
|
||
@Mock | ||
Statement statement; | ||
|
||
@Mock | ||
FrameworkMethod method; | ||
|
||
Object target = new Object(); | ||
|
||
@Test | ||
public void noError() throws Throwable | ||
{ | ||
// execute | ||
sut.apply(statement, method, target).evaluate(); | ||
|
||
// verify | ||
verify(statement).evaluate(); | ||
assertThat(_onBefore).isEqualTo(1); | ||
assertThat(_onError).isEqualTo(0); | ||
assertThat(_onFinally).isEqualTo(1); | ||
assertThat(_onSuccess).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void hasError() throws Throwable | ||
{ | ||
// prepare / verify | ||
Throwable error = new IllegalArgumentException(); | ||
doThrow(error).when(statement).evaluate(); | ||
|
||
// execute | ||
try | ||
{ | ||
sut.apply(statement, method, target).evaluate(); | ||
Fail.fail(); | ||
} | ||
catch (IllegalArgumentException actual) | ||
{ | ||
assertThat(actual).isSameAs(error); | ||
} | ||
|
||
// verify | ||
assertThat(_onBefore).isEqualTo(1); | ||
assertThat(_onError).isEqualTo(1); | ||
assertThat(_onFinally).isEqualTo(1); | ||
assertThat(_onSuccess).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void hasSupressError() throws Throwable | ||
{ | ||
// prepare / verify | ||
_errorReturn=false; | ||
doThrow(new IllegalArgumentException()).when(statement).evaluate(); | ||
|
||
// execute | ||
sut.apply(statement, method, target).evaluate(); | ||
|
||
// verify | ||
assertThat(_onBefore).isEqualTo(1); | ||
assertThat(_onError).isEqualTo(1); | ||
assertThat(_onFinally).isEqualTo(1); | ||
assertThat(_onSuccess).isEqualTo(0); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
bn-testing-common/src/test/java/de/bitnoise/testing/mockito/MockitoRuleTest.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,36 @@ | ||
package de.bitnoise.testing.mockito; | ||
|
||
import static org.fest.assertions.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.exceptions.verification.NoInteractionsWanted; | ||
|
||
public class MockitoRuleTest | ||
{ | ||
|
||
@Rule | ||
public MockitoRule sut = new MockitoRule(); | ||
|
||
@Mock | ||
List<String> mock; | ||
|
||
@Test | ||
public void verifyAtMockWorked() | ||
{ | ||
assertThat(mock).isNotNull(); | ||
} | ||
|
||
@Test(expected = NoInteractionsWanted.class) | ||
public void verifyNoMoreIntaractions() | ||
{ | ||
when(mock.add("value")).thenReturn(true); | ||
mock.add("value"); | ||
mock.add("value2"); | ||
verifyNoMoreInteractions(mock); | ||
} | ||
} |
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