3
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 方法?

4

1 回答 1

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();
}
于 2016-05-14T03:24:56.797 回答