Author Archives: Michael St. Germain

J Unit Mocks

*Through my teams experience we were getting a little confused on the differences of mocks and stubs, so i would like to explain each one separately.

In normal words, a mock implements some behaviors that may not already exist in the code you’re trying to test. I’ve found a picture to help illustrate.

It is creating a fake object that will use code you are testing. The tests make sure that object is interacting with the “class under test”, correctly. The mock will not have preprogrammed return values and are used to find side effects and whether the correct methods are being called.

To further explain, one may find themselves working on a portion of code where they don’t have an object that is meant to be used prepared already. These objects could also be a giant database, requiring you to access a large amount of data over and over while testing a much smaller class. Using a mock in this case would speed up the process. Focusing only on the class you want to test for behaviors.

Sources:
https://stackoverflow.com/questions/31890991/how-to-use-stubs-in-junit-and-java
(photo source)

https://semaphoreci.com/community/tutorials/stubbing-and-mocking-with-mockito-2-and-junit
(helpful for understanding)

From the blog Mikes CS Journey by Michael St. Germain and used with permission of the author. All other rights reserved by the author.

CS-443

This will be my log for the course CS-443.

From the blog Mikes CS Journey by Michael St. Germain and used with permission of the author. All other rights reserved by the author.