mockito

Partial Mocking on HttpSession

和自甴很熟 提交于 2021-02-08 04:15:02
问题 There is a servlet i want to test , servlet have session and puts "loginBean" (which contain logged in user related info) inside session , which i have mocked already and working fine , now IN GUI level , there are 2 tab , DetailSet1 , detailsSet2 , when u enter data of DetailSet1 , it get saved in session and also does some business logic , now it comes to DetailsSet2 , you already have DetailSet1 in session , so it got all it needs , data is saved in DB. Now it's obvious i have to mock

Partial Mocking on HttpSession

☆樱花仙子☆ 提交于 2021-02-08 04:11:59
问题 There is a servlet i want to test , servlet have session and puts "loginBean" (which contain logged in user related info) inside session , which i have mocked already and working fine , now IN GUI level , there are 2 tab , DetailSet1 , detailsSet2 , when u enter data of DetailSet1 , it get saved in session and also does some business logic , now it comes to DetailsSet2 , you already have DetailSet1 in session , so it got all it needs , data is saved in DB. Now it's obvious i have to mock

Mocking SecureRandom::nextInt()

安稳与你 提交于 2021-02-08 01:57:37
问题 I have a class that uses a SecureRandom instance and grabs the next random number. Lets say the example is: public class ExampleClass() { public void method() { Random sr = new SecureRandom(); System.out.printf("%d %n", sr.nextInt(1)); System.out.printf("%d %n", sr.nextInt(1)); } } Test code @RunWith(PowerMockRunner.class) public class ExampleClassTest { ... @Test @PrepareOnlyThisForTest(SecureRandom.class) public void mockedTest() throws Exception { Random spy = PowerMockito.spy(new

Mocking SecureRandom::nextInt()

試著忘記壹切 提交于 2021-02-08 01:57:04
问题 I have a class that uses a SecureRandom instance and grabs the next random number. Lets say the example is: public class ExampleClass() { public void method() { Random sr = new SecureRandom(); System.out.printf("%d %n", sr.nextInt(1)); System.out.printf("%d %n", sr.nextInt(1)); } } Test code @RunWith(PowerMockRunner.class) public class ExampleClassTest { ... @Test @PrepareOnlyThisForTest(SecureRandom.class) public void mockedTest() throws Exception { Random spy = PowerMockito.spy(new

Mockito Spy calls the actual method of the spied class instead of returning the hardcoded mock

对着背影说爱祢 提交于 2021-02-07 20:01:32
问题 Here's my Spy statement OAuthService oAuthServiceMock = Mockito.mock(OAuthService.class); Mockito.doReturn(oAuthServiceMock).when(inviteServiceSpy.buildOAuthService(RESOURCE_URL, CONSUMER_KEY, CONSUMER_SECRET)); Here's the method in the actual class public OAuthService buildOAuthService(String RESOURCE_URL, String CONSUMER_KEY, String CONSUMER_SECRET) { return new ServiceBuilder() .provider(new DummyOAuth1ApiProvider(RESOURCE_URL)) .apiKey(CONSUMER_KEY) .apiSecret(CONSUMER_SECRET)

Mockito Spy calls the actual method of the spied class instead of returning the hardcoded mock

我只是一个虾纸丫 提交于 2021-02-07 20:01:24
问题 Here's my Spy statement OAuthService oAuthServiceMock = Mockito.mock(OAuthService.class); Mockito.doReturn(oAuthServiceMock).when(inviteServiceSpy.buildOAuthService(RESOURCE_URL, CONSUMER_KEY, CONSUMER_SECRET)); Here's the method in the actual class public OAuthService buildOAuthService(String RESOURCE_URL, String CONSUMER_KEY, String CONSUMER_SECRET) { return new ServiceBuilder() .provider(new DummyOAuth1ApiProvider(RESOURCE_URL)) .apiKey(CONSUMER_KEY) .apiSecret(CONSUMER_SECRET)

org.mockito.exceptions.misusing.MissingMethodInvocationException in MOCKITO

删除回忆录丶 提交于 2021-02-07 18:22:18
问题 I am having a simple controller class @RestController open class MyController() { @Autowired lateinit var myInterface: MyInterface @GetMapping(value = ["/v1/call-Api"], produces = ["application/json"]) fun getData():Response{ callFx() /// Here I have logic } fun callFx():String{ return myInterface.getmyStringData() } } Now Come to implementation part of MyInterface @Service class MyImpl: MyInterface { override fun getmyStringData(){ return "Some string" } } Please note that for MyInterface ,

Connection refused when using wiremock

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 14:54:29
问题 I have this piece of code in a Junit, where I clearly set the port to 8888 when(clientUtils.getLinkUrl(eq(HOSTELS_MICROSERVICE.name()), eq(HOSTELS_MICROSERVICE.name()), anyMap())) .thenReturn("http://localhost:8888/HOSTELS/HOSTELSMethods"); stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn( aResponse().withStatus(200) .withHeader("Content-Type", APPLICATION_JSON_VALUE) .withBody(ResourceUtils.getResourceFileAsString ("__files/HOSTELS.json"))));

Connection refused when using wiremock

*爱你&永不变心* 提交于 2021-02-07 14:53:53
问题 I have this piece of code in a Junit, where I clearly set the port to 8888 when(clientUtils.getLinkUrl(eq(HOSTELS_MICROSERVICE.name()), eq(HOSTELS_MICROSERVICE.name()), anyMap())) .thenReturn("http://localhost:8888/HOSTELS/HOSTELSMethods"); stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn( aResponse().withStatus(200) .withHeader("Content-Type", APPLICATION_JSON_VALUE) .withBody(ResourceUtils.getResourceFileAsString ("__files/HOSTELS.json"))));

How to mock forEach behavior with Mockito

佐手、 提交于 2021-02-07 14:36:41
问题 I want to make the following work, but I don't know how to mock forEach behavior properly. (The code is taken from a related question Testing Java enhanced for behavior with Mockito ) @Test public void aa() { Collection<String> fruits; Iterator<String> fruitIterator; fruitIterator = mock(Iterator.class); when(fruitIterator.hasNext()).thenReturn(true, true, true, false); when(fruitIterator.next()).thenReturn("Apple") .thenReturn("Banana").thenReturn("Pear"); fruits = mock(Collection.class);