java - Verifying private constructor is not invoked/called using JMockit -
i have following class.
public task { public static task getinstance(string taskname) { return new task(taskname); } private task(string taskname) { this.taskname = taskname; } }
i testing task.getinstance()
using jmockit. while test, need verify call private task()
made. have used verifications
block earlier verify method execution on test fixture object, here don't have that.
this can done, although shouldn't on written test:
@test public void badtestwhichverifiesprivateconstructoriscalled() { new expectations(task.class) {{ // partially mocks `task` // records expectation on private constructor: newinstance(task.class, "name"); }}; task task = task.getinstance("name"); assertnotnull(task); } @test public void goodtestwhichverifiesthenameofanewtask() { string taskname = "name"; task task = task.getinstance(taskname); assertnotnull(task); assertequals(taskname, task.getname()); } @test public void goodtestwhichverifiesanewtaskiscreatedeverytime() { task task1 = task.getinstance("name1"); task task2 = task.getinstance("name2"); assertnotnull(task1); assertnotnull(task2); assertnotsame(task1, task2); }
again, both partial mocking , mocking of private methods/constructors should avoided in general.
Comments
Post a Comment