Thursday, July 3, 2025

CST438 - Week 2

In lab 1 on Junit you used a Mock.  In your own words describe what a Mock is and why it is useful in unit testing. 

In lab1, after creating our API endpoints we needed a way to verify that they were working properly as expected; meaning when we hit the end point we got the expected/proper data returned, or when we supplied bad data to the end point it returned with the correct failure response. You can manually test this during development of course, but as your API grows larger and has more complex interactions, it becomes harder and harder to quickly and reliably test changes that you make haven't broken anything. That's where unit testing can help out. Creating small, narrow, specific tests to ensure that changes a developer might make don't break things (in sometimes unexpected ways). In order to perform unit testing for a REST API end point, we need to have a functioning sever (and database) and have it populated with known data. What we're doing is using a Mock of a real world setup of a server and database where it contains valid (or sometimes invalid) data that we know about ahead of time. This way we can write small, targeted tests against that data ensuring our API endpoints are working as expected. Mocking up this fake but valid data is often much quicker than setting up a real server and database. It also lets you muck with data that you don't have to worry about if you accidentally mess up and delete or corrupt a database because of a new change you where implementing.

0 comments:

Post a Comment