Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright (c) 2015,
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.
package com.github.nwillc.contracts;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;

This contract checks:
  • compare with argument that can't be cast to correct type throws ClassCastException
  • returns 0 on equality
  • returns less then zero on less then
  • returns greater then zero on greater
No assumptions about null values are made, the jdk contract does not specify any.

public abstract class ComparatorContract<T> {
	private Comparator<T> comparator;
	private T value;
	private T lesserValue;
	protected abstract Comparator<T> getComparator();
	protected abstract T getValue();
	protected abstract T getLesserValue();
	public void contractSetup() throws Exception {
		assertThat().describedAs("getComparator must return non null value").isNotNull();
	public void shouldThrowExceptionForBadCast() throws Exception {
		try {
catch (ClassCastException e) {}
	public void shouldReturnZeroOnEquality() throws Exception {
	public void shouldReturnNegativeOnLessThan() throws Exception {
	public void shouldReturnPositiveOnGreaterThan() throws Exception {
New to GrepCode? Check out our FAQ X