Test für Methodenparameter erstellt

This commit is contained in:
Enrico Schrödter 2016-06-03 10:57:48 +02:00
parent b6df854cc6
commit 5821839cbc
2 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,14 @@
class VariableMultimethods{
public Integer method(Integer z, Integer x, Integer y){
return x+y+z;
}
public Integer method(Integer x, Integer y){
return x+y;
}
public Integer method(Integer y){
return y;
}
}

View File

@ -0,0 +1,98 @@
package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import org.junit.Test;
import org.junit.Ignore;
import bytecode.SourceFileBytecodeTest;
public class VariableMultimethodsTest extends SourceFileBytecodeTest{
@Override
protected void init() {
testName = "VariableMultimethods";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
}
@Test
public void testConstruct() throws Exception{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
assertTrue(true);
}
@Test
public void testOneArgument() throws Exception{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
Integer y = 1;
Class[] params = new Class[]{
y.getClass()
};
Method method = cls.getDeclaredMethod("method", params);
Integer returnValue = (Integer) method.invoke(obj, y);
assertEquals(new Integer(1), returnValue);
}
@Test
public void testTwoArgument() throws Exception{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
Integer x = 1;
Integer y = 2;
Class[] params = new Class[]{
x.getClass(),
y.getClass()
};
Method method = cls.getDeclaredMethod("method", params);
Integer returnValue = (Integer) method.invoke(obj, x, y);
assertEquals(new Integer(3), returnValue);
}
@Test
public void testThreeArgument() throws Exception{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
Integer z = 1;
Integer x = 2;
Integer y = 4;
Class[] params = new Class[]{
z.getClass(),
x.getClass(),
y.getClass()
};
Method method = cls.getDeclaredMethod("method", params);
Integer returnValue = (Integer) method.invoke(obj, z, x, y);
assertEquals(new Integer(7), returnValue);
}
}