TL DR How do I write unit tests for NetBeans platform code that uses static methods to look for dependencies?
In a NetBeans platform application, I find the code as follows:
MyService service = Lookup.getDefault().lookup(MyService.class);
service.doStuff(....);
To me, static access seems antipattern and hard to test. When I'm around Google, I find only comments about low grip and high grip, TV interfaces, etc.
Many people seem to think this is a good idea , but I wonder how I can write a reasonable unit test for code like this without resorting to mocking static methods or using the Lookup function in my unit test.
The first idea that comes to my mind is to refactor the search as a regular dependency:
public class MyClass {
private Lookup lookup = Lookup.getDefault();
public void myMethod() {
MyService service = lookup.lookup(MyService .class);
service.doStuff(....);
}
public void setLookup(Lookup lookup) {
this.lookup = lookup;
}
, .
, Lookup.getDefault()
. , Netbeans, , , .
, - . Netbeans?