TestNG vs Junit vs Mockito?

Viewed 2568

I am having difficulty understanding the difference between these 3. Aren't all of these testing frameworks, and not libraries? If so, how is it that we are able to use these in combination? Shouldn't we be able to only use one testing framework, since the framework dictates the flow of control of code unlike in the case of libraries where we call the library code from our code. How can 2 independent frameworks work together on the same piece of code? I've observed that Mockito is used in conjunction with TestNG a lot. Isn't TestNG sufficient for most testing scenarios?

1 Answers

Both Testng and Junit are Testing framework used for Unit Testing. TestNG is similar to JUnit. Few more functionalities are added to it that makes TestNG more powerful than JUnit.

Annotations Both JUnit and TestNG uses annotations and almost all the annotations looks similar.

TestNG uses @BeforeMethod ,@AfterMethod similar to @Before ,@After in JUnit4.

.........................

Mockito is a java based mocking framework, used in conjunction with other testing frameworks such as JUnit and TestNG.

It internally uses Java Reflection API and allows to create objects of a service. A mock object returns a dummy data and avoids external dependencies. It simplifies the development of tests by mocking external dependencies and apply the mocks into the code under test.

Summary

So as its name suggest Mockito is used for mocking the data or fake data which is act as real object to test, its acts like an stub or driver while still we need junit or testng with it. while Junit and TestNG is a unit testing framework of java

Explanation Tutorial

https://www.youtube.com/watch?v=eILy4p99ac8

Article on Mockito

https://www.journaldev.com/21816/mockito-tutorial

Related