I am trying to use bounded types with generics to create generic objects of subclasses (these implement an interface). But I am getting type mismatch errors when initializing objects with the subclasses.
Here is the interface:
public interface ScannableEntity {
}
Here's the class that implements this interface:
public final class Attachment implements ScannableEntity {
.
.
.
}
Now I created 2 classes (SegmentPageScanResult and ItemProcessor) with the bounded generic type as:
@Builder
public class SegmentPageScanResult<TEntity extends ScannableEntity> {
.
.
.
}
and
public class ItemProcessor<TEntity extends ScannableEntity> {
void submitAndExecute(SegmentPageScanResult<TEntity> pageScanResult) {
. . .
}
}
When I am trying to initialize the SegmentPageScanResult and try calling submitAndExecute
method of ItemProcessor from a Unit test as follows:
@ExtendWith(MockitoExtension.class)
public class ScanTest {
@Mock
private ItemProcessor<Attachment> itemProcessor;
@Test
public void testDoScan() {
Attachment mockRecord = new Attachment();
SegmentPageScanResult<Attachment> segmentPageScanResult = SegmentPageScanResult.builder()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.scannedItems(ImmutableList.of(mockRecord))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.isLastPage(true)
^^^^^^^^^^^^^^^^^
.build();
^^^^^^^^^
verify(itemProcessor).submitAndExecute(segmentPageScanResult);
}
}
I get the error -
Required type: SegmentPageScanResult<Attachment>
Provided:SegmentPageScanResult<ScannableEntity>
Can someone please help me understand why I am not able to initialize the generic object with the class implementing the interface?