-
-
Notifications
You must be signed in to change notification settings - Fork 102
Add unit tests for tag-system #355
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
cde96b7
to
db4627d
Compare
application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java
Show resolved
Hide resolved
application/src/test/java/org/togetherjava/tjbot/commands/tags/TagSystemTest.java
Show resolved
Hide resolved
application/src/test/java/org/togetherjava/tjbot/commands/basic/PingCommandTest.java
Show resolved
Hide resolved
application/src/test/java/org/togetherjava/tjbot/commands/tags/TagsCommandTest.java
Show resolved
Hide resolved
I usually follow a convention on the Display Names of tests, where I describe my GIVEN, WHEN, THEN in the test name itself. Would you think it's a good idea to adapt to that convention @Zabuzard ? Probably starting on this PR "hence me asking it here" |
application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java
Show resolved
Hide resolved
application/src/test/java/org/togetherjava/tjbot/commands/tags/TagSystemTest.java
Show resolved
Hide resolved
application/src/test/java/org/togetherjava/tjbot/jda/ButtonClickEventBuilder.java
Show resolved
Hide resolved
application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java
Outdated
Show resolved
Hide resolved
If possible, could you make it so this PR becomes 2 commits. 1 updating JDATester, and ButtonClickEventBuilder etc This makes imho more sense |
3d12f2b
to
fc9083a
Compare
I beautified the commit history (no code changes). Cheers |
This is a quite simple change but it touches a lot of files. All of them now require the config as parameter instead of using global singleton access. This makes it possible to mock the Config (needed for the unit tests).
enabling mockito inline extension to make it possible to mock final classes
* Added ButtonClickEvent generation and mocking * Added USER option to SlashCommandEventBuilder * Added `userWhoTriggered(user)` to slash command event builder * Added mocked rest actions (success & failure), and discord exceptions
* and improved ping tests
14128cd
to
3bb3a75
Compare
Kudos, SonarCloud Quality Gate passed! |
Overview
Implements and closes #213 . Adds unit tests to the tag system, i.e.
TagSystem
,TagCommand
,TagsCommand
andTagManageCommand
.The tests themselves should be rather self-explanatory.
Majority of changes in this PR are not logic changes but come from the fact that we made
Config
mockable, and hence had to get rid of the singleton - which bleeds into almost all commands.Features
While at it, we had to implement multiple more general features from which other unit tests will also benefit in the future:
Database
We added a method
Database.createMemoryDatabase(...)
to conveniently use an in-memory database for testing. An usage can be seen inTagsCommandTest
, where it is called@Before
each test.Mockito
The whole mockito test suite, in particular revolving around
JdaTester
has been enhanced and generally supports more calls now.And we also added some bigger features to it:
Button events
In order to test button events, we added a
JdaTester#createButtonClickEvent()
method that returns a builder that can create mocks of the event, similar to howJdaTester#createSlashCommandEvent(...)
works.The API of this builder is very similar to JDAs
Method
interface and similar:or
Command spies
There is now a method
JdaTester#spySlashCommand(command)
that makes it possible to work with a command that is spied upon and additionally is setup properly. For example, it works with a mocked component ID generator.Member spies
We also added a method
JdaTester#createMemberSpy(userId)
that is able to create valid member instances which are spied upon. Useful for testing things that require members. For example buttons that have been clicked by a user different to the message author.USER option for slash command events
We enhanced the
SlashCommandEventBuilder
and added overloadsoption(String, User)
andoption(String, Member)
.So now it also supports options with types
USER
, not justString
. For example:To make this work, the implementation had to be changed a bit. Therefore, other types can now also easily be added in the future.
Setting the user who triggered a slash command event
The
SlashCommandEventBuilder
now has a methoduserWhoTriggered(user)
that can be used to set the member who triggered the event. Useful for mocking, for example to adjust permissions of a user who wants to use the ban command.RestAction
mockingJdaTester
now has two methods to create success and failure mocks ofRestAction
s. This can be very useful when testing stuff that involves calls such asTextChannel#retrieveMessageById
or similar. Example:or also
Checklist
Unit tests
Mocking
USER
as option toSlashCommandEventBuilder
userWhoTriggered(user)
forSlashCommandEventBuilder
Config.getInstance()
mockingRestAction<T>
mocking (success & failure)