Repository navigation
Add unit testing for commands, JDA mock system - #182
Conversation
|
CodeFactor only complains about the |
Tais993
left a comment
There was a problem hiding this comment.
This does make the current project a lot harder, creating commands just got a whole lot more complicated, so not sure how that's gonna go with less knowledged programmers
@Tais993 The major problem with testing is that we expose One advantage of this proposal is that writing the actual test is still simple, from which beginners will benefit. Maintaining the platform behind it is the obstacle, which experienced devs can take responsibility for - I do get your point though and you are right. |
less confusing, clearer, gets rid of name clash
* Use shared pools instead of creating new instances each time * weaken type of pools * spotless
85a2c55 to
c68c9f3
Compare
|
History cleanup and added @Heatmanofurioso back into the history |
|
Kudos, SonarCloud Quality Gate passed!
|








Overview
Closes #152. Adds a unit testing framework for commands revolving around JDA mocks with Mockito.
Original proposal by @Heatmanofurioso, optimized and adjusted by me. (still quite some
TODOs in there though, can be done in other PRs).Also adds high unit coverage for
PingCommandandDatabaseCommand.Example
Basic usage example:
Details
It revolves around the class
JdaTesterwhich is basically a mock ofJDAand friends. The class offers acreateSlashCommandEventmethod that, using a builder-pattern, enables users to create their own command events. Those events can then be given tocommand.onSlashCommand(...)to trigger the test logic. Testing is then done via Mockito, since the events are Mockito mocks.The builder provides strong subcommand and option validation using the
CommandDatasettings fromcommand.getData(). That way, users will get runtime exceptions when trying to create wrong events, such as:The builder has quite some background bloat because unfortunately JDA does not provide utility to create events. The core of its logic is creating a
DataObjectwithDataObject.fromJson(json). The JSON has to be in the format specified by the Discord API (see the actual payloads send by Discord), which we construct over a POJO bridge using Jackson.Mockito
Note that the approach of Mockito mocking JDA is quite fragile. It is highly likely that adding any new command that uses some other features, will run into a JDA routine that has not been covered by the mock yet - causing NPEs and other annoyance. That means that anyone who wants to test their commands usually also has to spend some time on expanding the JDA mock.
Also, major updates in the internal API of JDA will likely break the mock, we are using their internal code (i.e. their
...Implclasses) directly here.I do not see any nice way around that though - it is what it is.