KEMBAR78
Introduction to Testing Frameworks | PPT
JUnit ,
Mockito, PowerMockito
Approach of Unit testing
with the help of
Unit Testing
 Testing tools
 Android Studio or Eclipse
 How to create JUnit Test Case in Eclipse or Android Studio
 Gradle Setup
 Proper Dependencies
Unit testing with Junit, Mockito, PowerMockito 2
What should be tested ?
 Test for boundary line conditions
 Test for both success and failure
 Test for general functionality
 Etc..
Unit testing with Junit, Mockito, PowerMockito 3
Test case design technique
 Test case design techniques can be broadly split into
two main categories
 Black box (functional)
 White box (structural)
Unit testing with Junit, Mockito, PowerMockito 4
Black Box tests
 Targeted at the apparent simplicity of the software
 Makes assumptions about implementation
 Good for testing component interactions
 Tests the interfaces and behavior
Unit testing with Junit, Mockito, PowerMockito 5
Input Output
White Box tests
 Targeted at the underlying complexity of the software
 Intimate knowledge of implementation
 Good for testing individual functions
 Tests the implementation and design
Unit testing with Junit, Mockito, PowerMockito 6
Input Output
Which framework to use?
 Black Box Testing
 Junit
 White Box Testing
 Mockito
 PowerMockito
 Parameterized Testing
 JunitParams
Unit testing with Junit, Mockito, PowerMockito 7
Junit 4
 Gradle dependency for Junit framework
 testImplementation 'junit:junit:4.12’
 Import statement for using the following annotations.
 import org.junit.*;
 @Test
 Identifies a method as a test method.
 @Before and @After
 Executed before and after each test. It is used to prepare the test
environment or teardown
 @Ignore
 Marks that the test should be disabled and the test case has not yet
been adapted
Unit testing with Junit, Mockito, PowerMockito 8
TestCase lifecycle
1. @Before
setup(){ /* initialization*/ }
2. @Test
testNameXYZ(){}
3. @After
tearDown(){ /* de-initialization*/ }
4. Repeats 1 through 3 for each testNameXYZ method…
Unit testing with Junit, Mockito, PowerMockito 9
Junit Test Example
Class StudentTest{
public Student mStudent;
@Before
public void setup() {
mStudent = new Student(); }
@After
public void tearDown() {
mStudent = null;
}
@Test
public getStateTest(){
//Setup
Boolean expected = false;
mStudent.setState(expected);
//Execution
Boolean result = mStudent.getState();
//verification
Assert.assertEquals(expected, result);
}
} Unit testing with Junit, Mockito, PowerMockito 10
/*POJO*/
Class Student {
private Boolean mState;
public Boolean getState() {
return mState;
}
public void setState(Boolean state) {
mState = state;
}
}
Junit Verification methods
 assertTrue(boolean condition)
 checks that the boolean condition is true.
 assertFalse(boolean condition)
 checks that the boolean condition is false.
 assertEquals(expected, actual)
 tests that two values are the same
 assertNull(obj)
 checks that the object is null
 assertNotNull(obj)
 checks that the object is not null
Unit testing with Junit, Mockito, PowerMockito 11
JunitParams Framework
 Parameterized test is to execute the same test over and over again
using different values
 Gradle dependency for JUnitParams framework
 testImplementation “pl.pragmatists:JUnitParams:1.1.1”
 Example
