public class CacheAdder{
private static final int HARD_CACHE;
static {
HARD_CACHE = 22;
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
public int divide(int number, int divider) {
return (int) HARD_CACHE+number/divider;
}
}
When I mock and run tests through this example class, mockito skips the static part which makes sense because I mock the value of HARD_CACHE but at the same time I want 100% coverage through the code. Should static fields be tested without mocks? Or is there a way to test static fields/methods/variables using mocks?
One way to do it is to use reflection for setting the value to the static field
You can look here: Mock private static final field using mockito or Jmockit
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.