Category Archives: Unit Testing

Unit Testing Interview Questions – Set 1

interview questions, practice tests on unit testing

This page lists down the practice tests / interview questions and answers for unit testing (general concepts) which applies for all programming languages. Those wanting to test their unit testing knowledge would find this test useful enough. The goal for this practice test is to help you do quick check of your knowledge in unit testing and prepare appropriately. More importantly, when you are preparing for interviews, these practice tests are intended to be handy enough. Those going for freshers / intern interviews in the area of programming language or for developer positions would also find these practice tests / interview questions to be very helpful. Following topics have been covered as part of …

Continue reading

Posted in Interview questions, Unit Testing. Tagged with , .

Mocha Unit Tests with Hello World

This blog helps you get started with Mocha and also present steps and sample tests which can be used to get started. Setup Mocha using NPM For installing Mocha globally such that it can be from anywhere, execute following command: For installing with npm, you may be required to install Nodejs. npm is distributed with NodeJS which implies that downloading and installing NodeJS automatically installs npm on your computer. One can get appropriate version of NodeJS from following page, Download NodeJS. Details on npm can be found on this page, NPM. Execute mocha command to make sure it is installed appropriately. Following is the screenshot of output as a result …

Continue reading

Posted in Javascript, Maintainability, Software Quality, Unit Testing. Tagged with .

Javascript – Jasmine Unit Tests for Promise Object

This article represents tips and code samples in relation with how to write unit tests for Promise object when using Jasmine framework. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are the key points described later in this article: Sample Promise Object Unit Tests related with Promise Object Sample Promise Object Pay attention to some of the following in the code below: Auth module returns a Promise object that returns a User object when resolved or returns an error object in case of error. A JSON object is passed to resolve or reject method. Save the file …

Continue reading

Posted in Javascript, UI, Unit Testing. Tagged with , , .

Quick Reference to Jasmine – Javascript Unit Testing Framework

jasmine unit testing framework

The article represents the fundamentals concepts and code samples around Jasmine, a popular Javascript unit testing framework.TO get started, download the framework from https://github.com/pivotal/jasmine/releases. For detailed examples, look at http://jasmine.github.io/edge/introduction.html. It All Starts with “describe” and “it” Function Blocks In Jasmine terminologies, a set of tests is collectively called as a “suite”. The test suite is defined using “describe” function block. Each test is called as a “spec” and defined using “it” function block. A test suite can have multiple test specs and also, test suites. This implies that a “describe” function block can have multiple “it” function blocks and also, “describe” function blocks contained within. One of the key …

Continue reading

Posted in Testing, Tools, UI, Unit Testing. Tagged with , .

Unit Testing with JUnit & Mockito – Code Examples

unit testing

The article presents quick code example for doing unit testing with JUnit & Mockito mocking framework. It also represents unit testing naming convention where features text is used for naming the test methods thereby representing test class as alternate documentation for the class under test. Following is done while defining Mock member variables Use @Mock annotation ahead of member variable definitions Following are steps done in the setup method: Initialize the mock using MockitoAnnotations.initMocks(this); Object of class under test is initialized with the mock objects Following steps are used in each of the unit test methods: Create/Initialize required objects for working with mocks and methods under test Set the mock …

Continue reading

Posted in Unit Testing. Tagged with , .

7 Popular Unit Test Naming Conventions

unit-testing

The article presents a compiled list of unit tests naming strategy that one could follow for naming their unit tests. The article is intended to be a quick reference instead of going through multiple great pages such as following. That said, to know greater details, please feel free access one of these pages listed below and know for yourself. Naming Standards for Unit Tests Naming Unit Tests Responsibly What are some popular naming conventions for unit tests? Unit Tests Naming Best Practices GivenWhenThen Technique Following are 7 popular unit tests naming conventions that are found to be used by majority of developers and compiled from above pages: MethodName_StateUnderTest_ExpectedBehavior: There are …

Continue reading

Posted in Unit Testing. Tagged with .

AngularJS – 7 Steps for Unit Testing AngularJS Scripts with Jasmine

This article presents instructions on what would it take to create a setup and run unit testing for your AngularJS scripts using Jasmine framework. I shall be presenting various samples on my AngularJS website, http://hello-angularjs.appspot.com. Sorry for the typos in this article and please feel free to suggest/add any points if I missed them out.   Following are key steps:  Learn Jasmine Create Unit Tests using Jasmine Download and install NodeJS Download & Install Karma Configure Karma Place Scripts Appropriately Execute Tests   Learn Jasmine Jasmine, one of the very popular Javascript unit testing framework, can be used for unit testing one’s AngularJS scripts. As a matter of fact, Angular …

Continue reading

Posted in Javascript, UI, Unit Testing. Tagged with , , .

Java Unit Testing Interview Questions

ATG interview questions

The article presents some of the frequently asked interview questions in relation with unit testing with Java code. Please suggest other questions tthat you came across and I shall include in the list below. What is unit testing? Which unit testing framework did you use? What are some of the common Java unit testing frameworks? Ans: Read the definition of Unit testing on Wikipedia page for unit testing. Simply speaking, unit testing is about testing a block of code in isolation. There are two popular unit testing framework in Java named as Junit, TestNG.   In SDLC, When is the right time to start writing unit tests? Ans: Test-along if …

