How to test a constructor call in JMockit for the following piece of code

StackOverflow https://stackoverflow.com/questions/19444435

  •  01-07-2022
  •  | 
  •  

Domanda

public class HuronClassloader extends URLClassLoader {

  public HuronClassloader(Logger logger) {

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

  public void doLogic() throws ClasspathFormattingException {

      // logic go heer
  }

// How to test the doLogic method using JMockit?

È stato utile?

Soluzione

You can try as follows; @Injectable will automatically inject the mock Logger object to the constructor when initializing your tested class.

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();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top