Instantiatetheobjectintheconstructor implementageturlmethodontheobject privatestringusername privatestringpassword privatetestappenvtestappenv this publicuser this this publicstringgetusername returnusername publicstringgetpassword returnpassword publicvoidsetpassword this publicstringgeturl returnthis re-usetestappenvstaticallyfromwithinmyuserobjectsincetestappenvwasoriginallydesignedtobeaccessedstatically afieldorinstantiateanobject delegatetothestaticmethodontestappenvpublicstringgeturl returntestappenv createanewenvironmentusersincetheenvironmentuserisaspecialcaseofuser atall addanewmethodtotheenvironmentuserwhichstaticallyusesthetestappenvobject inheritance importcom returntestappenv andiwouldusetheobjectinan publicvoidcreateanenvironmentuser environmentuserenuser assertequals enuser thecorrectimplementation publicvoidreadonlyuserprivsanddefaults assertequals user inheritance importcom publicstringgetpermission return
��������assertEquals("You�are�18�years�old",�yourAge);����}
Uninitialised�variables,�and�parameters�are�a�common�source�of�exceptions.
��������Integer�age=null;
��������String�ageAsString;
��������try{
������������ageAsString�=�age.toString();
Don’t�fix�the�cause�of�the�exception
When�I�remove�the�age�=�18;�statement�from�within�the�catch�block�and�run�the�code. The�code�threw�a�NullPointerException�because�we�added�no�try�catch�block�inside the�catch�block.
��������String�yourAge�=
����������������"You�are�"�+�age.toString()�+�"�years�old";
��������assertEquals("You�are�18�years�old",�yourAge);����}
��������try{
������������ageAsString�=�age.toString();
��������}catch(NoSuchMethodException�e){
������������age�=�18;
������������ageAsString�=�age.toString();
��������}
NoSuchMethodException�is�a�checked�exception�and�needs�to�be�declared�as�thrown�by
methods.�The�toString�method�does�not�declare�that�it�will�throw�a
NoSuchMethodException�so�I�receive�a�syntax�error.
NullPointerException�and�ArithmeticException�are�unchecked�exceptions�and�don’t need�to�be�declared�as�thrown�by�methods.
��������}catch(NullPointerException�e){
������������System.out.println("getMessage�-�"�+��������������������e.getMessage());
������������System.out.println("getStacktrace�-�"�+��������������������e.getStackTrace());
������������System.out.println("printStackTrace");������������e.printStackTrace();
��������}
����}
I�receive�the�following�output,�I�have�cut�down�the�output�to�save�space�so�...�represents some�missing�output:
� at�java.lang.reflect.Method.invoke(Method.java:601)
�
at�com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
From�this�I�can�see�that�getMessage�on�a�NullPointerException�does�not�return�a message,�so�we�need�to�use�the�stack�trace�to�figure�out�what�went�wrong.�Other exceptions�do�return�messages,�and�when�you�start�creating�your�own�exceptions,�I recommend�that�you�add�a�message�to�make�it�easier�for�other�people�to�understand�the problem�in�the�code.
getMethodName
������������System.out.println("Stack�Trace�Length�-�"�+
��������������������������������e.getStackTrace().length);
������������System.out.println("Stack�Trace�[0]�classname�-�"�+
��������������������������������e.getStackTrace()[0].getClassName());������������System.out.println("Stack�Trace�[0]�filename�-�"�+
��������������������������������e.getStackTrace()[0].getFileName());������������System.out.println("Stack�Trace�[0]�linenumber�-�"�+
��������������������������������e.getStackTrace()[0].getLineNumber());������������System.out.println("Stack�Trace�[0]�methodname�-�"�+
��������������������������������e.getStackTrace()[0].getMethodName());
Chapter�Twelve�-�Introducing�Inheritance
Create�a�User�that�is�composed�of�TestAppEnv
To�create�a�TestAppEnv�object�within�my�User�object�I�could:
add�a�new�TestAppEnv�field,
instantiate�the�object�in�the�constructor,�and
implement�a�getUrl�method�on�the�object.
����public�String�getPassword()�{
��������return�password;
����}
����public�void�setPassword(String�password)�{
��������this.password�=�password;
����}
add�a�getUrl�method�to�User
delegate�to�the�static�method�on�TestAppEnv
����public�String�getUrl(){
��������return�TestAppEnv.getUrl();
����}
public�class�EnvironmentUser�extends�User�{
����public�String�getUrl(){
��������return�TestAppEnv.getUrl();
����}
Create�a�ReadOnlyUser
To�create�a�ReadOnlyUser�which�has�the�permission�ReadOnly,�with�the�same�default“username”�and�“password”�from�User.�I�first�wrote�an�@Test�method�which�checked�for the�correct�implementation.
public�class�ReadOnlyUser�extends�User�{
����@Override
����public�String�getPermission()�{
��������return�"ReadOnly";
����}