Skip to main content
added 2 characters in body
Source Link
Farid Movsumov
  • 12.6k
  • 8
  • 72
  • 97

Code below will test exception message and exception code.

Important: It will fail if expected exception not thrown too.

try{
    $test->methodWhichWillThrowException();//if this method not throw exception it must be fail too.
    $this->fail("Expected exception 1162011 not thrown");
}catch(MySpecificException $e){ //Not catching a generic Exception or the fail function is also catched
    $this->assertEquals(1162011, $e->getCode());
    $this->assertEquals("Exception Message", $e->getMessage());
}

Code below will test exception message and exception code.

Important: It will fail if expected exception not thrown too.

try{
    $test->methodWhichWillThrowException();//if this method not throw exception it must be fail too.
    $this->fail("Expected exception 1162011 not thrown");
}catch(MySpecificException $e){ //Not catching a generic Exception or the fail function is also catched
    $this->assertEquals(1162011,$e->getCode());
    $this->assertEquals("Exception Message",$e->getMessage());
}

Code below will test exception message and exception code.

Important: It will fail if expected exception not thrown too.

try{
    $test->methodWhichWillThrowException();//if this method not throw exception it must be fail too.
    $this->fail("Expected exception 1162011 not thrown");
}catch(MySpecificException $e){ //Not catching a generic Exception or the fail function is also catched
    $this->assertEquals(1162011, $e->getCode());
    $this->assertEquals("Exception Message", $e->getMessage());
}
added 4 characters in body
Source Link
Farid Movsumov
  • 12.6k
  • 8
  • 72
  • 97

Code below will test exception message and exception code.

Important: It will fail if expected exception not thrown too.

try{
    $test->methodWhichWillThrowException();//if this method not throw exception it must be fail too.
    $this->fail("Expected exception 1162011 not thrown");
}catch(MySpecificException $e){ //Not catching a generic Exception or the fail function is also catched
    $this->assertEquals(1162011,$e->getCode());
    $this->assertEquals("Exception Message",$e->getMessage());
}

Code below will test exception message and exception code.

Important: It will fail if expected exception not thrown.

try{
    $test->methodWhichWillThrowException();//if this method not throw exception it must be fail too.
    $this->fail("Expected exception 1162011 not thrown");
}catch(MySpecificException $e){ //Not catching a generic Exception or the fail function is also catched
    $this->assertEquals(1162011,$e->getCode());
    $this->assertEquals("Exception Message",$e->getMessage());
}

Code below will test exception message and exception code.

Important: It will fail if expected exception not thrown too.

try{
    $test->methodWhichWillThrowException();//if this method not throw exception it must be fail too.
    $this->fail("Expected exception 1162011 not thrown");
}catch(MySpecificException $e){ //Not catching a generic Exception or the fail function is also catched
    $this->assertEquals(1162011,$e->getCode());
    $this->assertEquals("Exception Message",$e->getMessage());
}
Improved code in order not to catch fail function
Source Link

Code below will test exception message and exception code.

Important: It will fail if expected exception not thrown.

try{
    $test->methodWhichWillThrowException();//if this method not throw exception it must be fail too.
    $this->fail("Expected exception 1162011 not thrown");
}catch(ExceptionMySpecificException $e){ //Not catching a generic Exception or the fail function is also catched
    $this->assertEquals(1162011,$e->getCode());
    $this->assertEquals("Exception Message",$e->getMessage());
}

Code below will test exception message and exception code.

Important: It will fail if expected exception not thrown.

try{
    $test->methodWhichWillThrowException();//if this method not throw exception it must be fail too.
    $this->fail("Expected exception 1162011 not thrown");
}catch(Exception $e){
    $this->assertEquals(1162011,$e->getCode());
    $this->assertEquals("Exception Message",$e->getMessage());
}

Code below will test exception message and exception code.

Important: It will fail if expected exception not thrown.

try{
    $test->methodWhichWillThrowException();//if this method not throw exception it must be fail too.
    $this->fail("Expected exception 1162011 not thrown");
}catch(MySpecificException $e){ //Not catching a generic Exception or the fail function is also catched
    $this->assertEquals(1162011,$e->getCode());
    $this->assertEquals("Exception Message",$e->getMessage());
}
added 26 characters in body
Source Link
Farid Movsumov
  • 12.6k
  • 8
  • 72
  • 97
Loading
added 4 characters in body
Source Link
Farid Movsumov
  • 12.6k
  • 8
  • 72
  • 97
Loading
Source Link
Farid Movsumov
  • 12.6k
  • 8
  • 72
  • 97
Loading