TestNG vs Junit vs Mockito? - java

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?

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

What's the difference between #RunWith(MockitoJUnitRunner.class) and #RunWith(SpringJUnit4ClassRunner.class)? When to use it appropriately?

What's the difference between #RunWith(MockitoJUnitRunner.class) and #RunWith(SpringJUnit4ClassRunner.class)? When to use it appropriately?
MockitoJUnitRunner
specific for use with the Mockito test framework
the Mockito framework helps with mocking dependencies when you want to focus your tests on a single class and avoid invoking methods on dependencies (instead invokes a mock/dummy that is easily configured).
Above is what mockito is used for, but for more on this runner specifically - from the docs: "keeps tests clean and improves debugging experience". "Runner is completely optional - there are other ways you can get #Mock working". Source - https://static.javadoc.io/org.mockito/mockito-core/2.6.8/org/mockito/junit/MockitoJUnitRunner.html
SpringJunit4ClassRunner
specific for use with the spring framework
used for integration tests when it is required to load the spring context (create spring beans, perform dependency injection, etc).
In integration tests you may not do as much mocking of dependencies but you can do both in the same test.
Integration tests are useful when you would like to test loading the spring context or perhaps test from the service/high level all the way down to lower levels like data access with a single test.
In some cases you may want to use both - like an integration test where you would also like to mock some dependencies (perhaps they make remote calls). Unfortunately you can't use two #RunWiths but this is a good post about that - Multiple RunWith Statements in jUnit

Option of using spock lang specification test a Controller

just searching if spock lang specification can be used to test controllers that uses JDBC interface. I am not doing RESTful API and not using Grails. If Spock only supports grails then I guess Junit is another option. I like how spock mocks classes behaviors and how its code simplicity for unit testing. Any thoughts or suggestions?
Spock and Geb are totally independent of Grails. I never used Grails or even wrote Groovy application code in my whole life, I use Groovy only (and with pleasure) in order to write Spock and Geb tests for my Java applications or for any kind of web site.
FYI, Spock uses a JUnit runner itself, i.e. it builds upon JUnit and can be used as a full replacement for all your testing needs. It even comes with its own mocking capabilities, there is no need for Mockito or so anymore. If you had ever bothered to looked even briefly into the Spock documentation, you would have known, because none of those examples are related to Grails.

Can we write functional tests using junit?

I understand Junit is intended for unit testing. Can we write functional tests using junit as well? Like we write Integration tests using junit.
If functional tests are not going to be read by a non-tech user (aka customer) then it looks like a overkill to use tools such as Cucumber, Fitnesse etc. Given that I have a good knowledge of Junits - i want to reuse the same?
Also I notice it is possible to write functional tests using TestNG - Is it a good idea if junit is not suitable.
you can write any sort of test using junit. for example have a look at arquillian, which can boot a whole j2ee container from junit for testing, or fest, which enables swing UI testing from within junit, or use a selenium junit4 runner to test web applications from junit (combine with arquillian to boot the web application 1st :-) )

spring with testNG without subclassing

is there any way to use testNG and spring without subclassing or copying half of AbstractTestNGSpringContextTests? is there anything simple like junit's #RunWith?
According to the Spring documentation, it doesn't look like it provides a TestNG test runner out of the box:
In addition to generic testing infrastructure, the TestContext framework provides explicit support for JUnit 3.8.2, JUnit 4.5+, and TestNG 5.10 in the form of abstract support classes. For JUnit 4.5+, the framework also provides a custom Runner that allows one to write test classes that are not required to extend a particular class hierarchy.
I suppose you could try to write a your own custom TestNG test runner that could do something similar.

Junit and EasyMock understanding clarifications

Still Now I am using JUnit, I came across EasyMock, I am understanding both are for the same purpose.
Is my understanding correct?
What are the advantages does EasyMock has over the Junit?
Which one is easier to configure?
Does EasyMock has any limitations?
Please help me to learn
When I explain unit testing I like to describe them as a list of phases:
Test Setup : Define and Create all the Data and Objects you need for the tests
Expectations : Say what methods and parameters you expect to be executed during the test
Test : The actual behavior/method call you want to test
Assertions : Statements that make sure the outcome of the test were successful
Test Tear down : Destroy any side effects that occurred during the test
jUnit is a Unit Testing Framework and provides all but the Expectations phase of testing. Alternatives in the Java space include:
TestNG
jtest
jBehave (sort of)
jDave (sort of)
Other Language equivalents include:
PHP - phpUnit
Ruby - Test::Unit
Flash - FlexUnit
The concept of mocking is what added the new phase of Expectations, and since jUnit saw most of it's major development prior to the mocking movement, those features were not incorporated into the core, and a set of tools to fill that gap in the java space have opened up. Those libraries include
EasyMock
jMock
jMockIt
All of these libraries are compliments to any of the above Unit Testing frameworks I listed, including jUnit. They add the ability to define mock objects. Mock objects get "expectations" assigned to them, which are then asserted in the Assertions phase. Each Mock library accomplishes this slightly differently, but the major models are
Record Replay - EasyMock
Expectations - jMock, jMockIt
I personally am a fan of the Expectations approach, which is more declarative, and less error prone, because it requires less methods to be called by the implementor, but that is a stylistic preference not a technical one.
Other Languages (since they came to the unit testing world later than java) don't have this seperation for the most part. The Unit Testing Library and Mock Library are one and the same. This is the case in phpunit, rspec. I imagine jUnit will not be incorporating this natively any time soon, since there is already such a rich set of alternative mock libraries available.
They are not the same thing
JUnit is a xUnit testing framework - It has a test runner that loops over your test suites, executes each automated unit test and records the results.
EasyMock is a mock-object framework. It is used to replace hard to setup collaborators with dummies/fakes/mocks to help focus on the behavior I intend to test.
e.g. if my SUT.AuditCustomers() calls DAO.GetCustomer(databasePath), in my test I'd like to focus on the method AuditCustomers(), so I'd use a mockDAO which does not read the customers from the database instead returns known/hardcoded Customer objects to easy testing. This also has the benefit that any bug in GetCustomer does not fail the test for AuditCustomers()
I am not sure but I think JUnit and EasyMock instead of being exclusive to each other are supposed to work in tandem together.
JUnit idea is to test a given method and therefore you want to inject dummy instances of other classes into it, to ensure the JUnit test is not dependent on other class methods at all. This purpose of providing mock objects in JUnit is served by EasyMock and other similar mock object creators. Similar idea is used when using spring to inject dummy implementations into JUnit.
EasyMock seems to be promising, but you should evaluate if Spring or some other proxy object generators fit your scenario.

Categories