Using mockito is there a reason to nullify mocks in #After? - mockito

I'm wondering,
If in #Before method I'm initializing a mock objects, shouldn't I nullify references to it in #After ? Or would that be redundant? And why?

Not necessary, JUnit creates a new instance of the test per test method.
However if it's static fields it's another story, and proper lifecycle should be implemented, but I strongly advise you to not use static fields in a JUnit test ! Instead think about implementing your own JUnit Runner.
And for TestNG, it's a different story, as TestNG creates a single instance of the test, so you have to be careful there on the lifecycle of the mocks.

"Nullifying" reference doesn't change anything here.
#Before annotated method is ran before each test method. If you are initializing mocks in such method they will be reinitialized before each test. There is a different annotation - #BeforeClass, this annotation causes a method to be executed only once before execution of any test method in that test class. In this case however "nullifying" a reference will not help you because you still need to create a new mock object and assign its reference to your field.

Related

A Mocked repository returns null - Springboot JUnit Mockito

Im trying to use the save from my repository EmpresaRepository, but it returns null, heres my repository
heres my repository
and this is my test
And this is the output
This is the expected behaviour.
By using #MockBean you create and add a Mock of your EmpresaRepository into the ApplicationContext.
This means you execute the ::save() Method of your mock which returns null bey default.
Rule of thumb: Never Mock the thing you want to test.
The test should work if you replace the #MockBean annotation with Autowired.
Btw. please don't post screenshots of your code. It's annoying to switch between different tabs and also I'm not able to copy parts of your code.

Mockito junit 5 mock constructor

I want to mock a constructor and return a mock object when the constructor is called. This can be achieved using powermockito's whenNew method like this.
PowerMockito.whenNew(ClassName.class).withAnyArguments().thenReturn(mockObject);
Since Junit5 doesn't have powermockito support yet, I need to know if this can be achieved using Mockito.
Mockito 3.5 has added alot of PowerMock's functionality into core Mockito. It now has a method, mockConstruction(), that you can use to mock constructors. Reference: https://rieckpil.de/mock-java-constructors-and-their-object-creation-with-mockito/

Mocking #Log in Groovy

Given a class annotated with #Log; is it possible to mock the injected logger with Mockito for unit testing? Is this correct "Groovy-way" of doing things?
We've been having some issues while using Mockito with Groovy, as it is described here. Perhaps, you might want to use another mocking framework with Groovy support, such as GMock.
For more info you can check also this link.
The way I've achieved this is to define another Logger variable within the class to be tested, non-final and with a more permissive scope. The constructor then defaults to taking the injected logger and assigning it the more permissive object. In the tests, the logger instance can be set to a mockito mocked object which is then assigned to the more permissive object.
Not as perfect as allowing for the mocking of injected objects; but functional.

Is there a Mockito equivalent way to expect constructor invocations like PowerMock.expectNew?

If it doesn't, does it exist on EasyMock?
Thanks.
PowerMock is intended as an extension of both EasyMock and Mockito. From the horse's mouth: "PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities."
In any case, there is no EasyMock equivalent to expectNew and neither is there one in Mockito, either - that's exactly the hole that PowerMock is trying to fill. That being said, PowerMock is perfectly capable of doing this with Mockito. Here is the sample from the documentation:
How to mock construction of new objects
Use PowerMockito.whenNew, e.g.
whenNew(MyClass.class).withNoArguments().thenThrow(new
IOException("error message"));
Note that you must prepare the class
creating the new instance of MyClass for test, not the MyClass itself.
E.g. if the class doing new MyClass() is called X then you'd have to
do #PrepareForTest(X.class) in order for whenNew to work.
How to verify construction of new objects
Use PowerMockito.verifyNew,
e.g.
verifyNew(MyClass.class).withNoArguments();

Grails unit test for domain class insertBefore

How can I test the initBefore method of Groovy Domain-Classes with a unit test in Grails?
I created the dummy object but the beforeInsert-method is not called until myObject.save() is invoked and save is unavailable in the testing environments.
Edit: its a unit-test. there is no error, but the method beforeInsert is not called
beforeInsert is called during unit tests. I can verify this in my tests. A couple things to consider:
ensure you use a beforeInsert method, and not a closure. A closure will not work correctly.
it is called when the object is flushed, so perhaps you are having silent save errors beforehand. In your test when you save the object do .save(flush: true, failOnError: true)
Do you want test if the beforeInsert method is being called or the logic of beforeInsert is correct?
If you want to test if beforeInsert is being called the test class should extend the GrailsUnitTestCase. Do so should give you mocking capabilities and add all the methods like save() and validate(). You can verify if the mocked object called the beforeInsert method or not when you do a save().
If you are testing the logic of beforeInsert then you don't need to mock it. You can create the object and test the logic just like other unit test.
Hope this helps.
Just creat a domain object and save() it. Then check whether or not the "beforeInsert" manipulated your Object.
save() is available in the testing enviroments. Please show your Stacktrace when calling myDomainobject.save()
I had the same exact problem! In GORM (at least until the current version) the save method does not take effect immediately just because it is called! If you want it to take effect right away you need to specify flush:true like this domain.save(flush:true).
it says here http://grails.org/doc/2.2.x/ref/Domain%20Classes/save.html
The save method informs the persistence context that an instance
should be saved or updated. The object will not be persisted
immediately unless the flush argument is used:
To answer your question, beforeInsert is not called until the save is persisted (save takes effect) therefor you should invoke save with flush to test beforeInsert and beforeUpdate methods.
Hope this helps!

Resources