Author Archives: Michael St. Germain

J Unit Stubs

*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.

Simply put, you can view a stub as a type of mock with more control and is a little easier then making a mock of an object. This picture helped me realize the difference.

As can be seen, by using a stub you can pretty much do everything a mock can, without having an entire mocked object. generally a stub will have the minimal amount of parts needed in order to make tests pass. the tests themselves are focused on the class itself, rather then the manipulation of an object. A stub will usually return a predetermined value. Hence why it can be viewed as a type of mock, but is still its own thing.

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.

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.