@Override public <T> T getLegalForParam(int paramPosition) { Type type = method. getGenericParameterTypes()[paramPosition]; MockeryMetadata mockery = mockeryMetadata .fromParam(providersClass, method, null, paramPosition); checkSupportType.from(providersClass, method, mockery, type); return (T) mockery.legal(); }
@Override public <T> T getIllegalForParam(int paramPosition) { Type type = method. getGenericParameterTypes()[paramPosition]; MockeryMetadata mockery = mockeryMetadata .fromParam(providersClass, method, null, paramPosition); checkSupportType.from(providersClass, method, mockery, type); return (T) mockery.illegal(); }
private void applyValidationsToParams(Method method, Object[] args) { Type[] typeParams = method.getGenericParameterTypes(); for (int i = 0; i < typeParams.length; i++) { Type typeParam = typeParams[i]; MockeryMetadata mockery = getMockeryMetadata .fromParam(mockingClass, method, args, i); if (mockery == null) { String message = Messages.noMockeryFoundOnParam(mockingClass, method, i); throw new RuntimeException(message); } checkSupportType.from(mockingClass, method, mockery, typeParam); Object valueParam = args[i]; mockery.validate(valueParam); } }
@Test public void When_Get_Mockery_Metadata_From_Param_And_Type_Is_Supported_Then_Not_Get_Exception() throws NoSuchMethodException { Method method = Providers.class.getMethod("supportedTypeParam", String.class); Type type = method.getGenericParameterTypes()[0]; MockeryMetadata mockery = getMockeryMetadata .fromParam(Providers.class, method, null, 0); checkSupportTypeUT.from(Providers.class, method, mockery, type); }
@Test public void When_Get_Mockery_Metadata_From_Param_Without_Mockery_Get_Null() throws NoSuchMethodException { Method method = Providers.class.getMethod("mockeryParams", String.class, String.class, String.class); MockeryMetadata mockeryMetadata = getMockeryMetadata .fromParam(Providers.class, method, null, 0); assertNull(mockeryMetadata); }
@Test public void When_Get_Mockery_Metadata_From_Param_With_One_Mockery_Get_Mockery_Metadata() throws NoSuchMethodException { Method method = Providers.class.getMethod("mockeryParams", String.class, String.class, String.class); MockeryMetadata mockeryMetadata = getMockeryMetadata .fromParam(Providers.class, method, null, 2); assertNotNull(mockeryMetadata); }
@Test public void When_Get_Mockery_Metadata_From_Param_With_Multiple_Mockery_Get_Exception() throws NoSuchMethodException { Method method = Providers.class.getMethod("mockeryParams", String.class, String.class, String.class); try { getMockeryMetadata.fromParam(Providers.class, method, null, 1); fail(); } catch (RuntimeException e) { String expectedMessage = Messages .multipleMockeryOnMethodOrParam(Providers.class, method); assertThat(expectedMessage, is(e.getMessage())); } }
@Test public void When_Get_Mockery_Metadata_From_Param_But_Param_Type_Is_Not_Supported_Then_Get_Exception() throws NoSuchMethodException { Method method = Providers.class.getMethod("notSupportedTypeParam", Object.class); Type type = method.getGenericParameterTypes()[0]; try { MockeryMetadata mockery = getMockeryMetadata .fromParam(Providers.class, method, null, 0); checkSupportTypeUT.from(Providers.class, method, mockery, type); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is("When checking ValidMockery on method Providers#notSupportedTypeParam \n" + " an attempt to use it with class java.lang.Object was found. But it is not a supported type for ValidMockery.\n" + "To fix it, use ValidMockery with: [double, class java.lang.Double, float, class java.lang.Float, int, class java.lang.Integer, long, class java.lang.Long, class java.lang.String, class java.lang.Character].\n")); } }