Skip to content

Commit

Permalink
added bn-common
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerw committed Feb 1, 2012
1 parent ff40e6a commit 49c626a
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 0 deletions.
35 changes: 35 additions & 0 deletions bn-testing-common/pom.xml
@@ -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>
@@ -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
{
}

}
@@ -0,0 +1,8 @@
package de.bitnoise.testing.junit.rules;

public interface RuleDescriptor
{

Object getTarget();

}
@@ -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;
}

}
@@ -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());
}

}
@@ -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);
}

}
@@ -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);
}
}
9 changes: 9 additions & 0 deletions pom.xml
Expand Up @@ -6,6 +6,7 @@
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>bn-testing-db</module>
<module>bn-testing-common</module>
</modules>
<packaging>pom</packaging>

Expand All @@ -27,6 +28,14 @@
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<useProjectReferences>true</useProjectReferences>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
</plugins>
</build>

Expand Down

0 comments on commit 49c626a

Please sign in to comment.