Unable to set Exclusive Start Key on DynamoDB - java

I am trying to set exclusive start key to my request so that I can have my LastEvaluatedKey afterwards.
The follow exception occurs when I iterate over my query result. Can anyone help me in this regard?
"The provided starting key does not match the range key predicate"
QuerySpec queryExpression = ...
queryExpression = queryExpression.withExclusiveStartKey(OFFSET_PRIMARY_KEY, offsetParts[OFFSET_PRIMARY_KEY_INDEX], OFFSET_SORT_KEY, Long.valueOf( offsetParts[OFFSET_SORT_KEY_INDEX]));
ItemCollection<QueryOutcome> items = table.query(queryExpression);
Iterator<Item> iterator = items.iterator();
iterator.forEachRemaining(m -> { //exception occurs here
...
});
Stack Trace:
com.amazonaws.AmazonServiceException: The provided starting key does not match the range key predicate (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 6EA9NOO079F7VUVV1JP92P05MRVV4KQNSO5AEMVJF66Q9ASUAAJG)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1389)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:902)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:607)
at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:376)
at com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:338)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:287)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:1985)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.query(AmazonDynamoDBClient.java:1620)
at com.amazonaws.services.dynamodbv2.document.internal.QueryCollection.firstPage(QueryCollection.java:53)
at com.amazonaws.services.dynamodbv2.document.internal.PageIterator.next(PageIterator.java:45)
at com.amazonaws.services.dynamodbv2.document.internal.PageIterator.next(PageIterator.java:25)
at java.lang.Iterable.forEach(Iterable.java:74)
at com.my.com.services.database.DynamoDatabaseRepository.fetchMessageList(DynamoDatabaseRepository.java:675)
at service.DatabaseServiceTest.accc(DatabaseServiceTest.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130)

The “The provided starting key does not match the range key predicate” means that DynamoDB cannot hit the Item that ExclusiveStartKey represents by your query condition.So when you use queryPage api, you'd better not change the query condition until you find the last item; and you'd better not update the item that may be included in your pagination result.
Hope this answer can help someone.

This can happen if the row represented by the ExclusiveStartKey that you are passing no longer appears in the dynamodb query result.
Consider the case below:
Your Primary partition key is named "message_id"
Your Primary sort key is named "message_timestamp"
Your ExclusiveStartKey value is: '{"message_id":"3483b80a358cf60decfccc329bd5e72e","message_timestamp":1481114525}'
You are using KeyConditionExpression as: Key("message_id").eq("3483b80a358cf60decfccc329bd5e72e") & Key("message_timestamp").gt(1481202107)
Since the message_timestamp in KeyConditionExpression is greater than the corresponding value in ExclusiveStartKey, dynamodb will throw the error that you mentioned.

Related

Etag decoding error when using awssdk s3 v2 with GCS and Minio gateway

