0% found this document useful (0 votes)
66 views13 pages

Junit in Detail

This document provides an introduction to test-driven development (TDD) using JUnit and mocking frameworks like Mockito and PowerMock. It discusses unit testing and JUnit basics like annotations and assertions. It also demonstrates various mocking scenarios using Mockito and PowerMock, including mocking private, static, final methods, and new object instantiation. Examples are provided for JUnit 4 and 5 test cases.

Uploaded by

Deepak Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views13 pages

Junit in Detail

This document provides an introduction to test-driven development (TDD) using JUnit and mocking frameworks like Mockito and PowerMock. It discusses unit testing and JUnit basics like annotations and assertions. It also demonstrates various mocking scenarios using Mockito and PowerMock, including mocking private, static, final methods, and new object instantiation. Examples are provided for JUnit 4 and 5 test cases.

Uploaded by

Deepak Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

• Introduction to Test Driven Development

• What is Unit Testing and Junit

• Need of Mock Frameworks

• Mockito and Powermock


- various mocking scenarios –Demo

• Introduction to Junit-5 and its basic Features.


• What is Test driven development ?
--The Process

• Why Junit
-- The Tool
Test driven development
• Each new feature begins with writing a test.
• Makes the developer focus on the
requirements before writing the code.
• Tests might fail since tests are developed even
before the development.
• In order to pass the test, the development
team has to develop and refractor the code
TDD flow..with Example.

Unit Test

Code

Refractor
• Junit Intro, Installation/Set Up

• Junit Basics: Annotations Usages, Junit Assert

• Junit Sample Test Case : Junit 3.x vs Junit 4.x

• Junit Test Suite


Examples contd..
• @Parameterized
Mocking framework - Mockito
• MockitoRunner
• Rules-
• Use
the @RunWith(MockitoJUnitRunner.class) annotati
on at the class-level of the test case.
• //@InjectMocks annotation is used to create and
inject the mock object
• @InjectMocks
• MathApplication mathApplication = new
MathApplication();
Mocking framework - Powermock
• PowermockRunner
• Mocking private methods
• Mocking static methods
• Mocking Final methods
Mocking private methods
• Use the @RunWith(PowerMockRunner.class) annotation at
the class-level of the test case.
• Use
the @PrepareForTest(ClassWithPrivateMethod.class) annot
ation at the class-level of the test case.
Mocking private void method-
• classToBeTested = PowerMockito.spy(new
ClassToBeTested());
PowerMockito.doNothing().when(classToBeTested,
"privateMethod", arg1,arg2…);
• Mocking private non-void method-
doReturn(8).when(classToBeTested , "somePrivateMethod",
arg1, arg2…);
Mocking static methods
• Add @PrepareForTest at class level.
• @PrepareForTest(ClassWithStaticMethod.class)
Mocking static Non-void method-
• PowerMockito.mockStatic(ClassToBeTested.class);
• PowerMockito.when(ClassToBeTested.staticNonvoidMe
thod(arg)).thenReturn(retValue);
Mocking static void method-
• PowerMockito.mockStatic(ClassToBeTested.class);
• PowerMockito.doNothing().when(ClassToBeTested.clas
s);
• ClassToBeTested.saticVoidMethod(arg1, arg2);
Mocking final methods
• Add @PrepareForTest at class level.
• @PrepareForTest(ClassWithFinalMethod.class
)
Mocking static Non-void method-
• AFinalClass mockClass=
PowerMockito.mock(AFinalClass.class);
• Mockito.when(mockClass.finalMethod(testInp
ut)).thenReturn(mockedResult);
Mocking New operator
• I have a legacy class that contains a new() call to instantiate a LoginContext():
• public class TestedClass { public LoginContext login(String user, String password) {
LoginContext lc = new LoginContext("login", callbackHandler); } }
• Using Mockito-
TestedClass tc = spy(new TestedClass());
LoginContext lcMock = mock(LoginContext.class); when(tc.login(anyString(),
anyString())).thenReturn(lcMock);
Using Powermock-
@RunWith(PowerMockRunner.class)
@PrepareForTest(TestedClass.class)
public class TestedClassTest {
@Test public void testLogin() {
LoginContext lcMock = mock(LoginContext.class);
whenNew(LoginContext.class).withArguments(anyString(),
anyString()).thenReturn(lcMock);
TestedClass tc = new TestedClass();
tc.login ("something", "something else"); // test the login's logic } }
Junit Examples-4
• Mockito with Powermock

You might also like