Is there a way to tell a compiler to open all clas...
# getting-started
b
Is there a way to tell a compiler to open all classes and methods for test compilations?
s
e
Unfortunately, no. There is a compiler plugin that opens all classes that have a specific annotation.
BUT no plugin that opens all classes - and JetBrains said they are also opposed to that.
We have run into the same problem, because we use Spock for testing, which cannot mock final classes. In the end it didn’t matter that much for us, because we use Spring Boot and most classes are @Components and the Spring plugin “opens” those classes automatically.
b
The problem i'm dealing with is the MPP library project that has 90% of the code in common module and is targeting JS and WASM. Since there's no mocking library for that combination of platforms, I'm struggling to unit test it. So the I thought if i could open up everything for test runs, I could extend the classes I'm "mocking" and override the mock methods. I'm not too comfortable making everything open explicitly in production code just to achieve this.
m
This would be a good case for interfaces and constructor injection, or a full blown DI. Then you can construct your own version of the interface to pass in when you're constructing things. Don't have to use a framework to accomplish it, and makes it easily testable.
b
That was the other thing I've considered, however the problem with is is that massive boilerplate 😕
But i guess there's no way around it.
m
Always trade-offs. I would imagine you're only mocking integration points, and those have to be defined/constructed somehow, right? So it shouldn't be much more than introducing an interface into your production code, and moving 'injection' to the constructor, if it isn't already there. Then testing becomes easy.
💯 1