如何在JMockit中测试构造函数调用以获取以下代码段

时间:2013-10-18 07:55:56

标签: jmockit

public class HuronClassloader extends URLClassLoader {

  public HuronClassloader(Logger logger) {

    super(new URL[0]);
    this.logger = logger;
  }

  public void doLogic() throws ClasspathFormattingException {

      // logic go heer
  }

//如何使用JMockit测试doLogic方法?

1 个答案:

答案 0 :(得分:0)

你可以尝试如下;初始化测试类时,@ Injectable会自动将模拟Logger对象注入构造函数。

import mockit.Injectable;
import mockit.Tested;
...


@Tested
HuronClassloader loader;
@Injectable
Logger logger;


@Test
public void testSomeMethod() {
    //Optionally you can set expectation on your mock
    new Expectations() {{
        logger.someMethod(); result = ...;
    }};
    loader.doLogic();
}