-
-
Notifications
You must be signed in to change notification settings - Fork 93
Add unit testing for commands, JDA mock system #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
CodeFactor only complains about the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
TODO
s in there though, can be done in other PRs).Also adds high unit coverage for
PingCommand
andDatabaseCommand
.Example
Basic usage example:
Details
It revolves around the class
JdaTester
which is basically a mock ofJDA
and friends. The class offers acreateSlashCommandEvent
method 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
CommandData
settings 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
DataObject
withDataObject.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
...Impl
classes) directly here.I do not see any nice way around that though - it is what it is.