How to tell a Mockito mock object to return something different the next time it is called?

So, I’m creating a mock object as a static variable on the class level like so… In one test, I want Foo.someMethod() to return a certain value, while in another test, I want it to return a different value. The problem I’m having is that it seems I need to rebuild the mocks to get … Read more

throw checked Exceptions from mocks with Mockito

I’m trying to have one of my mocked objects throw a checked Exception when a particular method is called. I’m trying the following. @Test(expectedExceptions = SomeException.class) public void throwCheckedException() { List<String> list = mock(List.class); when(list.get(0)).thenThrow(new SomeException()); String test = list.get(0); } public class SomeException extends Exception { } However, that produces the following error. org.testng.TestException: … Read more

Python mock multiple return values

I am using pythons mock.patch and would like to change the return value for each call. Here is the caveat: the function being patched has no inputs, so I can not change the return value based on the input. Here is my code for reference. def get_boolean_response(): response = io.prompt(‘y/n’).lower() while response not in (‘y’, … Read more

How can I mock requests and the response?

I am trying to use Pythons mock package to mock Pythons requests module. What are the basic calls to get me working in below scenario? In my views.py, I have a function that makes variety of requests.get() calls with different response each time def myview(request): res1 = requests.get(‘aurl’) res2 = request.get(‘burl’) res3 = request.get(‘curl’) In … Read more

How to check String in response body with mockMvc

I have simple integration test @Test public void shouldReturnErrorMessageToAdminWhenCreatingUserWithUsedUserName() throws Exception { mockMvc.perform(post(“/api/users”).header(“Authorization”, base64ForTestUser).contentType(MediaType.APPLICATION_JSON) .content(“{\”userName\”:\”testUserDetails\”,\”firstName\”:\”xxx\”,\”lastName\”:\”xxx\”,\”password\”:\”xxx\”}”)) .andDo(print()) .andExpect(status().isBadRequest()) .andExpect(?); } In last line I want to compare string received in response body to expected string And in response I get: MockHttpServletResponse: Status = 400 Error message = null Headers = {Content-Type=[application/json]} Content type = application/json Body … Read more

Can Mockito stub a method without regard to the argument?

I’m trying to test some legacy code, using Mockito. I want to stub a FooDao that is used in production as follows: foo = fooDao.getBar(new Bazoo()); I can write: when(fooDao.getBar(new Bazoo())).thenReturn(myFoo); But the obvious problem is that getBar() is never called with the same Bazoo object that I stubbed the method for. (Curse that new … Read more

What’s the best mock framework for Java? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for … Read more

What’s the best strategy for unit-testing database-driven applications?

I work with a lot of web applications that are driven by databases of varying complexity on the backend. Typically, there’s an ORM layer separate from the business and presentation logic. This makes unit-testing the business logic fairly straightforward; things can be implemented in discrete modules and any data needed for the test can be … Read more