Skip to content

Commit

Permalink
Merge pull request #23 from target/fix_scale_ut
Browse files Browse the repository at this point in the history
Mock lock to fix Scale UT
  • Loading branch information
arpal7 authored Dec 13, 2023
2 parents edff3ea + ce6c2ce commit f4c44f7
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public void getStableWeight_ReturnsWeight() throws ScaleException, ExecutionExce
//arrange
FormattedWeight expected = new FormattedWeight(3);
when(mockCompletableFutureFormattedWeight.get(30000, TimeUnit.MILLISECONDS)).thenReturn(expected);

when(mockScaleDevice.tryLock()).thenReturn(true);
//act
FormattedWeight actual = scaleManager.getStableWeight(mockCompletableFutureFormattedWeight);

Expand All @@ -348,12 +348,28 @@ public void getStableWeight_ReturnsWeight() throws ScaleException, ExecutionExce
assertEquals(expected, actual);
}

@Test
public void getStableWeight_ReturnsBusy() throws ScaleException, ExecutionException, InterruptedException, TimeoutException {
//arrange
when(mockScaleDevice.tryLock()).thenReturn(false);
//act
try{
scaleManager.getStableWeight(mockCompletableFutureFormattedWeight);
}
catch(ScaleException scaleException) {
//assert
assertEquals("DEVICE_BUSY", scaleException.getDeviceError().getCode());
return;
}
fail("Expected Exception, but got none");
}

@Test
public void getStableWeight_ThrowsExecutionException() throws ExecutionException, InterruptedException, TimeoutException {
//arrange
ExecutionException executionException = new ExecutionException(new JposException(ScaleConst.JPOS_ESCAL_UNDER_ZERO));
when(mockCompletableFutureFormattedWeight.get(30000, TimeUnit.MILLISECONDS)).thenThrow(executionException);

when(mockScaleDevice.tryLock()).thenReturn(true);
//act
try {
scaleManager.getStableWeight(mockCompletableFutureFormattedWeight);
Expand All @@ -372,7 +388,7 @@ public void getStableWeight_ThrowsInterruptedException() throws ExecutionExcepti
//arrange
InterruptedException interruptedException = new InterruptedException();
when(mockCompletableFutureFormattedWeight.get(30000, TimeUnit.MILLISECONDS)).thenThrow(interruptedException);

when(mockScaleDevice.tryLock()).thenReturn(true);
//act
try {
scaleManager.getStableWeight(mockCompletableFutureFormattedWeight);
Expand All @@ -391,7 +407,7 @@ public void getStableWeight_ThrowsTimeoutException() throws ExecutionException,
//arrange
TimeoutException timeoutException = new TimeoutException();
when(mockCompletableFutureFormattedWeight.get(30000, TimeUnit.MILLISECONDS)).thenThrow(timeoutException);

when(mockScaleDevice.tryLock()).thenReturn(true);
//act
try {
scaleManager.getStableWeight(mockCompletableFutureFormattedWeight);
Expand Down

0 comments on commit f4c44f7

Please sign in to comment.