mockito

How do i mock RestTemplate exchange

我的未来我决定 提交于 2020-01-16 01:16:09
问题 Trying the mocking as below : Mockito.doReturn(responseEntity).when(restTemplate.exchange(anyString(), anyObject(), anyObject(), anyObject())); this gave me a compilation issue: "The method exchange(String, HttpMethod, HttpEntity, Class, Object[]) is ambiguous for the type RestTemplate" so tried : GetRelationshipInfoResponse relationship = getEntity(); ResponseEntity<GetRelationshipInfoResponse> responseEntity = new ResponseEntity<GetRelationshipInfoResponse>(relationship, HttpStatus.ACCEPTED

No JSON content (comes up null) from MockMvc Test using Spring MVC and Mockito

徘徊边缘 提交于 2020-01-15 12:00:51
问题 Have a codebase which uses SpringMVC 4.0.3.RELEASE for Restful Web Services. Codebase contains a fully functional Restful Web Service which I can verify works using postman and curl. However, when trying to write a unit test for the particular Restful Web Service using MockMvc, I become blocked with trying to obtain the JSON content from the unit test. Am wondering if its a config issue or an issue where I am not creating a fake object correctly (since this doesn't rely on tomcat and is

How to test a method using a PrintWriter?

放肆的年华 提交于 2020-01-15 09:23:26
问题 I have following method: @Component public class WriteCsvToResponse { private static final Logger LOGGER = LoggerFactory.getLogger(WriteCsvToResponse.class); public void writeStatus(PrintWriter writer, Status status) { try { ColumnPositionMappingStrategy mapStrategy = new ColumnPositionMappingStrategy(); mapStrategy.setType(Status.class); String[] columns = new String[]{"id", "storeId", "status"}; mapStrategy.setColumnMapping(columns); StatefulBeanToCsv btcsv = new StatefulBeanToCsvBuilder

Grails unit test verify mock method called

不羁岁月 提交于 2020-01-15 05:45:51
问题 In my unit test, I mock a service (which is a ref of the class under test). Like: given: def mockXxService = mockFor(XxService) mockXxService.demand.xxx(1) {->} service.xxService = mockXxService when: service.yyy() then: // verify mockXxService's xxx method is invoked. For my unit test, I want to verify that mockXxService.xxx() is called. But grails document's mockControl.verify() doesn't work for me. Not sure how to use it correctly. It is very similar to mockito's verify method. Anyone

Verify a static method was called by another static method in PowerMock

依然范特西╮ 提交于 2020-01-14 05:28:11
问题 I have a Tool class with two static methods, doSomething(Object) and callDoSomething(). The names are intuitive in that callDoSomething delegates its call to doSomething(Object); public class Tool { public static void doSomething( Object o ) { } public static void callDoSomething() { doSomething( new Object()); } } I have a Test class for Tool and I'd like to verify if doSomething(Object) was called (I want to do Argument Matching too in the future) @RunWith( PowerMockRunner.class )

Junit test fails when run on package, but success when run on file

放肆的年华 提交于 2020-01-14 04:16:13
问题 Big update: Has anyone run into this before? I'm using JUnit 4.5 and Mockito 1.7 in a Maven project. I have testCaseA.java in package testCaseFolder. if I open testCaseA.java, right click in the code, select "Run as" -"Junit test" it is okay. But if I right click package, select "Run as" -"Junit test", it will fail: org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced argument matcher detected! Somewhere before this line you probably misused Mockito argument matchers. For

Scala Spec2 Mockito: Argument matchers with complex types

泪湿孤枕 提交于 2020-01-13 23:29:34
问题 I'm trying to write a mock for a web service with Mockito. The mock should simulate a POST request using the play WS library. /** * Mock for the Web Service */ case class WSMock() extends Mockito { val wsRequestHolder: play.api.libs.ws.WS.WSRequestHolder = mock[play.api.libs.ws.WS.WSRequestHolder] val wsResponse: play.api.libs.ws.Response = mock[play.api.libs.ws.Response] wsResponse.status returns 200 wsResponse.body returns "BODY RESP FROM WS" val futureResponse = scala.concurrent.Future {

Mockito in Android error: java.lang.ClassNotFoundException: android.os.Parcelable

自作多情 提交于 2020-01-13 19:26:11
问题 The interface is as follows: interface ILoginView{ fun userResponseProcessor(user: User?) } I mock the interface in my test class: @Mock private val mMockLoginView: ILoginView? = null when run this test, An error occurred Underlying exception : java.lang.IllegalArgumentException: Could not create type at org.mockito.internal.runners.DefaultInternalRunner$1.withBefores(DefaultInternalRunner.java:38) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:276) ...

Mocking type-casting objects

核能气质少年 提交于 2020-01-13 10:13:11
问题 I have a simple method in my bean to handle cell edit event in primefaces datatable. Method looks like this: public void onCellEdit(CellEditEvent event) { Object oldValue = event.getOldValue(); Object newValue = event.getNewValue(); User user = (User) ((DataTable) event.getComponent()).getRowData(); if (!newValue.equals(oldValue)) user.setUsername(oldValue.toString()); .... } How do I mock this: User user = (User) ((DataTable) event.getComponent()).getRowData(); 回答1: CellEditEvent mockEvent =

Mockito Injecting Null values into a Spring bean when using @Mock?

有些话、适合烂在心里 提交于 2020-01-13 05:12:11
问题 As I am new to Spring Test MVC I don't understand this problem. I took the code below from http://markchensblog.blogspot.in/search/label/Spring Variable mockproductService is not injected from Application Context and it contains null values while using @Mock annotation and getting assetion error. The Assertion error I currently encounter is as follows: java.lang.AssertionError: Model attribute 'Products' expected:<[com.pointel.spring.test.Product@e1b42, com.pointel.spring.test.Product@e1f03]>