Continue reading

Posted in Interview questions, Unit Testing. Tagged with , , .

Test-Driven Development (TDD) Shines with Mocking

The article presents a perspective and some code samples on how one could some cool stuff with Test-driven development (TDD) and Mocking. The code samples are done in Java. Lets briefly understand what is TDD and mocking? What is TDD? Test-driven development, simply speaking, is a software development process in which developers write tests first and, then writing enough code to pass those tests. Once all of the tests pass, they do code refactoring to enhance code quality. Following are key advantages of adopting TDD as your development process: Enforces the developers to thoroughly think through various different test cases which could be used to test the functional/business requirements. It, …

Continue reading

Posted in Unit Testing. Tagged with , .

Unit Tests – Mocking vs Stubbing Vs Spying

The article lists down some good reads (from different webpages on internet) on unit testing concepts such as mocking, stubbing and spying and represents summarized information on these concepts. Mocks Aren’t Stubs What’s the difference between a mock & stub? Mocking vs. Spying in mocking frameworks Mocking and stubbing in Ruby on Rails What’s the difference between faking, mocking, and stubbing? After going through all the above pages, I could arrive at following definitions: Mocking Mocking is a unit testing phenomenon which is used to replace real objects with test objects that mocks the behavior. These test objects are called as Mock objects. The point to note is that mock …

Continue reading

Posted in Unit Testing. Tagged with .

Top 5 Unit Testing Best Practices

The article lists down top 5 unit testing best practices that one could adopt to be able to write great unit tests with greater code coverage. Test-Along, if not Test-Driven Many a times, rookies have been found to start writing unit tests once they have written most of the code, e.g., towards the end of the coding. However, to make the most of unit tests, one should write them as they write the code. This is somewhat different and less difficult than writing tests first and code later as in test-driven development (TDD). Following are some of the advantages of doing “test-along” development: One could achieve greater code coverage by …

Continue reading

Posted in Unit Testing. Tagged with .

Junit & Mock Framework Mockito Code Samples – Part 1

The article represents code samples for Junit tests and Mockito, a mocking framework. In addition, it describes different aspects of unit testing and mocking. Software Requirement The goal is to create a simple piece of software which caters to the requirement of school admission where in applicants submit their admission application.   Class Design & JUnit Tests To meet above requirement, following different component is designed: Core Components (Key Ones) AdmissionApplication.java: Consists of method recordNewApplication which validates the submitted application, and later, stores in the database after successful validation. ApplicationValidation.java: Consists of methods for validating business rules associated with admission such as mandatory inputs (first name, last name, class, gender, …

Continue reading

Posted in Unit Testing. Tagged with , .

JUnit Tests Code Samples for Testing Exception Scenarios

The article presents an example of unit tests which tests both happy path and exception scenario. Business Requirement User trying to open an account must be validated against the business rules related with users’ registration. Following are different business rules: User must provide a valid email address User must provide his first name User’s age must be 18 and above   Class Design To demonstrate the unit tests with happy and exception scenarios, following classes have been shown below: SignupValidation: Consists of code validating business rules SignupException: Custom exception representing Signup business rules failure EmailValidator (Get the code from this page). SignupValidationTests: Unit tests for testing signup validation code Class …

Continue reading

Posted in Unit Testing. Tagged with .

Unit Tests – An Alternate Functional Requirements Documentation

The article presents a perspective on how unit tests could be written in a way that they could be used as an alternate form of documentation for understanding functional requirements. Many a times, it has been found that developers, primarily rookies, did not have a clarity on how to name unit tests. Thus, many of them have been found to name the unit tests such as testXXX where XXX is name of the method which they wanted to tests. There has been many recommendations on how to name unit tests, some of which are following: Given…When…Then… Check the page by Martin Fowler (http://martinfowler.com/bliki/GivenWhenThen.html) WhenXXX_ExpectYYY Names representing different use case scenarios …

Continue reading

Posted in Unit Testing. Tagged with .

How to Increase Unit Test Coverage of Legacy Code?

test coverage

The article represents a time-tested approach on how to increase unit test coverage with legacy code. I shall try and answer following relevant questions in order to present the approach on increasing the test coverage of legacy applications (code). What are some of the key challenges? Leadership Buy-in: As soon as you start planning for writing unit tests on legacy applications, you come to realize that it won’t be very effective and smooth without the consideration of code refactoring. The reason being the fact that legacy code in many cases are found to have very high code complexity in terms of cyclomatic complexity and, familiar code smells such long classes, …

Continue reading

Posted in Maintainability, Software Quality, Testing, Unit Testing. Tagged with , .

A Quick Way to Identify Code with Low Testability

Following is a quick way to identify code with low testability and hence low maintainability: Take any specific public method of the class Count for number of decision points such as following: – If – For – While – Switch – Inline ifs If the number of decision points in a method is more than 15 or so, it could be taken as indication that it may get difficult to write unit tests covering all possible flows. Thus, the code will have lower testability. The number of decision points in a method is also used to define/measure cyclomatic complexity of the method, or more specifically, McCabe Cyclomatic Complexity. Thus, mathematically …

Continue reading

Posted in Maintainability, Software Quality, Unit Testing. Tagged with .