Showing posts with label junit. Show all posts
Showing posts with label junit. Show all posts

July 24, 2018

Differences between stub and mock

Mocks and stubs are both dummy implementations of objects the code under test interacts with. The same object may be replaced with a stub in one test and a mock in another depending on the intent of the test.

Stubs can be thought of as inputs to the code under test. When called they behave a certain way – return a fixed value, throw an exception, calculate a return value based on parameters, pull from a sequence of values, etc. You'd use a stub to simulate the collaborator behavior that drives the behavior of the code under test. For example, if your code queries a database using a DAO you might supply a stub DAO and have tests that verify the behavior when your code finds one result, multiple results, zero results or when the query fails with an exception. Stubs provide the condition that prompts the different behavior in your code but they're not the way you verify that behavior.

Mocks can be thought of as outputs from the code under test. Under certain circumstances your code should or should not interact with a collaborator or should interact with it in a specific way – pass certain parameters, call multiple times, call methods in a certain order, etc. The simplest example is an asynchronous method that accepts a callback parameter. With a synchronous methodthat just returned a value, you'd simply check that return value. With an asynchronous version, you can pass a mock callback and verify the value passed to it.



Fake
Fakes are objects that have working implementations, but not same as production one. Usually they take some shortcut and have simplified version of production code.
An example of this shortcut can be an in-memory implementation of Data Access Object or Repository. This fake implementation will not engage database but will use a simple collection to store data. This allows us to do integration test of services without starting up a database and performing time-consuming requests.

Stub
Stub is an object that holds predefined data and uses it to answer calls during tests. It is used when we cannot or don’t want to involve objects that would answer with real data or have undesirable side effects.
An example can be an object that needs to grab some data from he database to respond to a method call. Instead of the real object, we introduced a stub and defined what data should be returned.

Mock
Mocks are objects that register calls they receive. In test assertion, we can verify on Mocks that all expected actions were performed.
We use mocks when we don’t want to invoke production code or when there is no easy way to verify, that intended code was executed. There is no return value and no easy way to check system state change.


References:
https://www.quora.com/Software-Testing-What-is-the-difference-between-Mocks-and-Stubs
https://blog.pragmatists.com/test-doubles-fakes-mocks-and-stubs-1a7491dfa3da
Read more ...

July 31, 2016

How to improve Junit test cases

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks collectively known as xUnit that originated with JUnit.

Reference:
http://www.vogella.com/tutorials/JUnit/article.html

http://www.vogella.com/tutorials/Mockito/article.html

Read more ...

November 20, 2015

JUnit(CodePro) testing(Automatic creation for all test classes) with using Eclipse Plugin

CodePro Update Site Installation into Eclipse 3.4.x
Before you install:
  • Make sure Eclipse is installed. You can download Eclipse from the Eclipse.org website.
  • Make sure Java is installed. You can download Java from Sun's website.
  • Warning: Due to a known p2 bug in Eclipse 3.4, if you previously installed using a zip file, you need to uninstall and start with a fresh copy of Eclipse.
Choose from the following options: Installing software by adding a new update site locationFollow these steps if the product has not been previously installed in this instance of Eclipse/IDE.
  1. In Eclipse, click Help > Software Updates...
  1. In the Software Updates and Add-ons dialog, select the Available Software tab and click the Add Site...button.
  1. Get the update site location from the product download page. On the download page, right-click on the Update Site URL button and copy the link location to your clipboard.
  1. Paste the site location (URL) into the Location: text box. Hint: You may also drag and drop a site URL from a web browser into the Location text box or the Available Software page.
  1. Click OK to close the Add Site dialog. You should now see a new update site added to the Available Software list.
  1. Check all features to be installed and click the Install... button.
  1. Click Next to confirm installation.
  1. Read and accept the license agreement. To continue installing, select "I accept the terms of the license agreement" and click Finish.
  1. When prompted to restart Eclipse, click Yes to restart.
Updating already installed softwareFollow these steps if the product has been previously installed in this instance of Eclipse using the update site and you just want to update to get the latest build.
  1. In Eclipse, click Help > Software Updates...
  1. In the Software Updates and Add-ons dialog, select the Installed Software tab.
  1. Click the Update... button to check for available updates.
  1. If new updates are available, the Available Updates dialog comes up. Leave everything selected and clickFinish to continue.
  1. When prompted to restart Eclipse, click Yes to restart.


URL below for your version of Eclipse.
Eclipse 3.7 (Indigo)
http://dl.google.com/eclipse/inst/codepro/latest/3.7Eclipse 3.6 (Helios)
http://dl.google.com/eclipse/inst/codepro/latest/3.6Eclipse 3.5 (Galileo)
http://dl.google.com/eclipse/inst/codepro/latest/3.5Eclipse 3.4 (Ganymede)
http://dl.google.com/eclipse/inst/codepro/latest/3.4


Read more ...

My Favorite Site's List

#update below script more than 500 posts