0
public void testAdd() {

    /* Maths is the class in the source code containing add method() that i am trying to test here */
    Maths math = new Maths();

    String name = new Object(){}.getClass().getEnclosingMethod().getName();
    System.out.println(name);
    assertEquals(6.0, math.add(6.0, 0.0), DELTA);
    assertEquals(0.0, math.add(0.0, 0.0), DELTA);
    assertEquals(2.0, math.add(-5.0, 7.0), DELTA);
    assertEquals(-11.0, math.add(-5.0, -6.0), DELTA);
}

This is just a sample code.

I want to model Junit test cases to the source code method.

Like here if the given method is testAdd then i want to get all the methods names that this "testAdd" method is calling. Mainly i am interested to get math.add method details that is defined in source code.

Please let me know for any clarification.

Rohit Raj
  • 1
  • 3
  • `getEnclosingMethod()` only works for inner classes. I think it doesn't at all do what you assume it would be doing. In that sense: I have absolutely no idea what you are trying to achieve here. In other words: maybe you want to explain the **actual** problem to use that you intend to solve using this mechanism. And, off topic: there is a valid best practice that says: do not use more than one assert statement per test. – GhostCat Jun 03 '16 at 09:14
  • possible answer: http://stackoverflow.com/a/13659703/1581226 – qwerty Jun 03 '16 at 09:16
  • Use JUnit. It will "print" tests names for you and you won't have to do this manually. – Jaroslaw Pawlak Jun 03 '16 at 09:42
  • `new Throwable().getStackTrace()[0].getMethodName()` would give you the current method's name. [Ideone demo](http://ideone.com/4MrxDO). – Andy Turner Jun 03 '16 at 09:44

0 Answers0