mockito

Mockito and interface event

白昼怎懂夜的黑 提交于 2020-06-27 01:05:09
问题 I am writing an integration test using mockito. The unit under test is connected to a mocked object (objA) through an interface. The functionality that I am trying to mimic happens when the mocked objected fires an event and the unit under test is listening to it. The interface: public interface MyInterfaceAPI{ void fireyMyEvent(String msg); } The unit under test: public class UnitUnderTest{ ObjA objA; public UnitUnderTest(ObjA objA_t) { objA = objA_t; objA.addMyListener(new addMyHandler());

How to mock and verify Lambda expression in Kotlin?

懵懂的女人 提交于 2020-06-25 05:09:25
问题 In Kotlin (and Java 8) we can use Lambda expression to remove boilerplate callback interface. For example, data class Profile(val name: String) interface ProfileCallback { fun onSuccess(profile: Profile) } class ProfileRepository(val callback: ProfileCallback) { fun getProfile() { // do calculation callback.onSuccess(Profile("name")) } } We can change remove ProfileCallback and change it into Kotlin's Lambda: class ProfileRepository(val callback: (Profile) -> Unit) { fun getProfile() { // do

How to mock ObjectMapper.readValue() using mockito

非 Y 不嫁゛ 提交于 2020-06-24 22:48:37
问题 I'm testing a service layer and not sure how to mock ObjectMapper().readValue in that class. I'm fairly new to mockito and could figure out how to do it. The following is my code, service.java private configDetail fetchConfigDetail(String configId) throws IOException { final String response = restTemplate.getForObject(config.getUrl(), String.class); return new ObjectMapper().readValue(response, ConfigDetail.class); } ServiceTest.java @Test public void testgetConfigDetailReturnsNull() throws

How to mock ObjectMapper.readValue() using mockito

旧城冷巷雨未停 提交于 2020-06-24 22:48:27
问题 I'm testing a service layer and not sure how to mock ObjectMapper().readValue in that class. I'm fairly new to mockito and could figure out how to do it. The following is my code, service.java private configDetail fetchConfigDetail(String configId) throws IOException { final String response = restTemplate.getForObject(config.getUrl(), String.class); return new ObjectMapper().readValue(response, ConfigDetail.class); } ServiceTest.java @Test public void testgetConfigDetailReturnsNull() throws

Mocking a Keycloak token for testing a Spring controller

穿精又带淫゛_ 提交于 2020-06-24 11:53:05
问题 I want to write unit tests for my spring controller. I'm using keycloak's openid flow to secure my endpoints. In my tests I'm using the @WithMockUser annotation to mock an authenticated user. My problem is that I'm reading the userId from the token of the principal. My unit test now fails because the userId I read from the token is null; if (principal instanceof KeycloakAuthenticationToken) { KeycloakAuthenticationToken authenticationToken = (KeycloakAuthenticationToken) principal;

Mocking a Spy method with Mockito

喜你入骨 提交于 2020-06-24 11:40:06
问题 I am writing a unit test for a FizzConfigurator class that looks like: public class FizzConfigurator { public void doFoo(String msg) { doWidget(msg, Config.ALWAYS); } public void doBar(String msg) { doWidget(msg, Config.NEVER); } public void doBuzz(String msg) { doWidget(msg, Config.SOMETIMES); } public void doWidget(String msg, Config cfg) { // Does a bunch of stuff and hits a database. } } I'd like to write a simple unit test that stubs the doWidget(String,Config) method (so that it doesn't

Create a mocked list by mockito

坚强是说给别人听的谎言 提交于 2020-06-24 04:15:39
问题 I want to create a mocked list to test below code: for (String history : list) { //code here } Here is my implementation: public static List<String> createList(List<String> mockedList) { List<String> list = mock(List.class); Iterator<String> iterHistory = mock(Iterator.class); OngoingStubbing<Boolean> osBoolean = when(iterHistory.hasNext()); OngoingStubbing<String> osHistory = when(iterHistory.next()); for (String history : mockedList) { osBoolean = osBoolean.thenReturn(true); osHistory =

Junit Mockito for global java.util.Map

为君一笑 提交于 2020-06-17 09:40:29
问题 I am trying to Test a method but it has a global variable which is null, Please guide me so I can assign value to global variable i.e. a Map My Junit: public class ErrorTest { @Mock private DataSource db; @Mock private JdbcTemplate jdbcTemplate; @InjectMocks private RateServiceImpl rateService = new RateServiceImpl(); @Mock private RaterDao raterDao; @Resource private MessageSource msg ; @Mock Map<String, StringAttribute> errorMap = new HashMap<String, StringAttribute>(); @Before public void

Mock android.os.BaseBundle without roboelectric

自古美人都是妖i 提交于 2020-06-17 04:34:35
问题 I am trying to do a unit test on this code : Bundle cidParam(String accountId) { Bundle params = new Bundle(1); params.putString(Params.CID, accountId); return params; } This is the unit test : private void mockBundle(String cid) throws Exception { Bundle mBundle = PowerMockito.mock(Bundle.class); PowerMockito.doNothing().when((BaseBundle)mBundle).putString(AnalyticsController.Params.CID, cid); } However, it always returns: java.lang.RuntimeException: Method putString in android.os.BaseBundle

PowerMock does not mock correctly

不羁的心 提交于 2020-06-16 04:39:07
问题 I have written a simple method which should take a url and retrieve the data from this url via a get request. The method looks like this: public String getResponse(String connectionUrl) throws HttpException { HttpURLConnection connection = null; try { URL url = new URL(connectionUrl); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode != 200) { throw new HttpException("Response code was "