tag:blogger.com,1999:blog-3237724005744642470.post2672811894213628403..comments2020-11-17T09:20:38.485+00:00Comments on Captain Debug's Blog: Using PowerMock to Mock ConstructorsRoger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]Blogger1125tag:blogger.com,1999:blog-3237724005744642470.post-70997191771172994112011-10-20T15:16:09.945+01:002011-10-20T15:16:09.945+01:00Option 4: use JMockit.
import org.junit.*;
import...Option 4: use JMockit.<br /><br />import org.junit.*;<br />import mockit.*;<br /><br />public class MockConstructorTest<br />{<br /> @Tested<br /> UsesNewToInstantiateClass instance;<br /><br /> @Test<br /> public void mockConstructor()<br /> {<br /> final String expected = "MY_OTHER_RESULT";<br /> <br /> new Expectations() {<br /> AnyOldClass anyClass;<br /><br /> {<br /> anyClass.someMethod();<br /> result = expected;<br /> }<br /> };<br /><br /> String result = instance.createThing();<br /><br /> assertEquals(expected, result);<br /> }<br />}rliesenfeldhttps://www.blogger.com/profile/17185104709482425736[email protected]