This document discusses unit testing and JUnit. It defines unit testing as testing the smallest testable parts of an application, like functions and classes. JUnit is introduced as a testing framework for Java that allows writing unit tests using annotations like @Test. Tests can make assertions about expected results using methods like assertEquals(). The document also covers integration testing, which tests integrated components, and mocking frameworks like Mockito, which allow mocking dependencies to isolate units for testing.
3. What is Software Testing
Part of the software development life cycle
4. What is Software Testing
• Points out the defects and errors that were made during the development
phases
• Ensures the Quality of product
• Requires lower maintenance cost
8. Unit Testing
A unit is the smallest testable part of an application like functions, classes,
procedures, interfaces.
Unit testing is a method by which individual units of source code are
tested to determine if they are fit for use.
Unit testing should be done before Integration testing.
Unit testing simplifies the debugging process.
10. Unit Test versus Integration Test
Unit test verifies the logic of small piece of code
Unit tests shouldn't have dependencies on outside systems
Integration test is done to demonstrate that different pieces of the system work
together.
Integration tests cover whole applications
12. JUnit
Unit & Integration testing framework @ Java
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
Can be used on maven
13. JUnit
Requires @Test annotation to be testable method
Return type of methods have to be void
@Test
public void sampleTestMethod() {
//
}
14. JUnit Assertions
Assert can check value is null empty true false equals to given etc.
assertEquals();
assertTrue();
assertFalse();
assertNotNull();
assertNull();
15. JUnit Assertions
Assert can check value is null empty true false equals to given etc.
@Test
public void testMethod() {
Integer multipliedNums = multiply(1,3);
Assert.assertEquals(3, multipliedNums );
}
public Integer multiply(int first, int next) {
return x*y;
}
Test passed
18. Mockito
Mockito is one of Mocking framework which used in Java
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
Can be used on maven
20. Mockito
@InjectMocks creates an instance of the class and injects the mocks that
are created with the @Mock
@InjectMocks
private CategoryController categoryController;