I'm learning about reflection in java and I tried a simple reflected class, but an exception happened:
java.lang.NoSuchMethodException: it.accaemme.exercises.MaxDiTre.MaxDiTre.absDigit(java.lang.Integer)
Any suggest? Thanks
Here code:
/* /src/main/java/.../ */
/* filename: MaxDiTre.java */
package it.accaemme.exercises.MaxDiTre;
public class MaxDiTre {
public static int max(int num1, int num2, int num3) {
/* some stupid stuffs */
}
private int absDigit(int n) {
return Math.abs(n);
}
}
/* ------------ */
public class TestMaxDiTre {
/*...*/
// Test16: class test
MaxDiTre mdt;
@Before
public void AlphaTest() {
mdt = new MaxDiTre();
}
// non funziona
// Test17: test private method: absDigit()
@Test
public void testPrivateMethod_absDigit(){
Method m;
try {
//Class<?> argTypes = new Class() { Integer.class }
//Class<?> argTypes = Integer.class;
//Class<?> argTypes = null;
//@SuppressWarnings("deprecation")
//Class<int>[] argTypes = null;
//Class<?>[] argTypes = (Class<?>[]) null;
//Class<?>[] argTypes = null;
//int argTypes = (Class<?>[])8;
//m = MaxDiTre.class.getDeclaredMethod("absDigit", argTypes );
m = mdt.getClass().getDeclaredMethod("absDigit", argTypes);
m.setAccessible(true);
assertEquals(
8,
(int)m.invoke(
mdt, -8
)
);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Aucun commentaire:
Enregistrer un commentaire