> Query query = EntityManager.createQuery("queryString");
> query.setParameter(key, value);
> Object result = query.getSingleResult();
>
> I want to tell Mockito to return different results depending upon what
> parameters were passed in, but there is only a single Mock object to
> play with. To reiterate, the query string is the same, but the
> parameters differ.
>
> Any ideas?
Could you show more source code? In the given example I'd not care
about query.getSingleResult, I'd rather focus on query.setParameter
and would verify it with different arguments.
Best regards,
Bartosz
If all you want to test is MyQuery method here is a piece of code for this case:
EntityManager entityManager = Mockito.mock(EntityManager.class);
Query query = Mockito.mock(Query.class);
Mockito.when(entityManager.createQuery("select users from
users where users.username=:username and
users.age=:age")).thenReturn(query);
MyQuery(new User("joe", 15));
Mockito.verify(query).setParameter("username", "joe");
Mockito.verify(query).setParameter("age", 15);
As you can see I'm verifying setParameter method only. There is no
need to verify the result of getSingleResult, so the functionality
with Map that you described is not necessary.
Best regards,
Bartosz
>Instead, I will stub out the DAO layer, test the application layer on
>its own.
This what I would do either.
>each test will invoke at most one DAO method and test
>one interaction with the Query object.
This is what I wouldn't do, at least not with mocks :) I would rather
test DAO layer functionally by talking to the database.
Cheers,
Szczepan Faber