Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit mocks, which tool should i use? [closed]

I come from the PHP testing world and i'm starting testing in Java.

I've seen several tools to mock the SUT in JUnit, like Mockito, SevenMock, ClassMock, etc.

I really appreciate any recommendation of which one should i use.

Thanks in advance!

like image 222
victorgp Avatar asked Mar 24 '11 10:03

victorgp


People also ask

Which is better JMockit or Mockito?

JMockit will be the chosen option for its fixed-always-the-same structure. Mockito is more or less THE most known so that the community will be bigger. Having to call replay every time you want to use a mock is a clear no-go, so we'll put a minus one for EasyMock. Consistency/simplicity is also important for me.

Do I need to reset mocks?

Normally, you will not need to reset your mocks; instead, you will need to create new mocks for each test method. Instead of using reset(), consider writing short, focused test methods rather than lengthy, over-specified tests.

What is difference between JUnit and Mockito?

JUnit is the Java library used to write tests (offers support for running tests and different extra helpers - like setup and teardown methods, test sets etc.). Mockito is a library that enables writing tests using the mocking approach.

What does MockitoAnnotations openMocks do?

The MockitoAnnotations. openMocks() method returns an instance of AutoClosable which can be used to close the resource after the test. The MockitoAnnotations. openMocks(this) call tells Mockito to scan this test class instance for any fields annotated with the @Mock annotation and initialize those fields as mocks.


2 Answers

I've used Mockito quite a lot. http://mockito.org/ I have not used EasyMock so cant say much about it.

Using Mockito is straightforward but the classes that you intend to test should also be in a decoupled state which will make it easier to test. With mockito you are instantiating a particular class with mocks objects.

Say you got a class that you want to test, but want to mock one of its dependencies

final DepedencyToMockClass mockObject = mock(DepedencyToMockClass.class);
when(mockObject.getTestMethod()).thenReturn("Test");

Now this mockObject can now be injected when initializing your intended class.

final ClassToTest test = new ClassToTest(mockObject);

Mockito uses reflection to create these mock objects. However if you have a dependency and if it is declared final then mocking will fail.

Another useful method in Mockito is verify where you can verify certain operations in your mock objects. Have a peep at mockito. However there are limitations in mock objects, in some cases it will be hard to create mock objects perhaps external/third party code. I think it's good practise to attempt to instantiate real objects when injecting them for testing purposes, failing which Mockito helps.

like image 172
MalsR Avatar answered Oct 26 '22 23:10

MalsR


Mockito seems to be most often used

edit:

Comparison between Mockito vs JMockit - why is Mockito voted better than JMockit?

EasyMock vs Mockito: design vs maintainability?

http://www.dahliabock.com/blog/2009/08/21/mocking-mockito-vs-easymock-example/

like image 36
dantuch Avatar answered Oct 27 '22 01:10

dantuch