How to mock a void static method to throw exception with Powermock?

Answer is as below. After consulting here http://code.google.com/p/powermock/issues/detail?id=278 , in fact Adder.add(12) above is part of setting up mock static method. It means when invoking Adder.add() with argument 12, IOException will be thrown. It is hard to understand, right? 🙂 So it should be written as below. PowerMockito.mockStatic(Adder.class); PowerMockito.doThrow(new IOException()).when(Adder.class); Adder.add(anyInt()); EDIT: Link is dead, … Read more

Mockito: Difference between doThrow() and thenThrow()

Almost nothing: in simple cases they behave exactly the same. The when syntax reads more like a grammatical sentence in English. Why “almost”? Note that the when style actually contains a call to authenticationService.login. That’s the first expression evaluated in that line, so whatever behavior you have stubbed will happen during the call to when. … Read more

Mockito – Injecting a List of mocks

Annotate it with @Spy instead of @Mock. As Mockito cannot spy on an interface, use a concrete implementation, for example ArrayList. During test setup add the mocks to the List spy. This way you do not need to alter your test subject solely for test purposes. @InjectMocks private Wrapper testedObject = new Wrapper(); @Spy private … Read more

How do I enable Mockito debug messages?

Mockito 1.9.0 introduced listeners and now bundles a verbose logger: So basically if you want simple and stupid logs, just do the following: List mockWithLogger = mock(List.class, withSettings().verboseLogging()); See http://docs.mockito.googlecode.com/hg/latest/org/mockito/MockSettings.html#verboseLogging() for more information Cheers,

Mockito.mockedStatic for method with arguments

Edit – Mockito 3.7.7 Mockito 3.7.7 unified order of verify parameters (Issue #2173) Updated code: try (MockedStatic<Foo> dummyStatic = Mockito.mockStatic(Foo.class)) { dummyStatic.when(() -> Foo.method(“param1”)) .thenReturn(“someValue”); // when System.out.println(Foo.method(“param1”)); //then dummyStatic.verify( () -> Foo.method(“param1”), times(1), ); } Original answer It is possible, you need to use a lambda instead of a method reference: try (MockedStatic<Foo> dummyStatic … Read more

Mocking a method which returns Page interface

You can use a Mock reponse or an actual response and then use when, e.g.: Page<Company> companies = Mockito.mock(Page.class); Mockito.when(companyRepository.findAllByIsActiveTrue(pageable)).thenReturn(companies); Or, just instantiate the class: List<Company> companies = new ArrayList<>(); Page<Company> pagedResponse = new PageImpl(companies); Mockito.when(companyRepository.findAllByIsActiveTrue(pagedResponse)).thenReturn(pagedResponse);

How can I use OCMock to verify that a method is never called?

Since r69 of OCMock, it’s possible to reject a method call http://svn.mulle-kybernetik.com/OCMock/trunk/Source/Changes.txt Nice mocks / failing fast When a method is called on a mock object that has not been set up with either expect or stub the mock object will raise an exception. This fail-fast mode can be turned off by creating a “nice” … Read more

Create a JsonProcessingException

how about you create an anonymous exception of type JsonProcessingException when(mapper.writeValueAsString(any(Object.class))).thenThrow(new JsonProcessingException(“Error”){}); The {} braces does the trick. This is much better since it is not confusing to the reader of the test code.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)