By: Manjunath C B
with
1. Introduction
2. Necessary of testing
3. Types of testing
4. Thinking perspective on tests
5. Introduction to Junit with examples
6. Unit testing at ShieldSquare
Generic cost model
Different testing levels.
Unit testing is lowest-level testing that’s designed for programmers.
"Developers have to perform Unit Testing"
Who does testing?
When to perform testing ?
Thinking perspective on tests
Example:
[7, 8, 9] → 9 , [8, 9, 7] → 9 , [9, 7, 8] → 9 , [1] → 1
Check for duplicate numbers?
• [7, 9, 8, 9] → 9
Check for negative numbers:
• [-9, -8, -7] → -7
static int largest(int[] list);
[ 7, 8 , 9 ]
Function to test
Tests
JUnit is an open source framework that has been designed for the purpose of writing and running tests in the Java programming language.
JUnit is a regression-testing framework that developers can use to write unit tests as they develop systems.
Unit provides also a graphical user interface ( GUI ) which makes it possible to write and test source code quickly and easily.
Example demonstrating Junit test and folder structure
Annotations
@Test
@Before
@After
@BeforeClass
@AfterClass
@Mock
Example demonstrating Junit Annotations
Assertion methods
Example demonstrating Junit Assert, Exception Handling
What if the method doesn't return a value.
It must have some other side effect such as,
Testing void methods
You need to test whether that side effect has taken place
Testing dependent methods
Testing dependent methods
Mockito
Installing Mockito:
Mock Test Example :
Testing place order()
Mock Test Example :
Testing place order() by mocking order_DAO()
Mock Test Example
Test coverage
Test coverage measures the amount of testing performed by a set of test.
which parts of a program are actually executed when running the test
Its good to have atleast 80% test coverage
Eclipse test coverage plugin :
Test Coverage and Test Suit Example
Unit Testing @
Most of the ShieldSquare modules interact with Redis and Mongo. So there is a need for Mocking jedis and mongo.
Fongo : Library for mocking mongo(fake mongo)
https://github.com/fakemongo/fongo
Mock Jedis : Library for mocking redis
Refactoring at shieldsquare: Often changes in the module code.
Conclusion
Unit testing is low level testing. It has to be performed by developers.Unit testing is to be done in parallel to development. It reduces the cost in long run. Junit api is used for unit testing in java development. Its syntax and usage is easy to understand and implement. Mockito is used for mocking objects. Fongo and Mock jedis are used to mock mongo and redis. Which are well suited for shield square development. Testing void methods has to be done considering its side effects. Good to have more than 80% test coverage. EclEmma plugin is best known for calculating test coverages.