Метод службы издевательств с дженериками - PullRequest
0 голосов
/ 27 ноября 2018

Я должен издеваться над этим сервисом

@Service
public class RelationServiceImpl implements RelationService {

    @Autowired
    private Map<String, Class<? extends Historiable>> modelRelations;


    @Override
    public Class<? extends Historiable> getModel(String collectionName) {

        return modelRelations.get(collectionName);
    }

}

Но я не знаю, как определить поведение в тесте.Когда я пытаюсь это

    @Mock
    private RelationService relationService;

        @Test
        public void getHistoryOk2() throws NotFoundException, ClassNotFoundException {
            ...



 when(relationService.getModel(eq(collectionName))).thenReturn(ShipmentRequest.class);
            ...

     }

я получаю сообщение об ошибке компилятора, говорящее это:

The method thenReturn(Class<capture#1-of ? extends Historiable>) in the type OngoingStubbing<Class<capture#1-of ? extends Historiable>> is not applicable for the arguments (Class<ShipmentRequest>)

Но ShipmentRequest является дочерним классом Historiable

Historiable
|
|-ShipmentRequest
|-...
|-...

попробовал это тоже.Вместо использования ShipmentRequest.class в части thenReturn, вызовите этот метод вместо

when(relationService.getModel(eq(collectionName))).thenReturn(method());

    private Class<? extends Historiable> method(){
            return ShipmentRequest.class;
        }

Но ошибка изменится на эту

The method thenReturn(Class<capture#1-of ? extends Historiable>) in the type OngoingStubbing<Class<capture#1-of ? extends Historiable>> is not applicable for the arguments (Class<capture#3-of ? extends Historiable>)

1 Ответ

0 голосов
/ 27 ноября 2018

Требуется дополнительный актерский состав:

     when((Class<ShipmentRequest>)relationService.getModel(eq(collectionName))).thenReturn(ShipmentRequest.class);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...