2012-07-25

Тестирование мапингов в SpringMVC

После нашего воркшопа в Одессе мы получили ряд интересных вопросов. Об одном из которых, а точнее о его решение я бы хотел рассказать.
Задача: протестировать @RequestMapping для контроллера
Документация говорит нам (а она как я люблю говорить у Спринга отличная):
"Prior to Spring 3.1, type and method-level request mappings were examined in two separate stages -- a controller was selected first by the DefaultAnnotationHandlerMapping and the actual method to invoke was narrowed down second by the AnnotationMethodHandlerAdapter.
With the new support classes in Spring 3.1, the RequestMappingHandlerMapping is the only place where a decision is made about which method should process the request. Think of controller methods as a collection of unique endpoints with mappings for each method derived from type and method-level @RequestMapping information.
"
Правда нет конкретной рекомендации. Зато Oliver Girke на StackOverflow рекомендовал.
Итого имеем:
Поднятие контекста в тесте не требуется. Mockito в свою очередь порадовал вкусняшками типа @Mock. Я думаю что все заметили хак "request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);". Промучившись минут 15, а также немного подебажив (как говорит Женя дебажить Спринг одно удовольствие) я засабмитил багу и с чистой совестью поставил костыль.
В следующей серии о Spring Data и GenericDao.
Всего всего - ваши me & eugene

1 comment: