Junit and EasyMock Notes

Now I am using JUnit, I came across EasyMock, I understand that both of them are for the same purpose. Do I understand correctly?

What are the advantages of EasyMock over Junit?

Which one is easier to configure?

Does EasyMock have any limitations?

Please help me find out

+2


a source to share


3 answers


When I explain unit testing, I like to describe them as a list of phases:

  • Test Setup: defining and creating all data and objects required for tests
  • Expectations: Tell me what methods and parameters you plan to execute during testing.
  • Testing. The actual call to the behavior / method you want to test.
  • Assertions: Statements that guarantee the successful completion of the test.
  • Test Tear down: destroy any side effects that occurred during the test

jUnit is a unit testing framework and provides everything except the Waiting phase of testing. Alternatives in the Java space include:

  • TestNG
  • Jtest
  • jBehave (sort)
  • jDave (sort)

Other language equivalents include:

  • PHP - phpUnit
  • Ruby - Test :: Unit
  • Flash - FlexUnit


The concept of mock is what added a new phase of anticipation, and since jUnit saw much of this mainstream development before the mock move, these features were not included in the core and a toolbox to fill that gap in the Java space was opened. These libraries include

  • EasyMock
  • JMock
  • JMockit

All of these libraries are compliments to any of the above Unit Testing frameworks I've listed, including jUnit. They add the ability to define the layout of objects. Mock objects receive "waits" assigned to them, which are then validated in the approvals phase. Each Mock library does this slightly differently, but the main 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 as it requires fewer methods for the developer to call, but this stylistic preference is not technical.

Other languages ​​(since they came into the unit testing world after java), for the most part, there is no such separation. The unified test library and the mock library are one and the same. This applies to phpunit, rspec. I suppose jUnit won't include this natively anytime soon, as there is already such a rich set of alternative layout libraries.

+7


a source


They are not the same thing.

JUnit is an xUnit framework - it has a test runner that iterates over your test packages, runs each automatic unit test, and records the results.



EasyMock is a fake object structure . It is used to replace hard to customize collaborators with dummies / fakes / mockups to help focus on the behavior I intend to test.
for example if my SUT.AuditCustomers () calls DAO.GetCustomer (databasePath), in my test I would like to focus on the AuditCustomers () method, so I would use mockDAO which does not read customers from the database, but returns / hardcoded Customer for simple testing. This also has the advantage that any mistake in GetCustomer will not fail the test for AuditCustomers ()

+3


a source


I'm not sure, but I think that JUnit and EasyMock, instead of being exclusive to each other, should work together in tandem. The idea behind JUnit is to test this method, and therefore you want to inject dummy instances of other classes into it to make sure the JUnit test doesn't depend on other methods in the class. This purpose of providing mock objects in JUnit is served by EasyMock and other similar mock creators. A similar idea is used when using spring to inject dummy implementations into JUnit.

EasyMock seems promising, but you have to evaluate if spring or some other proxy object generators is the right fit for your scenario.

0


a source







All Articles