I'm working on a Java project want to write a unit test for an .equals method I have in a DTO. In the .equals method, there is a .getClass() method called by both objects under test. I want to mock this, but I can't tell what type of object it wants. I tried,
when(mockRoomInv.getClass()).thenReturn(RoomInv.class);
but sure as heck didn't do anything. What is the return type of getClass, and how do I manipulate it?
As Object.getClass() is final, you cannot mock that method with Mockito. I would strongly advice you to refactor your code to inject the class in another way. If that's not possible, you could try out powermock, where you could mock any final method. Object.getClass() is a bit special, so be sure to set MockGateway.MOCK_GET_CLASS_METHOD = true in powermock.
Object.getClass() is a final method, so you cannot mock it with Mockito.
You can mock static and final methods (as this one) and even private methods with Powermock (it's a quite cool tool ;) available at https://github.com/powermock/powermock.
You can use it with Mockito as explained in the Mockito wiki article. There you will find some useful examples.
Related
I've created a simple test class with two fields like
#Mock
private MyTestClass myTestClass;
#Spy
private final MyContext context = CommonTestData.getDefaultContext();
Basically I don't really need the spy functionality here, it is just used to automatically inject the object into other mocks.
For the test, I tried to configure myTestClass like this:
when(myTestClass.someMethod(eq(context))).thenReturn(someValue);
The problem now is, that Matchers.eq is not matching "un-enhanced" versions of MyContext. So when someMethod is called during the test with a "regular" instance of MyContext that actually equals the value used for context, the stubbed method is not called.
It seems like the Mockito enhanced MyContext class implements its own equals method, at least the equals method of MyContext never seems to be called. Thus, I currently can't think of any way to modify the actual comparison being done.
I can think of various workarounds for this issue like using a custom argument matcher or stubbing the method with an instance of the "real" object. I was however wondering: Is there any Mockito-provided solution to check for equality of enhanced classes against their regular counterparts?
This is conceptually wrong: the idea of equals() in Java is to be symmetric: when a.equals(b) then you better find that b.equals(a), too!
And in your case, a has class MyContext, and b has WhateverMockitoSpyDoesToMyContext. So even when the equals() of the mockito generated thing works, most likely the other way round, that "base" equals might return false (because the original MyContext class knows nothing about potential subclasses like what Mockito is doing here).
I agree that it might be convenient to have your example work out, but I am simply not aware of a "correct" way getting there. From that point of view, you actually have to look into use an ArgumentMatcher.
Beyond that: seriously consider if you really gain anything by using eq() in the first place. If that check is the "core" of your test, then sure, somehow you better look for clear ways to have that check. But if it is more of a byproduct: then just use any() instead.
Meaning: don't make your tests more complicated than necessary. Normally, you setup things for one specific case anyway. You only have to worry about things passed to someMethod() if your code under test could actually, correctly pass different objects to that method.
I'm reading some test code that calls mockStatic(MyClass.class), but MyClass is neither static nor does it contain static methods.
Are there other benefits of using mockStatic()?
Not sure if related, but PowerMock is also used in the test code.
I cannot comment so I just reply here..
According to its latest documentation, Mockito doesnt have mockStatic(), so I think this comes from PowerMock. You can go in to the method declaration to see at least the method comes from which module.
For Powermock, the class inside mockStatic() doesnt have to be static class. The goal of mockStatic is to mock the static method. See here: https://code.google.com/p/powermock/wiki/MockStatic
I would like to write some state based tests using JMockit to mock up CSVRecord. The problem is that CSVRecord is final (which means I have to use a mocking framework like JMockit) and CSVRecord's constructor has package private visibility.
Since it is package private, I can't call new CSVRecord(arg, arg, ...), which means I can never I instantiate my mock.
Its parent, CSVParser, is the only class that can create an instance.
Does JMockit have a way to deal with this scenario?
note: JMockit or Mockito are the only frameworks we use on this project. No other framework will be acceptable. My preference is to use a MockUp.
It sounds more like CSVRecord isn't a good candidate for mocking. If possible, tests targetting another public class which uses it internally would be preferable.
Otherwise, JMockit provides a Deencapsulation class with newInstance methods.
As a side note, Mockito only supports behavior-based tests; JMockit provides "mock-ups" (MockUp), but it's not the same as writing a pure state-based black box test.
If it is just a matter of calling the private constructor, then yes, JMockit has tools to deal with that. See the tutorial
Use:
ConstructorReflection.newInstance
in newer versions of Jmockit.
I want to mock a class using the Mockito framework, that has a couple of constructor arguments.
How would i go about passing those constructor arguments without generating setters for the private member variables ?
Thanks
You said you want to mock some but not all of the methods. I'm not sure why you'd want to do this - if your class is a collaborator, then it would make sense to mock the whole class. Or if it's the SUT, you probably don't want to mock it at all.
It's possible that what you're looking for is a spy, rather than a mock. If you decide to use a spy, you'll make it from a real object, which has already been constructed using whatever arguments you need it to be constructed from.
But before you consider using a spy, I urge you to think more carefully about exactly what it is that you're testing, and why you think you need to replace some of your methods with mock implementations, but not others.
I'm using easymock, and I am mocking my UserService class.
My UserService has a few methods:
boolean canUserLogin(..);
boolean canUserJoinClass(...);
Now some of the methods call each other, and if I am testing method#1 I want to stub/mock methods #2 and methods# 3 that are called in method#1.
What I am confused is, how can I mock parts of a class and leave others to run the actual code?
So I want to actually test UserService.method#1, but mock UserService.method#2 and UserService.method#3 that method#1 calls internally.
By specifying return values for the methods you want mocked; see the easymock docs for examples.
The "Specifying Return Values" section discusses creating return values for mocked methods.
The "Partial mocking" section (towards the bottom) discusses mocking actual classes.
I agree with the docs (and other answers) that this may be an indication of sketchy design. Without further details, it's hard to say how sketchy it is, if it is at all.
You can check some library like Easymock, but I don't sure whether it can do this.
And here is my solution without third-party library. Create a subclass of UserService, and override the method you want to mock.
class SubUserService{
#override
boolean canUserJoinClass(...){
return false;
}
}
But notice the mock method can't be private.
And if this is one real problem you meet, you should refactor you methods to different classes.
I know Mockito supports "spy" on real objects. I could not find an equivalent in Easy Mock. So, I am not sure if you can do this.
Having said that, this is a smell to me. Why do you need to mock it? Is that an indication of the fact that your object is doing too much and hence you need to mock the other interactions?
Also, whenever you need to worry about the implementation of a method (method 1 in this case) i.e. the fact that it calls method2 and method3, especially of the same class, that sounds to me like a encapsulation leaking.
Mocking is intended to be used for dependencies, so you can test in isolation. In this case, you don't have any dependencies, since the methods you are calling are on one class. So I wouldn't use mocking here.
If methods 2 and 3 are so complicated that you want to mock them when testing method 1, then perhaps you should separate them out into their own class(es), so you can easily mock them.