mockito

How do I return different values on different calls to a mock?

醉酒当歌 提交于 2020-01-12 17:28:33
问题 I have the following code which is getting the current counter value from DB. Then it updates the counter in DB and then again it retrieves the value. int current = DBUtil.getCurrentCount(); DBUtil.updateCount(50);// it updates the current count by adding 50 int latest = DBUtil.getCurrentCount(); I want to mock the static methods in such a way that the first call should return 100 and the second call should return 150. How can I use PowerMockito to achieve this? I am using TestNG, Mockito

How do I return different values on different calls to a mock?

我的梦境 提交于 2020-01-12 17:28:30
问题 I have the following code which is getting the current counter value from DB. Then it updates the counter in DB and then again it retrieves the value. int current = DBUtil.getCurrentCount(); DBUtil.updateCount(50);// it updates the current count by adding 50 int latest = DBUtil.getCurrentCount(); I want to mock the static methods in such a way that the first call should return 100 and the second call should return 150. How can I use PowerMockito to achieve this? I am using TestNG, Mockito

Mockito isA() & any…()

早过忘川 提交于 2020-01-12 11:53:51
问题 What is the difference between: verify(mock, times(1)).myMethod(Matchers.isA(String.class)); verify(mock, times(1)).myMethod(Matchers.anyString()); from the Mockito library? Both pass for my method and I'm wondering which one is "better" to use. 回答1: isA checks that the class matches the expected class. In Mockito 1.x, any , anyObject , and anyString ignore the argument entirely including its type, even though any can take a class parameter and anyString specifies it in the name. Typically,

Testing code which calls native methods

痴心易碎 提交于 2020-01-12 07:10:11
问题 I have a class like this: public final class Foo { public native int getBar(); public String toString() { return "Bar: " + getBar(); } } Please note that getBar() is implemented with JNI and that the class is final . I want to write a junit test to test the toString() method. For this I need to mock the getBar() method and then run the original toString() method to check the output. My first thought was that this must be impossible but then I found PowerMock which supports testing final

Mockito: How to easily stub a method without mocking all parameters

不问归期 提交于 2020-01-12 06:26:46
问题 I have a method i'd like to stub but it has a lot of parameters. How can i avoid mocking all parameters but still stub the method. Ex: //Method to stub public void myMethod(Bar bar, Foo foo, FooBar fooBar, BarFoo barFoo, .....endless list of parameters..); 回答1: I don't quite follow what problem you're having using Mockito. Assuming you create a mock of the interface that contains your myMethod() method, you can then verify only the parameters to the method that you are interested in. For

Mockito: How to easily stub a method without mocking all parameters

橙三吉。 提交于 2020-01-12 06:26:12
问题 I have a method i'd like to stub but it has a lot of parameters. How can i avoid mocking all parameters but still stub the method. Ex: //Method to stub public void myMethod(Bar bar, Foo foo, FooBar fooBar, BarFoo barFoo, .....endless list of parameters..); 回答1: I don't quite follow what problem you're having using Mockito. Assuming you create a mock of the interface that contains your myMethod() method, you can then verify only the parameters to the method that you are interested in. For

How to enforce MockitoJUnitRunner fail without basic http authentication?

风流意气都作罢 提交于 2020-01-11 11:23:09
问题 I write a Spring Boot app and I was able to access and test Controller with MockMvc . The issue is that during testing security is not enforced and I can access Controller with no user. Am I doing anything wrong? Is it intended behavior? ControllerTest class: @RunWith(MockitoJUnitRunner.class) public class ControllerTest { private MockMvc mockMvc; @Mock private Service service; @InjectMocks private Controller controller; private final static String URL = "/test"; @Before public void setUp()

How to enforce MockitoJUnitRunner fail without basic http authentication?

风流意气都作罢 提交于 2020-01-11 11:22:03
问题 I write a Spring Boot app and I was able to access and test Controller with MockMvc . The issue is that during testing security is not enforced and I can access Controller with no user. Am I doing anything wrong? Is it intended behavior? ControllerTest class: @RunWith(MockitoJUnitRunner.class) public class ControllerTest { private MockMvc mockMvc; @Mock private Service service; @InjectMocks private Controller controller; private final static String URL = "/test"; @Before public void setUp()

PowerMockito: got InvalidUseOfMatchersException when use matchers mocking static method

末鹿安然 提交于 2020-01-11 08:48:05
问题 When I'm testing this static method public class SomeClass { public static long someMethod(Map map, String string, Long l, Log log) { ... } } with import org.apache.commons.logging.Log; @RunWith(PowerMockRunner.class) //@PrepareForTest(SomeClass.class) public class Tests { @Test public void test() { ... PowerMockito.mockStatic(SomeClass.class); Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), isA(Log.class))).thenReturn(1L); ... } } I got InvalidUseOfMatchersException . My

How to inject a mock object to a class when testing?

*爱你&永不变心* 提交于 2020-01-11 06:46:43
问题 My user class is as follows, public class UserResource { @Inject UserService userService; public boolean createUser(User user) { DbResponse res = userService.addUser(user); if(res.isSuccess){ return true; }else{ return false; } } } My test class looks as follows, public class UserResourceTest { UserResource userResource; @BeforeMethod void beforeMethod() { userResource = new UserResource(); } @Test public void test() { User user= mock(User.class); boolean res= userResource.createUser(user);