java - How to verify that error was logged with unit tests -
let's have following class this:
public class myclass { public static final logger log = logger.getlogger(myclass.class); public void mymethod(string condition) { if (condition.equals("terrible")) { log.error("this terrible!"); return; } //rest of logic in method } }
my unit test myclass
looks this:
@test public void testterriblecase() throws moduleexception { mymethod("terrible"); //log should contain "this terrible!" or assert error logged }
is there way determine log contains specific string "this terrible"? or better, there way determine if logged error @ without looking specific string value?
create custom filter message , record if ever seen.
@test public void testterriblecase() throws moduleexception { class terriblefilter implements filter { boolean seen; @override public boolean isloggable(logrecord record) { if ("this terrible!".equals(record.getmessage())) { seen = true; } return true; } } logger log = logger.getlogger(myclass.class.getname()); terriblefilter tf = new terriblefilter(); log.setfilter(tf); try { mymethod("terrible"); asserttrue(tf.seen); } { log.setfilter(null); } }
Comments
Post a Comment