Skip to content

Instantly share code, notes, and snippets.

@RyanNutt
Last active October 4, 2015 19:52
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 RyanNutt/38471c023624131a0f84 to your computer and use it in GitHub Desktop.
Save RyanNutt/38471c023624131a0f84 to your computer and use it in GitHub Desktop.
I use a lot of parameterized JUnit tests in class and got tired of copying and pasting an existing full test file and then deleting the pieces I don't need. This one is pretty much cut down to the minimum.
import java.lang.reflect.Field;
import java.util.*;
import java.io.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith( Parameterized.class )
@SuppressWarnings( "unchecked" )
public class _CLASS_Test {
private String a;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
public _CLASS_Test( String a ) {
this.a = a;
}
@Parameters
public static List<String[]> params() {
return Arrays.asList( new String[][]{
{ "Chicken" }
} );
}
@Test( timeout = 250 )
public void test() throws Exception {
}
private static List<String[]> toList(String words) {
List<String[]> list = new ArrayList<>();
String[] ray = words.split(",");
for (String s: ray) {
list.add(new String[]{s});
}
return list;
}
/**
* Turns on stdOut output capture
*/
private void captureOut() {
System.setOut( new PrintStream( outContent ) );
}
/**
* Turns on stdErr output capture
*/
private void captureErr() {
System.setErr( new PrintStream( errContent ) );
}
/**
* Turns off stdOut capture and returns the contents
* that have been captured
*
* @return
*/
private String getOut() {
System.setOut( new PrintStream( new FileOutputStream( FileDescriptor.out ) ) );
return outContent.toString().replaceAll( "\r", "" );
}
/**
* Turns off stdErr capture and returns the contents
* that have been captured
*
* @return
*/
private String getErr() {
System.setErr( new PrintStream( new FileOutputStream( FileDescriptor.out ) ) );
return errContent.toString().replaceAll( "\r", "" );
}
private <T> T getField( Object instance, String fieldName ) throws Exception {
Field fld = instance.getClass().getDeclaredField( fieldName );
fld.setAccessible( true );
return (T) fld.get( instance );
}
private void setField( Object instance, String fieldName, Object value ) throws Exception {
Field fld = instance.getClass().getDeclaredField( fieldName );
fld.setAccessible( true );
if ( value instanceof Integer ) {
fld.setInt( instance, (int) value );
}
else {
fld.set( instance, value );
}
}
}
@RyanNutt
Copy link
Author

A few things probably need to be changed to make this usable.

expected is the correct answer. a is the parameter being passed to the method you're testing. You'll need to change each to the correct type if they're not ints. You can also add more parameters if one is not enough.

You'll also want to change CLASSNAME to whatever the name of the actual class you're testing is.

@RyanNutt
Copy link
Author

RyanNutt commented Oct 4, 2015

Added a couple of updates.

First is a set of methods that will turn on output capture for stdOut and stdErr and sister methods that will turn back off capture for both and return the contents as a string. Both the getOut and getErr methods also strip out any \r characters.

Also put in a few reflection methods to set and get instance variables to test getters and setters.

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