Hi,
The Java runtime will fail the same way if the whole hierarchy / object graph is not Serializable. So if your builders are not Serializable it won't work either with the real objects or with the mocks.
Just make your builders implement Serializable.
Also your test is way tooo big, you really should consider using the real builders instead of mocking them, besides mocking everything is considered a test smell or test antipattern. While builders are not per se Value Objects, I don't see the point of mocking them in you case.
http://www.mockobjects.com/2007/04/test-smell-everything-is-mocked.html
If the data initialization is expensive, you should create a factory class in your test classpath that will help you to create these objects.
Cheers,
-- Brice
On Thu, Sep 6, 2012 at 1:33 AM, Yuhan Zhang
<yzh...@onescreen.com> wrote:
Hi all,
I'm trying to mocking elasticsearch with mockito, and pass the mocked objects into a cascading workflow.
The objects in cascading are required to be serializable. so I mocked with serilizeable settings
ClusterStateRequestBuilder clusterStateRequestBuilder = mock( ClusterStateRequestBuilder.class, Mockito.withSettings().serializable());
However, it gave me a different exception for being unable to find a constructor for ClusterStateRequestBuilder$$$EnhancerByMockitoWithCGLIB$$4122f8b3; no valid constructor
Here's the stacktrace:
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor15.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:88)
... 12 more
Caused by: cascading.flow.FlowException: internal error during mapper configuration
at cascading.flow.FlowMapper.configure(FlowMapper.java:65)
... 16 more
Caused by: java.io.InvalidClassException: org.elasticsearch.action.admin.cluster.state.ClusterStateRequestBuilder$$EnhancerByMockitoWithCGLIB$$4122f8b3; org.elasticsearch.action.admin.cluster.state.ClusterStateRequestBuilder$$EnhancerByMockitoWithCGLIB$$4122f8b3; no valid constructor
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:730)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
Here's how I'm mocking it:
TransportClient client = mock(TransportClient.class, Mockito.withSettings().serializable());
IndexRequestBuilder indexBuilder = mock( IndexRequestBuilder.class, Mockito.withSettings().serializable());
when(client.prepareIndex( anyString(), anyString() )).thenReturn( indexBuilder );
when( indexBuilder.setId( anyString() )).thenReturn( indexBuilder );
when( indexBuilder.setType( anyString() )).thenReturn( indexBuilder );
when( indexBuilder.setSource( (XContentBuilder) any() )).thenReturn( indexBuilder );
//
AdminClient adminClient = mock( AdminClient.class, Mockito.withSettings().serializable());
IndicesAdminClient indicesAdminClient = mock( IndicesAdminClient.class, Mockito.withSettings().serializable());
PutMappingRequestBuilder putMappingRequestBuilder = mock( PutMappingRequestBuilder.class, Mockito.withSettings().serializable());
ListenableActionFuture<PutMappingResponse> listenerableActionFilter = mock( ListenableActionFuture.class, Mockito.withSettings().serializable());
PutMappingResponse putMappingResponse = mock( PutMappingResponse.class, Mockito.withSettings().serializable());
when(client.admin()).thenReturn( adminClient );
when(adminClient.indices()).thenReturn( indicesAdminClient );
when( indicesAdminClient.preparePutMapping( anyString() ) ).thenReturn( putMappingRequestBuilder );
when( putMappingRequestBuilder.setType( anyString() ) ).thenReturn( putMappingRequestBuilder );
when( putMappingRequestBuilder.execute() ).thenReturn( listenerableActionFilter );
when( listenerableActionFilter.actionGet() ).thenReturn( putMappingResponse );
//
ClusterAdminClient clusterAdminClient = mock( ClusterAdminClient.class, Mockito.withSettings().serializable());
ClusterStateRequestBuilder clusterStateRequestBuilder = mock( ClusterStateRequestBuilder.class, Mockito.withSettings().serializable());
ListenableActionFuture<ClusterStateResponse> listenerableActionFilter2 = mock( ListenableActionFuture.class, Mockito.withSettings().serializable());;
ClusterStateResponse clusterStateResponse = mock( ClusterStateResponse.class, Mockito.withSettings().serializable());
ClusterState clusterState = mock( ClusterState.class, Mockito.withSettings().serializable());
MetaData metaData = mock( MetaData.class, Mockito.withSettings().serializable());
IndexMetaData indexMetaData = mock( IndexMetaData.class, Mockito.withSettings().serializable());
MappingMetaData mappingMetaData = mock( MappingMetaData.class, Mockito.withSettings().serializable());
when( adminClient.cluster() ).thenReturn( clusterAdminClient );
when( clusterAdminClient.prepareState() ).thenReturn( clusterStateRequestBuilder );
when( clusterStateRequestBuilder.setFilterIndices( anyString() ) ).thenReturn( clusterStateRequestBuilder );
when( clusterStateRequestBuilder.execute() ).thenReturn( listenerableActionFilter2 );
when( listenerableActionFilter2.actionGet() ).thenReturn( clusterStateResponse );
when( clusterStateResponse.getState() ).thenReturn( clusterState );
when( clusterState.getMetaData() ).thenReturn( metaData );
when( metaData.index( anyString() )).thenReturn( null, indexMetaData, indexMetaData,indexMetaData,indexMetaData,indexMetaData,indexMetaData,indexMetaData,indexMetaData,indexMetaData );
when( indexMetaData.mapping( anyString() )).thenReturn( null, mappingMetaData, mappingMetaData, mappingMetaData, mappingMetaData, mappingMetaData, mappingMetaData, mappingMetaData );
could someone help with this?
Thank you.
Yuhan
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/RYV57hgnttcJ.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.