mockito

Mock Httpservletrequest and requestcontext

你。 提交于 2021-02-10 18:54:46
问题 I am trying to mock RequestContext and HttpServletRequest classes/interfaces but they not working. code: @Override public Object run() { String accessToken= ""; ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); String requestedServiceUri = request.getRequestURI(); //... Mock I have written //... HttpServletRequest request = Mockito.mock(HttpServletRequest.class); RequestContext requestContext = Mockito.mock(RequestContext.class); when(request.getHeader(

Unable to initialize Mockito

只谈情不闲聊 提交于 2021-02-10 16:50:35
问题 I am trying to set up Mockito 2.22 and have downloaded mockito-core-2.22.2.jar and byte-buddy-1.9.0.jar and, in the Netbeans 8.2 project, I have added those two jar files, JUnit 4.12 and Hamcrest 1.3 to the test libraries. When I try to run the MCVE: package com.stackoverflow.test; import org.junit.Test; import static org.mockito.Mockito.mock; public class SimpleMockTest { public static class A{ public String value(){ return "A"; } } @Test public void testASimpleMock() { A mocked = mock( A

Unable to initialize Mockito

只谈情不闲聊 提交于 2021-02-10 16:49:58
问题 I am trying to set up Mockito 2.22 and have downloaded mockito-core-2.22.2.jar and byte-buddy-1.9.0.jar and, in the Netbeans 8.2 project, I have added those two jar files, JUnit 4.12 and Hamcrest 1.3 to the test libraries. When I try to run the MCVE: package com.stackoverflow.test; import org.junit.Test; import static org.mockito.Mockito.mock; public class SimpleMockTest { public static class A{ public String value(){ return "A"; } } @Test public void testASimpleMock() { A mocked = mock( A

Using MockitoSugar of scalatestplus-play deprecated

元气小坏坏 提交于 2021-02-10 16:18:51
问题 I'm using "scalatestplus-play" % "5.1.0" , and I'm trying to bring MockitoSugar to my class like I did in earlier projects: class MyTestSpec extends AnyFreeSpec with MockitoSugar so I can use the mock[MyClass] function and now it docent exists, can't find in the documentation something else. in my earlier projects I used "scalatestplus-play" % "4.0.3" , I'm sure there is a different way now but can't find what it is 回答1: First option you have, is to use scalatestplus-mockito, where you can

Mockito get all mocked objects

强颜欢笑 提交于 2021-02-10 12:07:29
问题 I want to get the list of all mocked objects. Using a previous version of Mockito I could do this: List<Object> createdMocks = new LinkedList<Object>(); MockingProgress progress = new ThreadSafeMockingProgress(); progress.setListener(new CollectCreatedMocks(createdMocks)); These listeners are removed in the latest 2.8 version of Mockito, is there any alternative for it? 回答1: Since Mockito 2.x this has been replaced by implementations of org.mockito.listeners.MockitoListener which you engage

Issue with stubbing/mocking the method with makes a DB call

时间秒杀一切 提交于 2021-02-10 06:22:27
问题 I am having issue with Mocking a JDBC call using the MockitoJUnitRunner. Somehow Mockito is not mocking the actual call even though I have below subbing line into the test class. when(readOnlyJdbcTemplate.query(anyString(), any(Object[].class), any(int[].class), any(FeatureCollectionResponseExtractor.class))).thenReturn(actual); Very similar mocking is working in another class for very similar type of method. The only difference between them is my other class does have 3 parameters instead of

Issue with stubbing/mocking the method with makes a DB call

∥☆過路亽.° 提交于 2021-02-10 06:22:08
问题 I am having issue with Mocking a JDBC call using the MockitoJUnitRunner. Somehow Mockito is not mocking the actual call even though I have below subbing line into the test class. when(readOnlyJdbcTemplate.query(anyString(), any(Object[].class), any(int[].class), any(FeatureCollectionResponseExtractor.class))).thenReturn(actual); Very similar mocking is working in another class for very similar type of method. The only difference between them is my other class does have 3 parameters instead of

Mockito.anyString() crashes with NPE in Kotlin

倖福魔咒の 提交于 2021-02-09 10:53:53
问题 I am using espresso and I want to create a mock response for the content resolver. When I use: `when`(context.activity.contentResolver.query( ArgumentMatchers.isA(Uri::class.java), ArgumentMatchers.isA(Array<String>::class.java), ArgumentMatchers.anyString(), null, null)) .thenReturn(matrixCursor) I get the error: java.lang.NullPointerException: uri at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:128) Is it possible to create the mock response for the content

How to fix the null value response of RestTemplate.exchange in a Mockito Test?

点点圈 提交于 2021-02-08 23:38:20
问题 My Service class is below, followed by its test - @Service public class MyServiceImpl implements MyService { @Autowired private RestTemplate restTemplate; @Override public StudentInfo getStudentInfo(String name) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); HttpEntity entity = new HttpEntity(headers); StudentInfo student = null; URI uri = new URI("http:\\someurl.com"); ResponseEntity<String> responseEntity = restTemplate.exchange(uri,

Mock method with parameters

ⅰ亾dé卋堺 提交于 2021-02-08 11:43:39
问题 I am trying to mock the below line but it gives an error while executing it, it says: Misplaced argument matcher detected here: You cannot use argument matchers outside of verification or stubbing. Examples of correct usage of argument matchers: when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); verify(mock).someMethod(contains("foo")) Also, this error might show up because you use argument matchers with methods that cannot be