public Object[] getScreenWidthParameters() {
return new Object[] {
new Object[] { 0, 0 },
new Object[] { 480, 480 },
new Object[] { -1, -1 },
new Object[] { null, null } };
}
@Test
@Parameters(method = "getScreenWidthParameters")
public void getScreenWidth(int mScreenWidth, int expected) throws Exception {
//setup
mStudent.setState(expected);
//Execution
int result = mStudent.getScreenWidth();
//Verification
Assert.assertEquals(expected, result);
}
Unit testing with Junit, Mockito, PowerMockito 12
Problems with Junit Framework
Unit testing with Junit, Mockito, PowerMockito 13
Class Sample {
public BtManager mBtManager;
private boolean doOperation() {
//do something
return (condition) ? true : false;
}
public void fetchData() {
//result handled here itself
boolean result = doOperation();
}
// External dependency
public void enableBt(){
//Here mBtManager is thirtparty object
mBtManager.enableBt();
}
Mockito & PowerMockito
comes to the rescue
 Mockito
 Mockito is a mocking framework library enables mocks creation,
verification and stubbing.
 We just Mock the dependency class and we can test the Main Class
 But it only support public methods
 PowerMockito
 PowerMock is a framework that extends other mock libraries such
as EasyMock with more powerful capabilities
 Helps to enable mocking of static methods, constructors, final
classes and final methods, private methods, suppressing static
initializers and allows manipulating private members
 Currently PowerMock do not support Junit 5.
Unit testing with Junit, Mockito, PowerMockito 14
Mockito Framework
 Gradle dependency for Mockito framework
 testImplementation 'org.mockito:mockito-core:2.19.0’
 Annotated the class name with
 @RunWith(MockitoJUnitRunner.class)
class MyTestName() { }
 We mock any class using @Mock annotation
 @Mock
BtManager mBtManager;
 Mocking using Mockito Public Api
 BtManager mBtManager = Mockito.mock(BtManager.class);
Unit testing with Junit, Mockito, PowerMockito 15
Mockito cont...
 Stubbing
 It is done to change the behavior of dependencies
 Mockito.when( mockObject).methodName().thenReturn( boolean);
 Mockito.doNothing().when( mockObject).methodName();
 Mockito.when( mockObject.methodName(params))
.thenThrow(new Exception(“reason”));
 Verification to check a certain method is called or not
 Mockito.verify( mockObject).methodName( parameters….);
 Verification of private methods are not possible with Mockito
Unit testing with Junit, Mockito, PowerMockito 16
PowerMockito Framework
 Gradle dependency for PowerMockito framework
 testImplementation "org.powermock:powermock-module-junit4:1.6.5“
 testImplementation "org.powermock:powermock-module-junit4-rule:1.6.5"
 testImplementation "org.powermock:powermock-classloading-xstream:1.6.5"
 PowerMockRunner Annotation
 @RunWith(PowerMockRunner.class)
class MyTestName() { }
 PrepareForTest Annotation
 Annotation represents an array of class names, we want to mock.
 @PrepareForTest({Sample.class, Student.class})
class MyTestName() { }
 Mocking using Mockito Public Api
 BtManager mBtManager = PowerMockito.mock(BtManager.class);
Unit testing with Junit, Mockito, PowerMockito 17
PowerMockito cont...
 Stubbing
 PowerMockito.whenNew(Sample.class).withNoArguments()
.thenReturn( sampleMock);
 PowerMockito.when( mockObject.finalMethod()).thenReturn(boolean);
 PowerMockito.when(Sample.staticMethod()).thenReturn(boolean);
 PowerMockito.when( mockObject.methodName(params))
.thenThrow(new Exception(“reason”));
 Invocation
 WhiteBox.invokeMethod(mockObject, “MethodName”);
 Verification
 To check whether a certain method is called or not
 PowerMockito.verifyNew(Sample.class).withNoArgument();
 PowerMockito.verifyStatic(times);
 PowerMockito.verifyPrivate(mockObject).invoke("privateMethod");
Unit testing with Junit, Mockito, PowerMockito
18
Unit testing with JUnit 19
Thank You

Introduction to Testing Frameworks

  • 1.
    JUnit , Mockito, PowerMockito Approachof Unit testing with the help of
  • 2.
    Unit Testing  Testingtools  Android Studio or Eclipse  How to create JUnit Test Case in Eclipse or Android Studio  Gradle Setup  Proper Dependencies Unit testing with Junit, Mockito, PowerMockito 2
  • 3.
    What should betested ?  Test for boundary line conditions  Test for both success and failure  Test for general functionality  Etc.. Unit testing with Junit, Mockito, PowerMockito 3
  • 4.
    Test case designtechnique  Test case design techniques can be broadly split into two main categories  Black box (functional)  White box (structural) Unit testing with Junit, Mockito, PowerMockito 4
  • 5.
    Black Box tests Targeted at the apparent simplicity of the software  Makes assumptions about implementation  Good for testing component interactions  Tests the interfaces and behavior Unit testing with Junit, Mockito, PowerMockito 5 Input Output
  • 6.
    White Box tests Targeted at the underlying complexity of the software  Intimate knowledge of implementation  Good for testing individual functions  Tests the implementation and design Unit testing with Junit, Mockito, PowerMockito 6 Input Output
  • 7.
    Which framework touse?  Black Box Testing  Junit  White Box Testing  Mockito  PowerMockito  Parameterized Testing  JunitParams Unit testing with Junit, Mockito, PowerMockito 7
  • 8.
    Junit 4  Gradledependency for Junit framework  testImplementation 'junit:junit:4.12’  Import statement for using the following annotations.  import org.junit.*;  @Test  Identifies a method as a test method.  @Before and @After  Executed before and after each test. It is used to prepare the test environment or teardown  @Ignore  Marks that the test should be disabled and the test case has not yet been adapted Unit testing with Junit, Mockito, PowerMockito 8
  • 9.
    TestCase lifecycle 1. @Before setup(){/* initialization*/ } 2. @Test testNameXYZ(){} 3. @After tearDown(){ /* de-initialization*/ } 4. Repeats 1 through 3 for each testNameXYZ method… Unit testing with Junit, Mockito, PowerMockito 9
  • 10.
    Junit Test Example ClassStudentTest{ public Student mStudent; @Before public void setup() { mStudent = new Student(); } @After public void tearDown() { mStudent = null; } @Test public getStateTest(){ //Setup Boolean expected = false; mStudent.setState(expected); //Execution Boolean result = mStudent.getState(); //verification Assert.assertEquals(expected, result); } } Unit testing with Junit, Mockito, PowerMockito 10 /*POJO*/ Class Student { private Boolean mState; public Boolean getState() { return mState; } public void setState(Boolean state) { mState = state; } }
  • 11.
    Junit Verification methods assertTrue(boolean condition)  checks that the boolean condition is true.  assertFalse(boolean condition)  checks that the boolean condition is false.  assertEquals(expected, actual)  tests that two values are the same  assertNull(obj)  checks that the object is null  assertNotNull(obj)  checks that the object is not null Unit testing with Junit, Mockito, PowerMockito 11
  • 12.
    JunitParams Framework  Parameterizedtest is to execute the same test over and over again using different values  Gradle dependency for JUnitParams framework  testImplementation “pl.pragmatists:JUnitParams:1.1.1”  Example public Object[] getScreenWidthParameters() { return new Object[] { new Object[] { 0, 0 }, new Object[] { 480, 480 }, new Object[] { -1, -1 }, new Object[] { null, null } }; } @Test @Parameters(method = "getScreenWidthParameters") public void getScreenWidth(int mScreenWidth, int expected) throws Exception { //setup mStudent.setState(expected); //Execution int result = mStudent.getScreenWidth(); //Verification Assert.assertEquals(expected, result); } Unit testing with Junit, Mockito, PowerMockito 12
  • 13.
    Problems with JunitFramework Unit testing with Junit, Mockito, PowerMockito 13 Class Sample { public BtManager mBtManager; private boolean doOperation() { //do something return (condition) ? true : false; } public void fetchData() { //result handled here itself boolean result = doOperation(); } // External dependency public void enableBt(){ //Here mBtManager is thirtparty object mBtManager.enableBt(); }
  • 14.
    Mockito & PowerMockito comesto the rescue  Mockito  Mockito is a mocking framework library enables mocks creation, verification and stubbing.  We just Mock the dependency class and we can test the Main Class  But it only support public methods  PowerMockito  PowerMock is a framework that extends other mock libraries such as EasyMock with more powerful capabilities  Helps to enable mocking of static methods, constructors, final classes and final methods, private methods, suppressing static initializers and allows manipulating private members  Currently PowerMock do not support Junit 5. Unit testing with Junit, Mockito, PowerMockito 14
  • 15.
    Mockito Framework  Gradledependency for Mockito framework  testImplementation 'org.mockito:mockito-core:2.19.0’  Annotated the class name with  @RunWith(MockitoJUnitRunner.class) class MyTestName() { }  We mock any class using @Mock annotation  @Mock BtManager mBtManager;  Mocking using Mockito Public Api  BtManager mBtManager = Mockito.mock(BtManager.class); Unit testing with Junit, Mockito, PowerMockito 15
  • 16.
    Mockito cont...  Stubbing It is done to change the behavior of dependencies  Mockito.when( mockObject).methodName().thenReturn( boolean);  Mockito.doNothing().when( mockObject).methodName();  Mockito.when( mockObject.methodName(params)) .thenThrow(new Exception(“reason”));  Verification to check a certain method is called or not  Mockito.verify( mockObject).methodName( parameters….);  Verification of private methods are not possible with Mockito Unit testing with Junit, Mockito, PowerMockito 16
  • 17.
    PowerMockito Framework  Gradledependency for PowerMockito framework  testImplementation "org.powermock:powermock-module-junit4:1.6.5“  testImplementation "org.powermock:powermock-module-junit4-rule:1.6.5"  testImplementation "org.powermock:powermock-classloading-xstream:1.6.5"  PowerMockRunner Annotation  @RunWith(PowerMockRunner.class) class MyTestName() { }  PrepareForTest Annotation  Annotation represents an array of class names, we want to mock.  @PrepareForTest({Sample.class, Student.class}) class MyTestName() { }  Mocking using Mockito Public Api  BtManager mBtManager = PowerMockito.mock(BtManager.class); Unit testing with Junit, Mockito, PowerMockito 17
  • 18.
    PowerMockito cont...  Stubbing PowerMockito.whenNew(Sample.class).withNoArguments() .thenReturn( sampleMock);  PowerMockito.when( mockObject.finalMethod()).thenReturn(boolean);  PowerMockito.when(Sample.staticMethod()).thenReturn(boolean);  PowerMockito.when( mockObject.methodName(params)) .thenThrow(new Exception(“reason”));  Invocation  WhiteBox.invokeMethod(mockObject, “MethodName”);  Verification  To check whether a certain method is called or not  PowerMockito.verifyNew(Sample.class).withNoArgument();  PowerMockito.verifyStatic(times);  PowerMockito.verifyPrivate(mockObject).invoke("privateMethod"); Unit testing with Junit, Mockito, PowerMockito 18
  • 19.
    Unit testing withJUnit 19 Thank You