junit - Mocking the bean and defining its behavior at context level -
i have few mocked beans in configuration dependencies of many other beans. want define behavior return null on of method invocations irrespective of input. using easymock
testclass { @autowired actualclass ac; public test() { ac.dosometing(); } } actualclass { @autowired mockedclass mockedobject; public dosomething() { mockedobejct.somemethod(); //i want returned null in scenario test cases } } }
basically:
mockedclass mock = createnicemock(mockedclass.class); replay(mock); ac.setmockedclass(mock); the modern version, using junit rule , annotations,
public class mytest { @rule public easymockrule rule = new easymockrule(this); @mock(type=mocktype.nice) mockedclass mockedobject; @testsubject actualclass ac = new actualclass(); @test public void test() { ac.dosomething(); } } class actualclass { private mockedclass mockedobject; public void dosomething() { mockedobject.somemethod(); } } class mockedclass { public void somemethod() {} }
Comments
Post a Comment