Skip to content

Create initial JUnit test with mocking - #2

Merged
JacquelineNi merged 1 commit into
mainfrom
jacob/junit
Jan 28, 2026
Merged

Create initial JUnit test with mocking#2
JacquelineNi merged 1 commit into
mainfrom
jacob/junit

Conversation

@jakejack13

@jakejack13 jakejack13 commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

What is unit testing?

From GeeksForGeeks:

"Unit testing is the process of testing the smallest parts of your code, like it is a method in which we verify the code's correctness by running one by one. It's a key part of software development that improves code quality by testing each unit in isolation. You write unit tests for these code units and run them automatically every time you make changes. If a test fails, it helps you quickly find and fix the issue. Unit testing promotes modular code, ensures better test coverage, and saves time by allowing developers to focus more on coding than manual testing. A unit test is a small piece of code that checks if a specific function or method in is an application works correctly. It will work as the function inputs and verifying the outputs. These tests check that the code work as expected based on the logic the developer intended."

From WPILib:

"Unit testing is a method of testing code by dividing the code into the smallest “units” possible and testing each unit. In robot code, this can mean testing the code for each subsystem individually."

For more info, please take a look at the WPILib documentation for unit testing: https://docs.wpilib.org/en/stable/docs/software/wpilib-tools/robot-simulation/unit-testing.html

How to run tests

There are three options for running tests:

  1. Tests will run when either compiling the project via a ./gradlew build command or by clicking "Build Robot Code" in the three-dot menu in the upper right corner of VSCode.
  2. Tests will run when either running tests via the ./gradlew test command or by clicking "Test Robot Code" in the three-dot menu in the upper right corner of VSCode.
  3. Click the green arrow to the left of any test class or test method in VSCode to run a particular set of tests.
image

Complications

Testing hardware via unit tests is very tricky. When JUnit is running a test, the hardware doesn't actually exist. WPILib fixes this problem by simulating your components. However, you currently do not support simulated components in your code. This means that if you try to send a signal to your "motor," it won't behave like an actual motor and won't do anything. Because of this, it will be very difficult to unit test your components since they won't respond to any input...

How do we fix this? A couple ways:

  1. Get sims working on this project. This is the main way I see FRC teams doing unit testing. If you take a look at the WPILib docs I linked above, this is what they are doing in their examples. Since tests run on sims, if sims work, tests will work. This will require major effort though.
  2. Use a testing technique called mocking. Mocking libraries, such as Mockito in Java, can create "fake" objects. You can define exactly what methods of these objects will return regardless of what the actual logic for those methods are. For example, I could create a mock of a ModuleKraken object and always have it return a certain state. This allows us to, instead of simulating the entire robot, only test the class we want to test and "fake" results of all of the objects it relies on (that you would find in its fields). This would require some refactor to your code (essentially rewrite your constructors to pass in the objects instead of creating the objects themselves).

Since creating sims will require a much larger refactor, I've instead installed Mockito, a mocking library, and got started with unit testing using mocking. I'm hoping this gives you all a good starting point which you can build off of!

Unit testing is absolutely something the team should work towards. We should strive for complete code coverage under tests! I'm happy toget the process started so that it's less load on the team but everyone should learn this at some point. As always, feel free to ask any questions you may have about any of this.

@JacquelineNi
JacquelineNi merged commit 2675076 into main Jan 28, 2026
1 check passed
@jakejack13
jakejack13 deleted the jacob/junit branch January 29, 2026 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants