Skip to content

Instantly share code, notes, and snippets.

@josefbetancourt
josefbetancourt / ClasspathTestScanner
Last active August 29, 2015 14:24
Simple method to search Java classpath to find @test annotated JUnit tests
/**
* Get all test classes starting from a base package path.
* <p>
* Example use:
* <pre>
* UnitTestScanner scanner = new UnitTestScanner();
* List<Class<?>> list = scanner.getTestClasses("com.octodecillion.test");
* </pre>
* @param basePackage package where search will start
* @param debug if true output more info
@josefbetancourt
josefbetancourt / RequiresTestAnnotationScanner.java
Last active August 29, 2015 14:25
Scan classpath for classes with @RequiresTest annotated methods
package com.octodecillion.test;
import java.lang.reflect.Method;
import java.util.List;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
@josefbetancourt
josefbetancourt / FubarClassExample.java
Created November 30, 2011 09:19
Sample class for illustrating unit testing
public class Fubar {
private boolean launch = true;
public void getLaunch(){
return this.launch;
}
public void setLaunch(boolean launch){
/* this.launch = launch; */ // broken!
}
@josefbetancourt
josefbetancourt / Vsdf.java
Last active October 13, 2015 05:57
Very simple data file in Java source with embedded JUnit test. Presented in "Simple Java Data File" blog post.
/** Vsdf.java */
package com.octodecillion.vsdf;
import static com.octodecillion.vsdf.Vsdf.EventType.END;
import static org.hamcrest.core.DescribedAs.describedAs;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@josefbetancourt
josefbetancourt / InvokeCounter.java
Last active October 13, 2015 11:48
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;
/**
@josefbetancourt
josefbetancourt / JarInfo.java
Created December 15, 2012 00:18
Source code from blog post "Java Generics Example: Jar Manifest" at [http://octodecillion.com/blog/java-generics-manifest/]
/**
*
*/
package com.octodecillion.utils;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
package com.octodecillion.util;
import java.util.Map;
import java.util.Map.Entry;
/**
* A {@link Map} decorator that allows
* <a href="https://en.wikipedia.org/wiki/Fluent_interface#Java">Fluent</a>
* add of entries.<p>
*
package com.octodecillion.util;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
package com.octodecillion.util;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
package com.octodecillion.util;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;