Bastian/bstats-metrics

Option to disable initialization for unit tests?

Silthus opened this issue · 2 comments

I use Mockbukkit to test all of my Plugins and have a problem using bstats. Since it initializes everything in the constructor I am unable to mock it.

Is there a way to fix/disable the sending of data for unit tests?

Hey there!

I saw this Issue and decided to provide a workaround.
I use MockBukkit for a project of mine as well and the way I tackled this issue was by having a boolean flag in the main class which determines whether the Plugin is currently in a "Unit Test" environment or not.
The way I achieved this was by utilising the different constructors needed to use MockBukkit, as seen here:
https://github.com/seeseemelk/MockBukkit#using-mockbukkit

In a normal run, Spigot or similar would invoke the 0-args constructor, the ClassLoader and PluginDescriptionFile will be inferred by the Server software. But MockBukkit needs to pass these arguments itself, so it uses the full constructor to create the object.
Using this discrepancy provides an easy way to determine whether or not the plugin is being run in a testing environment. You could use this to only start bStats if that flag was not set.

This isn't an ideal solution but it should definitely work!
Alternatively you could also set an environment variable but the method with the two constructors is probably less prone to accidents or forgetfulness.

Thanks for the workaround, that does perfectly well :)