Skip to content

Instantly share code, notes, and snippets.

@josefbetancourt
Last active October 13, 2015 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefbetancourt/4191499 to your computer and use it in GitHub Desktop.
Save josefbetancourt/4191499 to your computer and use it in GitHub Desktop.
Behavior counters for improved JUnit tests
package com.octodecillion.junit;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
/**
* Counters to assert block execution in JUnit tests.
*
* @author jbetancourt
* @since 2012-11-19T20:52-05
*/
public class InvokeCounter {
private final Map<String, Integer> counters = new HashMap<String, Integer>();
private static final String DEFAULT_COUNTER = "DEFAULT__COUNTER";
private static Integer ZERO = Integer.valueOf(0);
{
counters.put(DEFAULT_COUNTER, ZERO);
}
public int get() {
return get(DEFAULT_COUNTER);
}
public void increment() {
incrementValue(DEFAULT_COUNTER);
}
public void increment(int shouldBe) {
increment(DEFAULT_COUNTER, shouldBe);
}
public void reset() {
counters.put(DEFAULT_COUNTER, ZERO);
}
@SuppressWarnings("boxing")
public void assertCounter(int shouldBe) {
Integer value = counters.get(DEFAULT_COUNTER);
assertThat(NOT_WHERE_IT_SHOULD_BE, value, equalTo(shouldBe));
}
@SuppressWarnings("boxing")
public void assertCounterMin(int shouldBe) {
Integer value = counters.get(DEFAULT_COUNTER);
assertTrue(NOT_AT_LEAST_MINIMUM + shouldBe + "'", value >= shouldBe);
}
@SuppressWarnings("boxing")
public void assertCounterMax(int shouldBe) {
Integer value = counters.get(DEFAULT_COUNTER);
assertTrue(NOT_AT_LEAST_MAXIMUM + shouldBe + "'", value <= shouldBe);
}
public void create(String name) {
if (name == null) {
throw new NullPointerException(NAME_IS_NULL);
}
if (name.trim().length() == 0) {
throw new IllegalArgumentException(NAME_IS_BLANK);
}
if (counters.containsKey(name)) {
throw new IllegalStateException(
String.format(ALREADY_CREATED, name));
}
counters.put(name, ZERO);
}
@SuppressWarnings("boxing")
public int get(String name) {
return counters.get(name);
}
public void increment(String name) {
incrementValue(name);
}
public void reset(String name) {
counters.put(name, ZERO);
}
@SuppressWarnings("boxing")
public void increment(String name, int shouldBe) {
Integer value = incrementValue(name);
assertThat(NOT_INCREMENTED_CORRECTLY, value, equalTo(shouldBe));
}
@SuppressWarnings("boxing")
public void assertCounter(String name, int shouldBe) {
Integer value = counters.get(name);
assertThat(NOT_WHERE_IT_SHOULD_BE, value, equalTo(shouldBe));
}
@SuppressWarnings("boxing")
public void assertCounterMin(String name, int shouldBe) {
Integer value = counters.get(name);
assertTrue(NOT_AT_LEAST_MINIMUM + shouldBe + "'", value >= shouldBe);
}
@SuppressWarnings("boxing")
public void assertCounterMax(String name, int shouldBe) {
Integer value = counters.get(name);
assertTrue(NOT_AT_LEAST_MINIMUM + shouldBe + "'", value <= shouldBe);
}
@SuppressWarnings("boxing")
private Integer incrementValue(String name) {
if (name == null || name.trim().length() == 0) {
throw new NullPointerException(INCREMENT_NAME_IS_NULL_OR_BLANK);
}
if (!counters.containsKey(name)) {
throw new IllegalStateException(String.format(NOT_FOUND, name));
}
Integer value = counters.get(name);
value++;
counters.put(name, value);
return value;
}
private static final String ALREADY_CREATED = "Counter '%s' already created";
private static final String NAME_IS_NULL = "counter, create, name is null";
private static final String NAME_IS_BLANK = "counter, create, name is empty";
private static final String NOT_FOUND = "Counter '%s' not found";
private static final String NOT_INCREMENTED_CORRECTLY = "counter was not incremented correctly";
private static final String INCREMENT_NAME_IS_NULL_OR_BLANK = "counter, increment, name is null or blank";
private static final String NOT_AT_LEAST_MINIMUM = "counter is not at least minimum '";
private static final String NOT_AT_LEAST_MAXIMUM = "counter is not at least maximum '";
private static final String NOT_WHERE_IT_SHOULD_BE = "counter is not where it should be";
}
@josefbetancourt
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment