java - How to check TestNG Assert.assertEqual() conditional checking -


let me explain expecting have method following

public void  removebyobject() { try {             dsrcollection dsrcollection = new dsrcollection();             dsrcollection. setnuid(180);             dsrcollectionrepository.remove(dsrcollection); } catch (exception e) {             // todo: handle exception             e.printstacktrace();         } 

here want check particular method removebyobject() executed or not(also want involve assert.assertequal(dsrcollectionrepository.remove(dsrcollection),??)). checking condition should actual value.

or in more specific way object should appear in actual value place. requirement if application failed execute dsrcollectionrepository.remove(dsrcollection) should return asserterror message

to make removebyobject() more testable can extract code in try block own method. e.g.:

public class dsrcollectionrepositorymanager /* or whatever class called. */ {     /* ... */      public void removebyobject() {         try {             removebyobjectorthrow();         } catch (exception e) {             // todo: handle exception             e.printstacktrace();         }     }      protected boolean /* or whatever return type */ removebyobjectorthrow() {         dsrcollection dsrcollection = new dsrcollection();         dsrcollection.setnuid(180);         return dsrcollectionrepository.remove(dsrcollection);     }      /* ... */ } 

now can test removebyobjectorthrow() , see if throws exception or not.

if trying test removebyobject() called on behalf and/or don't want call removebyobjectorthrow() directly can subclass unit under test. e.g.:

public class dsrcollectionrepositorymanagertest {     @test     public void removeobjectsuccessful() {         boolean expected = true;         dsrcollectionrepositorymanager dsrcollectionrepositorymanager = new dsrcollectionrepositorymanager() {             @override             protected boolean removebyobjectorthrow() {                 try {                     boolean actual = super.removebyobjectorthrow();                     assert.assertequals(actual, expected);                     return actual;                 } catch (exception cause) {                     string message = "`removeobject` should not have thrown exception did";                     throw new assertionerror(message, cause);                 }             }         };         dsrcollectionrepositorymanager.removebyobject();     }      @test     public void removeobjectunsuccessful() {         dsrcollectionrepositorymanager dsrcollectionrepositorymanager = new dsrcollectionrepositorymanager() {             @override             protected boolean removebyobjectorthrow() {                 super.removebyobjectorthrow();                 string detailmessage = "`removebyobject` should have thrown exception did not";                 throw new assertionerror(detailmessage);             }         };         dsrcollectionrepositorymanager.removebyobject();     } } 

as don't know name of unit under test nor type of want use assertequals i've made things idea same: isolate code want test , test it.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -