I am using mockito as mocking framework. I have a scenerio here, my when(abc.method()).thenReturn(value) does not return value, instead it returns null.
Here is how my class and test looks like.
public class foo(){
public boolean method(String userName) throws Exception {
ClassA request = new ClassA();
request.setAbc(userName);
ClassB response = new ClassB();
try {
response = stub.callingmethod(request);
} catch (Exception e) {
}
boolean returnVal = response.isXXX();
return returnVal;
}
Now follwoing is the test
#Test
public void testmethod() throws Exception{
//arrange
String userName = "UserName";
ClassA request = new ClassA();
ClassB response = new ClassB();
response.setXXX(true);
when(stub.callingmethod(request)).thenReturn(response);
//act
boolean result = fooinstance.lockLogin(userName);
//assert
assertTrue(result);
}
stub is mocked using mockito i.e using #Mock. The test throws NullPointerException in class foo near boolean retrunVal = response.isXXX();
the argument matcher for stub.callingmethod(request).thenReturn(response) is comparing for reference equality. You want a more loose matcher, like this I think:
stub.callingmethod(isA(ClassA.class)).thenReturn(response);
Ensure that your ClassA implements its own equals and that it is correctly implemented.
Related
I'm unit testing my dao. I create a list, I add an object to my list and I tell mockito when my method is called, to return my list with a single object. However, when I look at what's returned from my dao method, it's an empty list. I'm not sure what I'm missing.
#InjectMocks
private Dao dao;
#Mock
private JdbcTemplate jdbcTemp;
#Test
public void testGetData() {
List<MyObj> list = new ArrayList<>();
MyObj myObj = new MyObj();
myObj.setMethod("method val");
list.add(myobj);
Mockito.when(jdbcTemp.query(anyString(), Mockito.any(PreparedStatementSetter.class),
Mockito.any(Dao.MyRowMapper.class))).thenReturn(list);
List<MyObj> res = dao.getData(param1, param2); // this is empty, instead of having a value of 1
Assertions.assertThat(res).isNotNull();
}
My Dao class:
public List<MyObj> getData(String arg1, String arg2) {
List<MyObj> list = new ArrayList<MyObj>();
try {
list.addAll(jdbcTemp.query(query, new PreparedStatementSetter() {
public void setValues(PreparedStatement pstmt) throws SQLException {
pstmt.setString(PARAM_ONE, arg1);
pstmt.setString(PARAM_TWO, arg2);
}
}, new MyRowMapper()));
} catch (Exception exp) {
}
return list;
}
I actually made a mistake in describing the problem.
I had two jdbcTemplates in my dao.
So the way I resolved it is to use a #Qualifier("jdbcTemplate") when creating a mock jdbctemplate
I have function like following to generate an object of provided type from any payload:
public static <T> Optional<T> generateObject(String payloadJson, Class<T> type) {
T objectPayload = null;
try {
objectPayload = objectMapper.readValue(payloadJson, type);
}catch (IOException e) {
log.info(e.getMessage());
}
return Optional.ofNullable(objectPayload);
}
Had written the test case as follows
#Test
void generateObject() throws Exception {
Mockito.when( eventUtil.generateObject("a", Mockito.any(Class.class)) ).thenReturn( Optional.of("") );
Mockito.<Optional<Object>>when( eventUtil.generateObject("b", Mockito.any(Class.class)) ).thenReturn( Optional.of("") );
Optional<Object> o2 = eventUtil.generateObject("b", Mockito.any(Class.class));
assertEquals( "lol", o2.get() );
Mockito.verify(eventUtil);
}
The test execution is failing on Mockito.when statement with
java.lang.IllegalArgumentException: Unrecognized Type: [null]
**Note: I have tried providing null, another model class and even the parent class itself with thenReturn( Optional.of()) with no luck. Please point me to the correct direction in this case.
As explained Why doesn't Mockito mock static methods?
you cannot mock static method by using mockito.
Anyway I guess your goal is to check if this utility method behave as you expect in case the objectMapper throws exception or just works fine. So you can make the method non static, then you can inject the objectMapper and try something like:
#Test
void objectMapperThrowsException() throws Exception {
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
EventUtil eventUtil = new EventUtil(mapper);
Mockito.when(mapper.readValue(any(),any())).thenThrow(IOException.class);
Optional<Object> result = eventUtil.generateObject("b", Object.class);
assertTrue(result.isEmpty());
}
and the positive case
#Test
void objectMapperReturnAValidObject() throws Exception {
Object anObject = new Object();
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
EventUtil eventUtil = new EventUtil(mapper);
Mockito.when(mapper.readValue(any(),any())).thenReturn(anObject);
Optional<Object> result = eventUtil.generateObject("b", Object.class);
assertFalse(result.isEmpty());
assertEquals(anObject, result.get());
}
The best method to test this method is to convert any data type to JSON and call the method to convert to the original one, try to use a helper class or any other data type
public class TargetTest {
#Test
void generateObject() throws Exception {
// Arrange
ObjectMapper mapper = new ObjectMapper();
Helper helper = new Helper();
helper.setName("test");
String json = mapper.writeValueAsString(helper );
// Act
Helper actual = EventUtil.generateObject(json, Helper.class);
// Assert
}
class Helper {
private String name;
// Getter and setter
}
}
, If you want to mock it, try to use PowerMockito
I have a test using jmockit's result = new Object[] {...} that is failing. The jmockit version is 1.34. The test should throw an exception but instead jmockit is returning a collection with the exception in it. Here is an example:
public class ServiceTest {
public class Service {
private Set<String> saved;
public Service() {
saved = new HashSet<>();
saved.add("one");
saved.add("two");
}
public Set<String> readAll() {
return Collections.unmodifiableSet(saved);
}
}
#Test(expected = RuntimeException.class)
public void testReadAll(#Mocked Service service) {
new Expectations() {{
service.readAll(); times = 1; result = new RuntimeException();
}};
service.readAll();
}
#Test
public void testReadAllWithArray(#Mocked Service service) {
new Expectations() {{
service.readAll(); times = 1; result = new Object[]{new RuntimeException()};
}};
Set set = service.readAll();
assertThat(set.iterator().next(), instanceOf(RuntimeException.class));
}
}
testReadAllWithArray shows that the object returned by readAll is a set with the exception in it.
Is this a bug or is there any workaround?
Upgrade from jmockit 1.34 to the latest version.
I have a class that I wish to test using Mockito. The problem I am facing is that when the Mock is called it always returns a null value.
I can see when I debug that Mockito see the mock but always returns null. My understanding is that in the CUT that when the client.sendMessage is called it will return the value for Transaction that I have created in the #Test.
Any suggestions on what I am doing wrong?
The class under test:
public String SendMessage(String ID, String body) {
String receipt = null;
Map<String, Integer> Transaction = null;
try {
Transaction = client.sendMessage(ID, body);
} catch(Exception e) {
receipt = "FAIL";
return receipt;
}
receipt = Transaction.get("receipt").toString();
return receipt;
}
My test method is:
#Mock
private MessageSenderClient client;
#InjectMocks
MessageSender ms;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
#Test
public void testPushNotificationProcessorTransactionID() {
String Id = "1234";
String body = "Test Message";
Map<String, Integer> Transaction = new HashMap<String, Integer>();
Transaction.put("Id", 123456);
client = Mockito.mock(PushNotificationClient.class);
Mockito.doReturn(Transaction).when(client).sendMessage(Matchers.anyString(), Matchers.anyString());
String transactionID = ms.SendPushMessage(Id, body);
assertEquals(transactionID, "1");
}
In Mockito, is it possible to define the behavior of a mock object in the event that it is type cast, perhaps as one can use Mockito's "when ... thenReturn" functionality to define the behavior of a mock object in the event that of its methods is called?
For example, in the following example class and test...
public class MyClass {
public String myMethod(ObjectString arg) {
ans = (String) arg;
return ans;
}
}
public class MyClassTest {
#Test
public void myMethod_should_convert_to_string() {
MyClass testMyClass = new MyClass();
ObjectString mockObjectString = Mockito.mock(ObjectString.class);
String expected = "expected string returned";
Mockito.when(mockObjectString.IS_CAST_TO_STRING).thenReturn(expected);
String actual = testMyClass.myMethod(mockObjectString);
Assert.assertEquals(expected, actual);
}
}
...is there something I can perhaps replace 'IS_CAST_TO_STRING' with that will cause mockObjectString to be cast to the specific value "expected string returned"?
An instance of ObjectString can never be cast to String. String does not inherit from any class called ObjectString nor does it implement any interface called ObjectString. Casting to String will always throw a ClassCastException unless arg is null.
On the other hand, if your class under test looked like this:
public class MyClass {
public String myMethod(final Object arg) {
final String ans = (String) arg;
return ans;
}
}
Then, you could achieve what you're looking for without Mockito:
#Test
public void myMethod_should_convert_to_string() {
MyClass testMyClass = new MyClass();
String expected = "expected string returned";
String actual = testMyClass.myMethod(expected);
assertEquals(expected, actual);
}