In my implementation class I have a read write lock definition as follows,
#Inject
#Named("CustomizedLock")
private ReentrantReadWriteLock rwLock;
I'm using it in a method named run() as,
rwLock.writeLock().lock();
to lock the process. But when I'm trying to test this with mockito I found ReentrantReadWriteLock is initialized. But when I'm trying to get rwLock.writeLock() it's null. Here is my test.
#Mock
private ReentrantReadWriteLock feedReadWriteLock;
#InjectMocks
private CustomModule module = mock(CustomModule.class);
/////////////////////////
#Test
public void test() {
when(module.getReadWriteLock()).thenReturn(mock(ReentrantReadWriteLock.class));
PowerMockito.doReturn(new ReentrantReadWriteLock()).when(module.getReadWriteLock());
cacheJob.run();
}
As I said rwLock.writeLock() is null but rwLock is initialized. Please explain how this happens with mockito. And what is the ideal way to do this?
You are getting mocking wrong:
#Mock
private ReentrantReadWriteLock feedReadWriteLock;
The above creates a mock that you then somehow have to get into your class under test.
But this:
#InjectMocks
private CustomModule module = mock(CustomModule.class);
is bogus. The InjectMocks annotation exists to do that "getting it into" part for you.
In other words, you should be doing something like:
#Mock
private ReentrantReadWriteLock feedReadWriteLock;
#InjectMocks
private CustomModule module = new CustomModule();
for example. In other words: you do not mock an instance of your class under test. You create a "real" instance - and then you have to "insert" the required mock objects into that real instance. There is absolutely no point in mocking the class under test. Because you want to test your code, not something that the mocking framework mocked for you.
The InjectMocks annotation tries to do that for you (using reflection and all kinds of black magic). Unfortunately when it can't do its job, it just silently fails.
In that sense, the answer is: don't just blindly use stuff. You have to fully understand each and any line of code that you write down. Thus: read this to understand the difference between these annotations.
And just for the record: please note that you also have to either use the Mockito JUnitRunner or to manually call Mockito.initMocks() in order have the annotations do their magic.
If you want to mock the class under test, you should use #Spy.
#RunWith(MockitoJUnitRunner.class)
public class CustomModuleTest {
#Mock
private ReentrantReadWriteLock feedReadWriteLock;
#Spy
#InjectMocks
private CustomModule module;
#Test
public void test() {
doReturn(feedReadWriteLock).when(module).getReadWriteLock();
cacheJob.run();
}
}
Note that the syntax is a bit different with a spy than with a mock. The spy allows your to instantiate the real object, but mock some methods as you wish.
Here, I return feedReadWriteLock, which is annoted with #Mock, this way, you may be able to change its behavior. You don't want to return a new instance like you did in your example.
Related
after hours of tries and fails I come to you in hope of a solution.
I'm struggle making unit tests for my spring boot application. I'm using mockito and Junit 5.
My architecture is made out like this:
A controller
An interface of the service
A implementation of the service interface
A repository extending CrudRepository<Entity, Long>
For now I just want to test out my service implementation.
This is how it looks like for now :
`
#SpringBootTest public class ServiceImplTest{
#Mock
private Entity e;
#MockBean
private EntityRepository entityRepository;
#MockBean
private EntityService entityService;
#BeforeEach
init(){
e = new Entity();
e.name ="abc";
}
#Test
private simpleTest(){
// saving my element in the mocked repository
entityRepository.save(e);
// I have a repository query to delete an element in a specific way. I ask it to return 1 if it receives the order to activate this method
doReturn(1).when(entityRepository).specialDeleteEntity(1L);
// in the code serviceDeleteEntity() does some operations then calls entityRepository.specialDeleteEntity
int howMany = entityService.serviceDeleteEntity(1L);
// this fails because there was nothing in the repository to be deleted
assertEquals(howMany, 1);
}
}
I just have a feeling the the Mocked Repository is not connected to my Mocked Service and by this, the operations between them don't work.
I have also tried another solution where I didn't mock the repository , just in case :
#SpringBootTest class ServiceImplTest {
#MockBean
private EntityRepository mockEntityRepository;
#Autowired
private EntityService entityService;
#Test
void testDelete() {
// Given
final Entity entity = new Entity();
entity.name = "abc";
// Setup
when(mockEntityRepository.specialDeleteEntity(1L)).thenReturn(1);
// When
final int result = entityService.specialDeleteEntity(1L);
// Then
assertThat(result).isEqualTo(1);
verify(mockEntityRepository).specialDeleteEntity(1L);
}
}
I may lack some anotations or some methods maybe. I just want your advice on the problem and maybe a step towards the solution. Thank you very much.
There are a few issues with your test:
1. Wrong use of mocks
A mock is a dummy implementation of a class. You use them to bypass the normal behaviour of those classes. For example, if you write a unit test for EntityService, you don't want to set up an entire database and insert/delete mock data. So in that case you want to mock dependencies such as EntityRepository.
This means that in your EntityServiceTest you should only be mocking EntityRepository. You shouldn't mock Entity because this class normally doesn't contain a lot of behaviour and you shouldn't mock EntityService because you want to test the actual behaviour of this service and not of a mock.
2. Choose one mocking framework
Your test is currently using a combination of #Mock and #MockBean.
Both of these annotations allow you to mock a class.
The difference is that #Mock relies only on Mockito and #MockBean mocks the class and creates a Spring bean of it to use for autowiring.
This means that the #MockBean annotation only works when you run (a part of) your Spring Boot application.
This is why you use the #SpringBootTest annotation.
The downside is that this might require additional configuration and slows down the tests due to starting/stopping the application.
If you only need to unit test a single class with some mocks, it would be easier to write a test with Mockito only.
To do so, use the #Mock annotation and replace #SpringBootTest with #ExtendWith(MockitoExtension.class):
// Replace #SpringBootTest with #ExtendWith to enable testing with Mockito
#ExtendWith(MockitoExtension.class)
class EntityServiceTest {
// Add #InjectMocks to the class where the mocks should be injected into.
// Normally this is the class that you want to test
#InjectMocks
private EntityService service;
// Use #Mock in stead of #MockBean
#Mock
private EntityRepository repository;
// ...
}
3. Test the behaviour of your class
As I mentioned before, a mock is a dummy implementation of a class.
So that means that if you call repository.save(..), it doesn't really do anything as there's no behaviour and no database behind it.
What you actually want to test is whether the service. serviceDeleteEntity() method calls the repository. specialDeleteEntity() method with the right arguments.
This means that your second example of your test is the right way.
The only thing you don't need is the entity because your test doesn't rely on it (assuming that your service passes the id argument to the repository and returns the result of the query):
#Test
void testDelete() {
// Given
when(mockEntityRepository.specialDeleteEntity(1L)).thenReturn(1);
// When
final int result = entityService.specialDeleteEntity(1L);
// Then
assertThat(result).isEqualTo(1);
verify(mockEntityRepository).specialDeleteEntity(1L);
}
thanks to #g00glen00b for the solution.
#ExtendWith(MockitoExtension.class)
public class EntityServiceImplTest {
#InjectMocks
private EntityServiceImpl entityServiceImpl;
#Mock
private EntityRepository entityRepository;
#Test{
Entity e = new Entity();
when(entityRepository.getEntityById(1L)).thenReturn(e);
final Entity result = entityServiceImpl.getEntityById(1L);
assertEquals(e, result);
}
}
The issue here was that i had to use MockitoExtension on one hand.
Another problem was that, in my service implementation method , there was a hidden "new Timestamp(System.currentTimeMillis())" used to call the method in the repository.
So there was always a difference between the time I "promised" and the time created during the call of the service implementation.
Thank you again
I think you have to insert the mocked repository into your service in order it to be used:
entityService.setRepository(mockEntityRepository);
I have two mocked objects from the same class only difference between the two objects is a different state. Before I can test the state difference I need to stub some methods, this means I have to do it for both objects which doesn't seem efficient to me.
I have following setup for a unit test on java:
#Mock
private CuratorFramework zkClient;
#Mock
private CuratorFramework zkClientNotStarted;
#BeforeEach
void beforeEach() {
when(zkClient.getState()).thenReturn(CuratorFrameworkState.STARTED);
when(zkClient.checkExists()).thenReturn(existsBuilder);
when(zkClient.exampleMethod()).thenReturn(exampleObject);
...
when(zkClientNotStarted.getState()).thenReturn(CuratorFrameworkState.LATENT);
when(zkClientNotStarted.checkExists()).thenReturn(existsBuilder);
when(zkClientNotStarted.exampleMethod()).thenReturn(exampleObject);
...
}
Which rubs me the wrong way because there is a lot of repeated code and couldn't find anything that resemble something like this:
#Mock
private CuratorFramework zkClient;
#Mock
private CuratorFramework zkClientNotStarted;
#Mock
private CuratorFramework zkClientBase;
#BeforeEach
void beforeEach() {
when(zkClientBase.checkExists()).thenReturn(existsBuilder);
when(zkClientBase.checkExists()).thenReturn(existsBuilder);
when(zkClientBase.exampleMethod()).thenReturn(exampleObject);
...
zkClient.inheritStubs(zkClientBase)
when(zkClient.getState()).thenReturn(CuratorFrameworkState.STARTED);
zkClientNotStarted.inheritStubs(zkClientBase);
when(zkClientNotStarted.getState()).thenReturn(CuratorFrameworkState.LATENT);
}
Is there a way to clone/copy a mock to a new mock which inherits the initialized stubs? Or is this wrong use case for the Mockito framework?
Only similar question I found is this one: Mockito: Mock object based on another
And that one is asked more than 5 years ago. I hope something has changed during that time.
Is it possible to both mock an abstract class and inject it with mocked classes using Mockito annotations. I now have the following situation:
#Mock private MockClassA mockClassA;
#Mock private MockClassB mockClassB;
#Mock(answer = Answers.CALLS_REAL_METHODS) private AbstractClassUnderTest abstractClassUnderTest;
#Before
public void init() {
MockitoAnnotations.initMocks(this);
Whitebox.setInternalState(abstractClassUnderTest, mockClassA);
Whitebox.setInternalState(abstractClassUnderTest, mockClassB);
}
I'd like to use something like #InjectMocks on AbstractClassUnderTest but it can't be used in combination with #Mock. The current situation, with Whitebox from Powermock, works but I'm curious if it's possible to solve it with just annotations. I couldn't find any solutions or examples.
(I know about the objections to test abstract classes and I'd personally rather test a concrete implementation and just use #InjectMocks.)
I am not aware of any way to go about this, for one clear reason: #InjectMocks is meant for non-mocked systems under test, and #Mock is meant for mocked collaborators, and Mockito is not designed for any class to fill both those roles in the same test.
Bear in mind that your #Mock(CALLS_REAL_METHODS) declaration is inherently dangerous: You're testing your AbstractClassUnderTest, but you are not running any constructors or initializing any fields. I don't think you can expect a test with this design to be realistic or robust, no matter what annotations can or cannot do for you. (Personally, I was previously in favor of real partial mocks of abstract classes as a "tool in the toolbox", but I'm coming around to thinking they're too far removed from reality to be useful.)
Were I in your position, I would create a small override implementation for testing:
#RunWith(JUnit4.class) public class AbstractClassTest {
/** Minimial AbstractClass implementation for testing. */
public static class SimpleConcreteClass extends AbstractClass {
public SimpleConcreteClass() { super("foo", "bar", 42); }
#Override public void abstractMethod1() {}
#Override public String abstractMethod2(int parameter) { return ""; }
}
#InjectMocks SimpleConcreteClass classUnderTest;
#Mock mockClassA;
#Mock mockClassB;
}
At this point, you have a simple and predictable AbstractClass implementation, which you can use even without a mocking framework if you just wanted to test that AbstractClass has the same API for extension that it did before. (This is an often-overlooked test for abstract classes.) You can even extract this, as it may be useful for other testing: Should you want to override the abstract behavior for a single test class, you can create an anonymous inner class with just a single method override, or you can set classUnderTest = spy(classUnderTest); to set up Mockito proxying and the behavior you want.
(Bear in mind that #InjectMocks and #Spy can't be used reliably together, as documented in this GitHub issue and the Google Code and mailing list threads to which it links.)
I found some trick with mocking field before initialization.
#InjectMocks
private AbstractClass abstractClass;
#Mock
private MockClass mockClass;
#Before
public void init() {
abstractClass= mock(AbstractClass.class, Answers.CALLS_REAL_METHODS);
MockitoAnnotations.initMocks(this);
}
Maybe it'll help someone.
valid construction:
#InjectMocks
SomeClass sc = mock(SomeClass.class);
Invalid construction:
#InjectMocks
#Mock
SomeClass sc;
I want to inject mocks to another mock. I want to use only annotation style.
Why was in Mockito forbid second construction ?
Update
example:
public class ArrTest {
private SomeClass someClass;
public List<String> foo(){
anotherMethod(); // I suppose that this method works. I want to test it separately.
//logic which I need to test
return someClass.doSmth();// I suppose that this method works. I want to test it separately.
}
public void anotherMethod(){
///...
}
}
public class SomeClass {
public List<String> doSmth(){
return null;
}
}
test:
public class ArrTestTest {
#InjectMocks
ArrTest arrTest = Mockito.mock(ArrTest.class);
#Mock
SomeClass someClass;
#Test
public void fooTest(){
Mockito.when(someClass.doSmth()).thenReturn(new ArrayList<String>());
Mockito.doNothing().when(arrTest).anotherMethod();
System.out.println(arrTest.foo());
}
}
It sounds like you're trying to do something that doesn't really make sense. You shouldn't need to inject any dependencies into your mock since mocks by definition don't have any behaviour until you define it with when(mock.someMethod()).thenAnswer() or some variation.
(except perhaps if you're using a spy(), but you've specifically said you're using a #Mock).
Maybe you could explain your use case and why you're trying to inject dependencies into a mock?
#InjectMocks specifically indicates that the annotated field will NOT contain a mock. Annotating #InjectMocks #Mock is not just unsupported—it's contradictory.
To return stubs wherever possible, use this:
#Mock(answer=Answers.RETURNS_DEEP_STUBS)
YourClass mockYourClassWithDeepStubs;
But heed the official documentation for this Answer:
WARNING: This feature should rarely be required for regular clean code! Leave it for legacy code. Mocking a mock to return a mock, to return a mock, (...), to return something meaningful hints at violation of Law of Demeter or mocking a value object (a well known anti-pattern).
Good quote I've seen one day on the web: every time a mock returns a mock a fairy dies.
A mock doesn't have any real implementation. #InjectMocks would try to find and call setters for whatever mock objects have already been created and pass them in. Mockito "knows" that this is kinda pointless on a mock, since there won't be any way to get the mock objects back out, much less do anything meaningful with them.
How can you get a mock object in at runtime when it is not created/initialized in the class you are testing, it is not static (singleton pattern), or you don't have some sort of test constructor to hook into?
In a class that I am writing some unit testing for, I have come across a scenario I haven't encountered/solved yet. I have a JMS resource (a QueueConnectionFactory for reference, but it shouldn't matter), that is a private variable of the class I am testing. Since it has the javax.annotation.Resource annotation, at runtime it is assumed to be available. During testing, it is not, which creates the need for mocking this object.
It is not a static class and is not being used in a static way, if it was I could easily mock using the various static mocking methods I have run into. Since the resource is never created locally (in a constructor or even in a test constructor), I have no way of passing in a Mock object so that at runtime of the test, the mock is used instead of the actual object. How can I mock this Resource so that when the test executes, it will be used in place of the private #Resource object in the class I am testing?
For reference, the code is calling createConnection() on the QueueConnectionFactory which is throwing a null pointer exception since the Factory has not been initialized/mocked.
#Stateless
public class Example{
#Resource(name = "jms/exampleQCF")
private QueueConnectionFactory queueFactory;
...
public void testMe(){
Connection connection = queueFactory.createConnection();
...
}
}
After a lot more hunting around and looking at all the options Mockito/Powermock had to offer, I found the solution (which I will share in case others run into this same issue).
When you have private member variables that are never initialized (and just assumed created in other places), you can use the #InjectMocks annotation to "inject" Mocks you want into your class you are testing.
Add a variable in your test class for the class you are testing, and give it the annotation #InjectMocks (org.Mockito.InjectMocks).
Use #Mock annotations to setup the mocks you want to inject. Use the #Mock (name = "privateVariableNameHere") name property to map the Mock object to the private variable inside your class you are testing.
In either a setup function or before you call your class, initialize the mocks. The easiest way I have found is to use a "setup" method with the #Before annotation. Then inside there call MockitoAnnotations.initMocks(this); to quickly initialize anything with the #Mock annotation.
Define your Mock functionality in your test method (before calling the method you are testing).
Using the #InjectMock object, call your method you are testing... the mocks SHOULD be hooked in and working as defined in the earlier steps.
So for the example class I use above, the code to test/mock would have Connection returned as a mock which you can do whatever with. Based on the example above in my question, this is what the code would look like:
#RunWith(PowerMockRunner.class)
#PrepareForTest({/* Static Classes I am Mocking */})
public class ExampleTest {
#Mock (name = "queueFactory") //same name as private var.
QueueConnectionFactory queueFactoryMock;
#Mock
Connection connectionMock; //the object we want returned
#InjectMocks
Example exampleTester; //the class to test
#Before
public void setup(){
MockitoAnnotations.initMocks(this); // initialize all the #Mock objects
// Setup other Static Mocks
}
#Test
public void testTestMe(){
//Mock your objects like other "normally" mocked objects
PowerMockito.when(queueFactoryMock.createConnection()).thenReturn(connectionMock);
//...Mock ConnectionMock functionality...
exampleTester.testMe();
}
}
Several approaches here:
ReflectionTestUtils of Spring Testing framework: ReflectionTestUtils.setField(objectToTest, "privateFieldName", mockObjectToInject);. With this you don't introduce another dependency.
org.mockito.internal.util.reflection.FieldSetter.
PowerMock.Whitebox.setInternalState() to mock a private field.
If you need to mock internal local variable creation, use PowerMockito.whenNew(Foo.class).withNoArguments().thenReturn(foo);. Very, very useful. Cannot find other ways to do the same.
With only Mockito you cannot mock local variable creation, because when(any(Foo.class) does not work; will return null. It compiles but does not work.
References:
Mockito: Mock private field initialization