What would be a better practice with Spek? `object...
# spek
a
What would be a better practice with Spek?
object MyTestCase : Spek({})
vs
class MyTestCase : Spek({})
d
I use
object
as I believe that’s what was/is used at the docs. Also, since these test classes (leaves, not the intermediate classes, if any) are not parametrised and the runtime engine does not seem to require it, there’s no need to be a
class
. Other than that, I have no idea if there’s an opinionated view on
object
vs
class
on the official docs.
a
Yeah, I noticed that the docs uses
object
as well, however since
object
are singletons that gets lazily initialized when needed, I was wondering what happens when in a big project where we have thousands of test singletons in memory that might not be garbage collected, wouldn't we risk an OOM in CIs or our local development machine?
d
An object should be eligible for collection once its classloader is. As I have no idea how the runtime manages these classes, I guess that might be true. If you find out the answer, let me know 😛
a
I have no idea either 😞 But from the byte code decompilation that Kotlin generates.
Copy code
public final class MyTest extends Spek {
   public static final MyTest INSTANCE;

   private MyTest() {
      super((Function1)null.INSTANCE);
   }

   static {
      MyTest var0 = new MyTest();
      INSTANCE = var0;
   }
}
It seems that INSTANCE won't be garbage collected, but again I have no idea how does JUnit's test runtime manages test instances
r
No strong opinions on using
class
vs
object
, I used it in the samples to show people that they can use
object
😅. But TBH, the class and object thing is just an extra ceremony, ideally I would want to write their tests directly in kts files instead of wrapping them in a class. I might add some kind of support via the Kotlin Scripting API, but that API being jvm only makes me step back from it.
👍 1
Also, with regards to the gc concern, spek classes in the JVM are tiny, they don't have any state, so no fields - IIRC it will be 16 bytes (12 bytes for the header + 4 (padding - multiples of 8 ) - the static member don't count as they are stored in permgen (but the objects they are referring to,
INSTANCE
for example will be in the heap). With 2000 tests, the spek classes will only occupy ~32KB of memory.
a
@raniejade Thanks for the insights! from your explanation it doesn't seem to be a problem in big projects, 32KB for 2000 tests ain't much.