junit4 - JUnit & EasyMock -
is possible mock class instantiated in class unit testing?
class classtotest { public string methodtotest(object a, object b, object c) { //do lots of cool stuff here someotherclass someotherclass = new someotherclass(); someotherclass.domorecoolstuff(a, b, c, this); //do more cool stuff "this" updated someotherclass }
i want mock someotherclass used int classtotest. using easymock , can't seem compile. ideas? going wrong? thoughts?
you need way mock in there. part of testing. when find this, , "i can't test that" need refactor code can test. refactoring turn weird , strange designs beautiful art. (too deep?)
for example:
interface factory { public someotherclass create(); } class classtotest { factory factory; public void setfactory( factory factory ) { this.factory = factory } public string methodtotest(object a, object b, object c) { //do lots of cool stuff here someotherclass someotherclass = factory.create(); someotherclass.domorecoolstuff(a, b, c, this); //do more cool stuff "this" updated someotherclass }
a test this:
public void test() { someotherclass mock = easymock.create( someotherclass.class ); factory factory = easymock.create( factory.class ); classtotest test = new classtotest(); test.setfactory( factory ); expect(factory.create()).andreturn(mock); // can test mock interaction replay(mock,factory); test.methodtotest(a,b,c); verify(mock,factory); }
there many ways this. point make code testable. there have been many times find myself needing method class wrote find it's there because needed testing.
Comments
Post a Comment