mockito

java.lang.VerifyError with Mockito 1.10.17

瘦欲@ 提交于 2020-01-24 04:59:26
问题 I am trying to replace JMock with Mockito (1.10.17). I have already done some unit tests successfully, but now I want to use the timeout feature verify(publisher, timeout(5000)).notifySubscribers(any(BecameMasterMessage.class)); and I get this exception: java.lang.VerifyError: (class: org/mockito/internal/verification/VerificationOverTimeImpl, method: verify signature: (Lorg/mockito/internal/verification/api/VerificationData;)V) Incompatible argument to function at org.mockito.verification

Mock any method signature with Mockito

爱⌒轻易说出口 提交于 2020-01-24 03:56:05
问题 Hi I have this piece of code that is not very well designed, but I'm not the owner of this code so I can't change it. public interface Car{ // This is a marker interface. } public class BigCar implements Car{ public boolean isVeryBig(){ return true;} } public class QuiteBigCar implements Car{ public boolean isVeryBig(boolean withHatchBack){ return true;} } public Pickup implements Car{ public boolean isVeryBig(boolean withHatchBack, boolean withRoofRack){ return true;} } You see the interface

How to simulate throwing an exception only once in retry with JUNIT/Mockito test?

[亡魂溺海] 提交于 2020-01-23 07:37:08
问题 I put a simple retry because the operation can rarely fail. The simplified code is below. The method putObject can accidentally throw an exception, in this case the retry should allow to invoke this method again. Is it possible to write a JUnit test for this? I know that with Mockito library we can force to throw an Exception invoking a method but how to force this exception to be thrown only once? public class RetryExample { Bucket bucket = new Bucket(); static int INTERNAL_EXCEPTION_CODE =

Spring Controller testing with MockMvc post method

南楼画角 提交于 2020-01-23 06:23:04
问题 I am trying to test a method of my controller in a Spring Boot application. This is a post endpoint, which gets an id in a request and it passes on this id to a service: @Slf4j @Controller public class AdministrationController { private final AdministrationService administrationService; @Autowired public AdministrationController(AdministrationService administrationService) { this.administrationService = administrationService; } @PostMapping("/administration") public @ResponseBody

Realm Unit Testing

若如初见. 提交于 2020-01-22 20:03:07
问题 I am trying to unit test Realm and its interactions but things are not going too well. I have included all dependencies and keep getting vague failures, below is my code for the Helper class which is a wrapper over Realm . Questions Is this the correct way of testing Realm? How can I test data that is in the app's sandbox, can that data only be tested by UI/Instrumentation tests? I am getting an error currently (below) and before I was getting a "Powermock zero args constructor doesn't exist"

mockito - mocking an interface - throwing NullPointerException

ⅰ亾dé卋堺 提交于 2020-01-22 18:49:06
问题 I am getting null pointer exception after mocking also. Please find my project structure. //this is the pet interface public interface Pet{ } // An implementation of Pet public class Dog extends Pet{ int id, int petName; } // This is the Service Interface public interface PetService { List<Pet> listPets(); } // a client code using the PetService to list Pets public class App { PetService petService; public void listPets() { // TODO Auto-generated method stub List<Pet> listPets = petService

mockito - mocking an interface - throwing NullPointerException

若如初见. 提交于 2020-01-22 18:49:04
问题 I am getting null pointer exception after mocking also. Please find my project structure. //this is the pet interface public interface Pet{ } // An implementation of Pet public class Dog extends Pet{ int id, int petName; } // This is the Service Interface public interface PetService { List<Pet> listPets(); } // a client code using the PetService to list Pets public class App { PetService petService; public void listPets() { // TODO Auto-generated method stub List<Pet> listPets = petService

How can I test a method which invoke protected (unwanted) methods of parent class?

旧时模样 提交于 2020-01-22 15:40:14
问题 I'm stuck in a very weird case. I have some specific code which I need to test. Here it is: public class A { /* * The real method of real class is so big that I just don't want to test it. * That's why I use throwing an exception. */ protected void method(Integer result) { throw new RuntimeException("Oops!"); } protected <T> T generifiedMethod(String s, T type) { throw new RuntimeException("Oops!"); } protected void mainMethod(Integer value) { throw new RuntimeException("Oops!"); } } I also

How can I mock methods of @InjectMocks class?

断了今生、忘了曾经 提交于 2020-01-22 04:25:06
问题 For example I have handler: @Component public class MyHandler { @AutoWired private MyDependency myDependency; public int someMethod() { ... return anotherMethod(); } public int anotherMethod() {...} } to testing it I want to write something like this: @RunWith(MockitoJUnitRunner.class} class MyHandlerTest { @InjectMocks private MyHandler myHandler; @Mock private MyDependency myDependency; @Test public void testSomeMethod() { when(myHandler.anotherMethod()).thenReturn(1); assertEquals

What is the difference between mocking and spying when using Mockito?

旧时模样 提交于 2020-01-19 01:58:50
问题 What would be a use case for a use of a Mockito spy? It seems to me that every spy use case can be handled with a mock, using callRealMethod. One difference I can see is if you want most method calls to be real, it saves some lines of code to use a mock vs. a spy. Is that it or am I missing the bigger picture? 回答1: The answer is in the documentation: Real partial mocks (Since 1.8.0) Finally, after many internal debates & discussions on the mailing list, partial mock support was added to