mockito

Mockito.when().thenReturn() doesn't work or returns null

混江龙づ霸主 提交于 2020-01-03 16:57:28
问题 During the test there is a NullPointerException thrown. I tried to debug it and the only thing I worked out was that eventOptional is always null. Just as if Mockito.when().thenReturn() didn't work. Can anybody help? Here's my code for a tested service and for the test itself: @Service public class EventService { @Autowired public EventService(EventRepository eventRepository) { this.eventRepository = eventRepository; } //... public void updateEvent(EventDTO eventDTO) { Optional<Event>

Mockito.when().thenReturn() doesn't work or returns null

断了今生、忘了曾经 提交于 2020-01-03 16:56:08
问题 During the test there is a NullPointerException thrown. I tried to debug it and the only thing I worked out was that eventOptional is always null. Just as if Mockito.when().thenReturn() didn't work. Can anybody help? Here's my code for a tested service and for the test itself: @Service public class EventService { @Autowired public EventService(EventRepository eventRepository) { this.eventRepository = eventRepository; } //... public void updateEvent(EventDTO eventDTO) { Optional<Event>

Checking consistency of multiple arguments using Mockito

对着背影说爱祢 提交于 2020-01-03 07:18:26
问题 I'm using Mockito to mock a class that has a method that looks something like this: setFoo(int offset, float[] floats) I want to be able verify that the values in the array ( floats ) are equal (within a given tolerance) to values in an array of expected values. The catch is that I want to check the contents of floats starting at the position specified by offset . For the purposes of the test I don't know/care what the offset is as long as it points at the values I'm expecting. I also don't

How to mock a call of an inner method from a Junit

自闭症网瘾萝莉.ら 提交于 2020-01-02 09:27:38
问题 I have MyClass and I am doing a test-class for every method (Method1Test) public class MyClass { public int method1(){ int a = method2(); return a; } public int method2(){ return 0; } } @RunWith(MockitoJUnitRunner.class) public class Method1Test { @InjectMocks private MyClass myClass = new MyClass(); @Before public void setup(){} @Test public void test01(){ Mockito.when(myClass.method2()).thenReturn(25); int a = myClass.method1(); assertTrue("We did it!!!",a==25); } } The problem is that I am

Mockito - stub abstract parent class method

左心房为你撑大大i 提交于 2020-01-02 08:51:13
问题 I am seeing very strange behavior trying to stub a method myMethod(param) of class MyClass that is defined in an abstract parent class MyAbstractBaseClass . When I try to stub (using doReturn("...").when(MyClassMock).myMethod(...) etc.) this method fails, different exceptions are thrown under different scenarios. The exceptions are thrown right on that line. When I use doReturn("...").when(MyClassMock).myMethod(CONCRETE PARAM CLASS OBJECT) , I get the following exception: org.mockito

How to do UI testing for kotlin file with dagger in android?

ぃ、小莉子 提交于 2020-01-02 07:23:14
问题 Below is my stack trace, I have gone through all the questions and answers on SO but can't find any solution java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null) at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74) at java.lang.reflect.Proxy.invoke(Proxy.java:1006) at $Proxy6.isTypeMockable(Unknown Source) at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29) at org

apkbuilder finds duplicate file when adding powermock to an android test project

匆匆过客 提交于 2020-01-02 04:40:11
问题 I'm attempting to add powermock-mockito and mockito into an Android Test project. I created the android test project using the android command-line tool to create the build.xml and project structure. I have the following jars in my libs/ directory: dexmaker-1.0.jar dexmaker-mockito-1.0.jar mockito-all-1.9.5.jar powermock-mockito-1.5-full.jar When I attempt to build the project with ant debug, I get the following error: [apkbuilder] Creating ProjectTests-debug-unaligned.apk and signing it with

Mocking Clojure protocols

落花浮王杯 提交于 2020-01-02 04:39:19
问题 Can one of the popular Java mocking frameworks like EasyMock or Mockito be used to mock Clojure protocols defined with defprotocol ? If so, how? 回答1: You should be able to mock protocols using any mock library. Under the covers, every protocol uses a Java interface as an implementation detail, and you could just mock that interface. That said, don't do this! Mocking in Java is absurdly complex because of reflection, protection levels, final classes, etc. Any time you want a Clojure object

Mocking RestTemplateBuilder and RestTemplate in Spring integration test

假如想象 提交于 2020-01-02 02:34:49
问题 I have a REST resource that gets a RestTemplateBuilder injected to build a RestTemplate : public MyClass(final RestTemplateBuilder restTemplateBuilder) { this.restTemplate = restTemplateBuilder.build(); } I would like to test that class. I need to mock the calls the RestTemplate makes to another service: request = restTemplate.getForEntity(uri, String.class); I tried this in my IT: @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public

How to mock a private dao variable?

你。 提交于 2020-01-02 01:43:05
问题 I have a dao.create() call that I want to mock when testing a method. But I am missing something as I'm still getting NPE. What is wrong here? class MyService { @Inject private Dao dao; public void myMethod() { //.. dao.create(object); // } } How can I mock out the dao.create() call? @RunWith(PowerMockRunner.class) @PrepareForTest(DAO.class) public void MyServiceTest { @Test public void testMyMethod() { PowerMockito.mock(DAO.class); MyService service = new MyService(); service.myMethod(); /