Skip to content

Commit f4c44f7

Browse files
authored
Merge pull request #23 from target/fix_scale_ut
Mock lock to fix Scale UT
2 parents edff3ea + ce6c2ce commit f4c44f7

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/test/java/com/target/devicemanager/components/scale/ScaleManagerTest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public void getStableWeight_ReturnsWeight() throws ScaleException, ExecutionExce
338338
//arrange
339339
FormattedWeight expected = new FormattedWeight(3);
340340
when(mockCompletableFutureFormattedWeight.get(30000, TimeUnit.MILLISECONDS)).thenReturn(expected);
341-
341+
when(mockScaleDevice.tryLock()).thenReturn(true);
342342
//act
343343
FormattedWeight actual = scaleManager.getStableWeight(mockCompletableFutureFormattedWeight);
344344

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

351+
@Test
352+
public void getStableWeight_ReturnsBusy() throws ScaleException, ExecutionException, InterruptedException, TimeoutException {
353+
//arrange
354+
when(mockScaleDevice.tryLock()).thenReturn(false);
355+
//act
356+
try{
357+
scaleManager.getStableWeight(mockCompletableFutureFormattedWeight);
358+
}
359+
catch(ScaleException scaleException) {
360+
//assert
361+
assertEquals("DEVICE_BUSY", scaleException.getDeviceError().getCode());
362+
return;
363+
}
364+
fail("Expected Exception, but got none");
365+
}
366+
351367
@Test
352368
public void getStableWeight_ThrowsExecutionException() throws ExecutionException, InterruptedException, TimeoutException {
353369
//arrange
354370
ExecutionException executionException = new ExecutionException(new JposException(ScaleConst.JPOS_ESCAL_UNDER_ZERO));
355371
when(mockCompletableFutureFormattedWeight.get(30000, TimeUnit.MILLISECONDS)).thenThrow(executionException);
356-
372+
when(mockScaleDevice.tryLock()).thenReturn(true);
357373
//act
358374
try {
359375
scaleManager.getStableWeight(mockCompletableFutureFormattedWeight);
@@ -372,7 +388,7 @@ public void getStableWeight_ThrowsInterruptedException() throws ExecutionExcepti
372388
//arrange
373389
InterruptedException interruptedException = new InterruptedException();
374390
when(mockCompletableFutureFormattedWeight.get(30000, TimeUnit.MILLISECONDS)).thenThrow(interruptedException);
375-
391+
when(mockScaleDevice.tryLock()).thenReturn(true);
376392
//act
377393
try {
378394
scaleManager.getStableWeight(mockCompletableFutureFormattedWeight);
@@ -391,7 +407,7 @@ public void getStableWeight_ThrowsTimeoutException() throws ExecutionException,
391407
//arrange
392408
TimeoutException timeoutException = new TimeoutException();
393409
when(mockCompletableFutureFormattedWeight.get(30000, TimeUnit.MILLISECONDS)).thenThrow(timeoutException);
394-
410+
when(mockScaleDevice.tryLock()).thenReturn(true);
395411
//act
396412
try {
397413
scaleManager.getStableWeight(mockCompletableFutureFormattedWeight);

0 commit comments

Comments
 (0)