I am currently migrating my Java codebase to use S3 client v2 instead of old v1 client.
I'm interacting with the S3 API through a Minio gateway that sends/get objects to/from GCS (Google Cloud Storage).
The synchronous client was previously working fine but now I'm unable to do simple Put operations such as PutObjectRequest because the sdk client expects a MD5 hash in "eTag" field when it actually received a differently encoded hash. I am declaring my client as follows :
AwsBasicCredentials credentials = AwsBasicCredentials.create(config.getKey(), config.getSecret());
return S3Client.builder()
.endpointOverride(URI.create(config.getEndpoint()))
.region(Region.of(config.getRegion()))
.credentialsProvider(StaticCredentialsProvider.create(credentials))
.serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(true).build())
.build();
And using it with a simple Put request such as :
byte[] content = "THIS IS THE CONTENT".getBytes();
RequestBody requestBody = RequestBody.fromBytes(content);
PutObjectRequest build = PutObjectRequest.builder()
.bucket(BUCKET)
.key("test-file")
.build();
s3Client.putObject(build, requestBody);
The error stacktrace is the following :
software.amazon.awssdk.core.exception.SdkClientException: Unable to unmarshall response (Invalid base 16 character: '-'). Response Code: 200, Response Text: OK
at software.amazon.awssdk.core.exception.SdkClientException$BuilderImpl.build(SdkClientException.java:97)
at software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.handleSuccessResponse(HandleResponseStage.java:100)
at software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.handleResponse(HandleResponseStage.java:70)
at software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.execute(HandleResponseStage.java:58)
at software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.execute(HandleResponseStage.java:41)
at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptTimeoutTrackingStage.execute(ApiCallAttemptTimeoutTrackingStage.java:64)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptTimeoutTrackingStage.execute(ApiCallAttemptTimeoutTrackingStage.java:36)
at software.amazon.awssdk.core.internal.http.pipeline.stages.TimeoutExceptionHandlingStage.execute(TimeoutExceptionHandlingStage.java:77)
at software.amazon.awssdk.core.internal.http.pipeline.stages.TimeoutExceptionHandlingStage.execute(TimeoutExceptionHandlingStage.java:39)
at software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage$RetryExecutor.doExecute(RetryableStage.java:113)
at software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage$RetryExecutor.execute(RetryableStage.java:86)
at software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(RetryableStage.java:62)
at software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(RetryableStage.java:42)
at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
at software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(StreamManagingStage.java:57)
at software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(StreamManagingStage.java:37)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.executeWithTimer(ApiCallTimeoutTrackingStage.java:80)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(ApiCallTimeoutTrackingStage.java:60)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(ApiCallTimeoutTrackingStage.java:42)
at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(ExecutionFailureExceptionReportingStage.java:37)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(ExecutionFailureExceptionReportingStage.java:26)
at software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient$RequestExecutionBuilderImpl.execute(AmazonSyncHttpClient.java:240)
at software.amazon.awssdk.core.client.handler.BaseSyncClientHandler.invoke(BaseSyncClientHandler.java:96)
at software.amazon.awssdk.core.client.handler.BaseSyncClientHandler.execute(BaseSyncClientHandler.java:120)
at software.amazon.awssdk.core.client.handler.BaseSyncClientHandler.execute(BaseSyncClientHandler.java:73)
at software.amazon.awssdk.core.client.handler.SdkSyncClientHandler.execute(SdkSyncClientHandler.java:44)
at software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler.execute(AwsSyncClientHandler.java:55)
at software.amazon.awssdk.services.s3.DefaultS3Client.putObject(DefaultS3Client.java:3053)
at com.airbus.geocloud.dm.client.storage.sync.SyncStorageClientIntegrationTest$Local.simplePut(SyncStorageClientIntegrationTest.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalArgumentException: Invalid base 16 character: '-'
at software.amazon.awssdk.utils.internal.Base16Codec.pos(Base16Codec.java:80)
at software.amazon.awssdk.utils.internal.Base16Codec.decode(Base16Codec.java:66)
at software.amazon.awssdk.utils.internal.Base16Lower.decode(Base16Lower.java:65)
at software.amazon.awssdk.services.s3.checksums.ChecksumsEnabledValidator.validatePutObjectChecksum(ChecksumsEnabledValidator.java:131)
at software.amazon.awssdk.services.s3.internal.handlers.SyncChecksumValidationInterceptor.afterUnmarshalling(SyncChecksumValidationInterceptor.java:88)
A few notes :
I have done the same test directly on GCS and it's working perfectly (GCS is actually returning a MD5 hash) so it seems to be a Minio gateway issue.
I was previously (when using client v1) using a "S3SignerType" override
I have tried adding one of Aws4Signer or AwsS3V4Signer to my client configuration, without success
Environment:
Minio:
Version: 2019-05-02T19:07:09Z
Release-Tag: RELEASE.2019-05-02T19-07-09Z
Commit-ID: 4b858b562a0887e10bfd0414dc87e68f1af31c3a
Java SDK version: 2.5.25
Any ideas ?
It actually was a bug with Minio versions under RELEASE.2019-05-23T00-29-34Z, it has since been fixed.

Getting org.mockito.exceptions.misusing.InvalidUseOfMatchersException exception even though i have used Mather correctly

I am trying to mock a service using Mockito, but it is saying i have not used argument Mather correctly.
import com.spmsoftware.appframework.validation.ValidationContext;
import com.spmsoftware.appframework.validation.ValidationContextSupport;
import com.spmsoftware.filters.api.model.Filter;
import com.spmsoftware.filters.api.validation.FilterValidationService;
import com.spmsoftware.tablereference.api.service.TableReferenceService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.when;
public class TestInputTabValidator {
    #Mock
    private Filter filter;
    #Mock
    private TableReferenceService tableReferenceService;
    #Mock
    private FilterValidationService filterValidationService;
    #InjectMocks private InputTabValidator inputTabValidator;
    #Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        preCondition();
    }
    private void preCondition() {
        when(tableReferenceService.getTable(anyLong())).thenReturn(anyObject());
        when(tableReferenceService.tableExists(1L)).thenReturn(true);
    }
    #Test
    public void validateInputTabSaveWithFilter() {
        ValidationContext validationContext = new ValidationContextSupport();
        inputTabValidator.validate(new Object[]{anyLong(), filter}, validationContext);
        Assert.assertTrue("no validation errors recorded", !validationContext.hasErrors());
    }
    public void validateInputTabSaveWithoutFilter() {
        ValidationContext validationContext = new ValidationContextSupport();
        inputTabValidator.validate(new Object[]{anyLong()}, validationContext);
        Assert.assertTrue("no validation errors recorded", !validationContext.hasErrors());
    }
}
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 2 recorded:
-> at com.spmsoftware.genericWebservices.validator.TestInputTabValidator.preCondition(TestInputTabValidator.java:55)
-> at com.spmsoftware.genericWebservices.validator.TestInputTabValidator.preCondition(TestInputTabValidator.java:56)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
at com.spmsoftware.genericWebservices.validator.TestInputTabValidator.preCondition(TestInputTabValidator.java:56)
at com.spmsoftware.genericWebservices.validator.TestInputTabValidator.setUp(TestInputTabValidator.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Cause is due to Mockito.any(MessageCreator.class) , but isn't there a way to test my send method is getting executed without creating an actual object in the MessageCreator?
The problem is in the following line:
when(tableReferenceService.getTable(anyLong())).thenReturn(anyObject());
You can't use a matcher as the expected return value. What you want is probably something like:
when(tableReferenceService.getTable(anyLong())).thenReturn(new Table());
or
Table table = mock(Table.class);
when(tableReferenceService.getTable(anyLong())).thenReturn(table);
depending on whether the object returned by tableReferenceService.getTable() requires mocking or not.

What is the current best practice for testing a DoFn that use MapState

I'm writing a piece of dataflow transform that uses org.apache.beam.sdk.state.MapState to implement caching functionality. However upon introducing MapState, the unit test starts to dysfunction. The exception says: java.lang.UnsupportedOperationException: Parameter StateParameter{referent=StateDeclaration{id=cache, field=private final org.apache.beam.sdk.state.StateSpec xxxxFn.cache, stateType=org.apache.beam.sdk.state.MapState<java.lang.String, object>}} not supported by DoFnTester
So if DoFnTester is no longer an option, what is the current best practice for testing a DoFn that use MapState?
P.S. I'm currently on Beam 2.0.0, and I can't upgrade to 2.2.0 because https://issues.apache.org/jira/browse/BEAM-3693
Here is the complete Trace:
java.lang.UnsupportedOperationException: Parameter StateParameter{referent=StateDeclaration{id=cache, field=private final org.apache.beam.sdk.state.StateSpec xxxxFn.cache, stateType=org.apache.beam.sdk.state.MapState<java.lang.String, object>}} not supported by DoFnTester
at org.apache.beam.sdk.transforms.DoFnTester$5.dispatchDefault(DoFnTester.java:725)
at org.apache.beam.sdk.transforms.DoFnTester$5.dispatchDefault(DoFnTester.java:710)
at org.apache.beam.sdk.transforms.reflect.DoFnSignature$Parameter$Cases$WithDefault.dispatch(DoFnSignature.java:255)
at org.apache.beam.sdk.transforms.reflect.DoFnSignature$Parameter.match(DoFnSignature.java:193)
at org.apache.beam.sdk.transforms.DoFnTester.<init>(DoFnTester.java:709)
at org.apache.beam.sdk.transforms.DoFnTester.of(DoFnTester.java:92)
at xxxxFnTest.testNormalRun(GetPredictionsFnTest.java:50)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
The Java DirectRunner is the best way to test your DoFn, whatever features it uses.
Using DoFnTester will enable you to gain false confidence by testing scenarios that are not feasible while missing scenarios that can really happen. For this reason, and bugs / lack of maintenance, DoFnTester is now deprecated and you should try to move off of it.
To have control over watermarks, the processing clock, and element bundling, you can use TestStream as described on the Beam blog.

Mockito Test RecyclerView Click Error

I have a test with Mockito from my RecyclerView clicks, but an error is thrown.
This is my code:
#Test
public void getUserClick() {
RecyclerView rcv = Mockito.mock(RecyclerView.class);
List<User> usersList = new ArrayList<>();
User commonUser = Mockito.mock(User.class);
commonUser.setId("1");
commonUser.setName("User");
commonUser.setEmail("admin#admin.com");
usersList.add(commonUser);
UserAdapterTest adapter = Mockito.mock(UserAdapterTest.class);
adapter.addData(usersList);
rcv.setAdapter(adapter);
// Test Click
rcv.getChildAt(0).performClick();
//Verify Click Item
Mockito.verify(adapter).testClickItem(commonUser);
}
And the error launch here:
java.lang.NullPointerException in rcv.getChildAt(0).performClick();
But the IDE doesn't tell me anything else.
java.lang.NullPointerException
at com.project.test.TestRecycler.getUserClick(TestRecycler.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.mockito.internal.junit.JUnitRule$1.evaluateSafely(JUnitRule.java:63)
at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:43)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
And my adapter already has the click event implemented.
When you do:
RecyclerView rcv = Mockito.mock(RecyclerView.class);
rcv becomes a mock (an object that returns null (or 0, or false) by default, on any method call). So rcv.getChildAt(0) by default is returning null, and when you try to call performClick() on it, the NullPointerException occurs.
When you create a mock and want some method to return a specific value, you must tell it using Mockito.when(). So in your case, you should do something like this:
// configure "when" behaviour **before** calling the getChildAt() method
Mockito.when(rcv.getChildAt(0)).thenReturn(objectYouWantToBeReturned);
Obviously you need to create objectYouWantToBeReturned before using when.
The code above will make rcv.getChildAt(0) returns the object you want, but only if you call it with parameter 0.
If you want to return the same object for any value (not only 0), you can do:
Mockito.when(rcv.getChildAt(Mockito.anyInt())).thenReturn(objectYouWantToBeReturned);
This will return objectYouWantToBeReturned for all calls of getChildAt, no matter what's the parameter value.

AssertionError on totally unrelated line of code - Junit test for school project

I am working on a Java RMI project and I am trying to begin to test the code I've written and when I run the following code, I get an AssertionError on a line that doesn't even have an assertion statement. I am confused as to how to fix this.
public void basicTest() throws UnknownHostException, RemoteException, AlreadyBoundException, NotBoundException, InterruptedException {
int numBooks = 20;
int copiesPerBook = 5;
int booksPerMember = 4;
// Simulate the server
LibraryServerImpl library = new LibraryServerImpl(numBooks, copiesPerBook, booksPerMember);
LibraryServer stub = (LibraryServer) java.rmi.server.UnicastRemoteObject.exportObject(library, 0);
Registry registry = LocateRegistry.createRegistry(port);
registry.bind(libraryName, stub);
// Simulate the client
Member member = new MemberImpl();
assertNotNull(member.getName()); // Will fail until you implement MemberImpl
Thread t = new Thread(new BasicClient(member));
t.start();
t.join();
}
This is the copied trace:
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertNotNull(Assert.java:712)
at org.junit.Assert.assertNotNull(Assert.java:722)
at PublicTests.basicTest(PublicTests.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Line 56 corresponds to registry.bind(libraryName, stub);
Clearly, this does not make sense, so one of the facts is false.
Fact: you got the above stack trace. --no reason to doubt the truthfulness of this fact.
Fact: PublicTests.java is written by you. --no reason to doubt the truthfulness of this fact.
Fact: line 56 of PublicTests.java corresponds to registry.bind(libraryName, stub). Well, this cannot be true, since according to the stack trace, the failure is detected by a call to org.junit.Assert.assertNotNull(), but the line registry.bind(libraryName, stub) is not an invocation of org.junit.Assert.assertNotNull().
However, further down in your code you do have an invocation of assertNotNull(member.getName()), which means that this must be what is being reported as line 56.
So, what you are looking at is a mismatch in the line number being reported in the stack trace. As user indivisible suggested, please refresh your project, clean your output folder, rebuild all projects, and try again.

Categories

Resources