0

While studying another person's code, I've come across this:

    public class TestFailedError extends AssertionError {  

        private final String testName;  
        private final String testData; 

        public TestFailedError(final String testName, final String message) {  
            super(getDetailMessage(testName, message));
            this.testName = testName;
            this.testData = testName;
        } 
  private static String getDetailMessage(String testData, String message) {
        return "Test case " +testData+ " failed :"+ message;
        }
    }

I don't understand the point of super() here. Reading up on super didn't help. Reason the linked question didn't help is because it provides examples of super.variable and super.method, while mine was super(method), so wasn't entirely sure.

Community
  • 1
  • 1
Andrejs
  • 10,803
  • 4
  • 43
  • 48
  • 1
    The answer you referred is very clear and simple? What is it exactly you don't understand? – Sleiman Jneidi Feb 24 '16 at 21:11
  • 1
    `super` is referring to the super-class in this case `AssertionError`. In this particular case it is calling the constructor of `AssertionError`. – brso05 Feb 24 '16 at 21:11
  • 1
    **What** do you not understand? – Raedwald Feb 24 '16 at 21:12
  • Dear downvoter, I've explicitly linked to a different question trying to show I did some research. – Andrejs Feb 24 '16 at 21:14
  • Calling the [`Exception(String message)`](https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html#Exception(java.lang.String)) constructor is a common idiom when you're writing custom exceptions. This way the detail message of the exception shows nicely in logs etc. – Mick Mnemonic Feb 24 '16 at 21:15

1 Answers1

2

super(getDetailMessage(testName, message)); is calling to AssertionError constructor, which TestFailedError is inherits from, and will save the String it received. It will look like

public AssertionError(String str) {
    // save str to local member
}

The str variable will contain the return string from the method getDetailMessage.

Guy
  • 46,488
  • 10
  • 44
  • 88