Mock Runtime.getRuntime()?

前端 未结 4 786
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 02:33

Can anyone make any suggestions about how best to use EasyMock to expect a call to Runtime.getRuntime().exec(xxx)?

I could move the call into a method i

4条回答
  •  旧时难觅i
    2020-12-30 02:50

    Your class shouldn't call Runtime.getRuntime(). it should expect a Runtime to be set as its dependency, and work with it. Then in your test you can easily provide a mock and set it as a dependency.

    As a sidenote, I'd suggest watching this lecture on OO Design for testability.

    Update: I didn't see the private constructor. You can try using java bytecode instrumentation in order to add another constructor or make the constructor public, but that might turn out to be impossible as well (if there are some restrictions on that class).

    So your option is to make a wrapper (as you suggested in the question), and follow the dependency-injection approach.

提交回复
热议问题