junit4

Mocking static method that is called multiple times

一曲冷凌霜 提交于 2021-01-27 14:52:58
问题 I have a static method, that is used in multiple places, mostly in static initialization block. It takes a Class object as parameter, and returns the class's instance. I want to mock this static method only when particular Class object is used as parameter. But when the method is called from other places, with different Class objects, it returns null. How can we have the static method execute actual implementation in case of parameters other than the mocked one? class ABC{ void someMethod(){

Mocking static method that is called multiple times

落花浮王杯 提交于 2021-01-27 14:35:38
问题 I have a static method, that is used in multiple places, mostly in static initialization block. It takes a Class object as parameter, and returns the class's instance. I want to mock this static method only when particular Class object is used as parameter. But when the method is called from other places, with different Class objects, it returns null. How can we have the static method execute actual implementation in case of parameters other than the mocked one? class ABC{ void someMethod(){

Static initializer doesn't run during JUnit tests

心不动则不痛 提交于 2021-01-27 04:32:26
问题 I have an interesting JUnit problem here (JUnit 4.12). I have a base class that only has static methods. They have to be static, because of the way they're used. I inherit other classes from the base class. So, if the base class is Base , we have ChildA and ChildB . Most of the methods are contained in the base class, but it has to know which child it actually is (just calling the methods as the base class is invalid). This is done via a static data member in the base class: public class Base