Do you have questions ? We got the answers!

Ask a question:

1
How to mock the data in Testng ? as like in junit

Open 1 Answers 28 Views
I am panning to prepopulate data from DB for api automation. how to mock the data before starting the test. Please provide me some example.

1 Answer

0

You can try exploring Mockito framework: http://site.mockito.org/

Github: https://github.com/mockito/mockito

Gradle dependency: dependencies { testCompile "org.mockito:mockito-core:1.+" }

 

Sample:

 

Verify interactions:
 
import static org.mockito.Mockito.*;
 
// mock creation
List mockedList = mock(List.class);
 
// using mock object - it does not throw any "unexpected interaction" exception
mockedList.add("one");
mockedList.clear();
 
// selective, explicit, highly readable verification
verify(mockedList).add("one");
verify(mockedList).clear();
 
stub method calls
 
// you can mock concrete classes, not only interfaces
LinkedList mockedList = mock(LinkedList.class);
 
// stubbing appears before the actual execution
when(mockedList.get(0)).thenReturn("first");
 
// the following prints "first"
System.out.println(mockedList.get(0));
 
// the following prints "null" because get(999) was not stubbed
System.out.println(mockedList.get(999));

Hope that helps. 

answered Nov 25, 2016 by stbadmin (4,970 points)

Top Contributors

  • 4970 pointsstbadmin

    34 questions 454 answers218 comments

  • 433 pointsSunilBhaskar

    41 questions 15 answers32 comments

  • 425 pointsMehedi

    36 questions 3 answers14 comments

  • 364 pointssumitkumarwatts

    0 questions 14 answers0 comments

  • 354 pointsSoft Tester

    43 questions 0 answers4 comments

  • 327 pointsKeeshaGolden

    0 questions 17 answers2 comments

  • 322 pointsVipul Sharma

    44 questions 0 answers24 comments

  • 310 pointsvysh.sharma

    25 questions 0 answers0 comments

  • 267 pointsOnkar_Nimje

    33 questions 15 answers15 comments

  • 258 pointsVanitha

    26 questions 0 answers6 comments

...