Here you write a java code used for search. You can use the wildcard character (_) for class or method names and the generic types (E,K,N,T,V) for types.
Here you write your test cases using the JUnit framework and Java's reflection feature. Read the examples carefully before you try your own test cases!
public class Stack{
public void push(Object o){}
public Object pop(){}
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.lang.Object;
import java.lang.reflect.*;
public class StackTest{
@Test
public void testPushPop() throws Exception{
// class name
Class<?> clazz = Class.forName("Stack");
// create a class instance
// (when there is no constructor or the constructor doesn't have any arguments)
Object c = clazz.newInstance();
// m_index stores the index of the method that's going to be invoked
// the index stated is its position in source file
int m_index = 0;
// get a method instance using its name and argument types (fully qualified name)
Method m = clazz.getDeclaredMethod("push", java.lang.Object.class);
// call the method
m.invoke(c, 3);
m.invoke(c, 4);
// index of next method to be invoked
m_index = 1;
m = clazz.getDeclaredMethod("pop");
assertEquals("Wrong answer!", 4, m.invoke(c));
}
}
public class ComplexNumber{
public ComplexNumber(double i, double j){}
public double getRealPart(){}
public double getImaginaryPart(){}
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.lang.reflect.*;
public class ComplexNumberTest{
@Test
public void testComplexNumber() throws Exception{
// the string used here is the source code's class name
Class<?> clazz = Class.forName("ComplexNumber");
// create a class instance
// constructor with arguments
Constructor ct = clazz.getDeclaredConstructor(double.class, double.class);
Object c = ct.newInstance(1.0,2.0);
// m_index stores the index of the method that's going to be invoked
// the index stated is its position in source file
int m_index = 1;
// get a method instance using its name and argument types (fully qualified name)
Method m = clazz.getDeclaredMethod("getRealPart");
assertEquals("Wrong answer!", 1.0, m.invoke(c));
// index of next method to be invoked
m_index = 2;
m = clazz.getDeclaredMethod("getImaginaryPart");
assertEquals("Wrong answer!", 2.0, m.invoke(c));
}
}
public class _{
public int gcd(int i, int j){}
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.lang.reflect.*;
public class GcdTest{
@Test
public void testGcd1() throws Exception{
// class name
Class<?> clazz = Class.forName("_");
// create a class instance
// (when there is no constructor or the constructor doesn't have any arguments)
Object c = clazz.newInstance();
// m_index stores the index of the method that's going to be invoked
// the index stated is its position in source file
int m_index = 0;
// get a method instance using its name and argument types (fully qualified name)
Method m = clazz.getDeclaredMethod("gcd", int.class, int.class);
assertEquals("Wrong answer!", 1, m.invoke(c, 1, 3));
}
// add more test cases
@Test
public void testGcd2() throws Exception{
Class<?> clazz = Class.forName("_");
Object c = clazz.newInstance();
int m_index = 0;
Method m = clazz.getDeclaredMethod("gcd", int.class, int.class);
assertEquals("Wrong answer!", 1, m.invoke(c, 4, 7));
}
}