LinkToHandler extends ForwardingMap<Class<?>, Object> { @Override public LinkMethod get(Object key) { return new LinkMethod((Class<?>) key); } LinkToHandler(ServletContext context, Router router); void start(); @Override LinkMethod get(Object key);  }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenInvocationIsIncomplete() { handler.get(TestController.class).toString(); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenInvokingInexistantMethod() { handler.get(TestController.class).get("nonExists").toString(); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenMethodIsAmbiguous() { handler.get(TestController.class).get("method").toString(); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenUsingParametersOfWrongTypes() { handler.get(TestController.class).get("method").get(123).toString(); }
@Test public void shouldReturnWantedUrlWithoutArgs() { when(router.urlFor(TestController.class, anotherMethod, new Object[2])).thenReturn("/expectedURL"); String uri = handler.get(TestController.class).get("anotherMethod").toString(); assertThat(uri, is("/path/expectedURL")); }
@Test public void shouldReturnWantedUrlWithParamArgs() { String a = "test"; int b = 3; when(router.urlFor(TestController.class, method2params, new Object[]{a, b})).thenReturn("/expectedURL"); String uri = handler.get(TestController.class).get("method").get(a).get(b).toString(); assertThat(uri, is("/path/expectedURL")); }
@Test public void shouldReturnWantedUrlWithPartialParamArgs() { String a = "test"; when(router.urlFor(TestController.class, anotherMethod, new Object[]{a, null})).thenReturn("/expectedUrl"); String uri = handler.get(TestController.class).get("anotherMethod").get(a).toString(); assertThat(uri, is("/path/expectedUrl")); }
@Test public void shouldReturnWantedUrlForOverrideMethodWithParamArgs() throws NoSuchMethodException, SecurityException { String a = "test"; when(router.urlFor(SubGenericController.class, SubGenericController.class.getDeclaredMethod("method", new Class[]{String.class}), new Object[]{a})).thenReturn("/expectedURL"); String uri = handler.get(SubGenericController.class).get("method").get(a).toString(); assertThat(uri, is("/path/expectedURL")); }
@Test public void shouldReturnWantedUrlForOverrideMethodWithParialParamArgs() throws SecurityException, NoSuchMethodException { String a = "test"; when(router.urlFor(SubGenericController.class, SubGenericController.class.getDeclaredMethod("anotherMethod", new Class[]{String.class, String.class}), new Object[]{a, null})).thenReturn("/expectedURL"); String uri = handler.get(SubGenericController.class).get("anotherMethod").get(a).toString(); assertThat(uri, is("/path/expectedURL")); }
@Test public void shouldUseExactlyMatchedMethodIfTheMethodIsOverloaded() { String a = "test"; when(router.urlFor(TestController.class, method1param, a)).thenReturn("/expectedUrl"); String uri = handler.get(TestController.class).get("method").get(a).toString(); assertThat(uri, is("/path/expectedUrl")); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenPassingMoreArgsThanMethodSupports() { String a = "test"; int b = 3; String c = "anotherTest"; handler.get(TestController.class).get("anotherMethod").get(a).get(b).get(c).toString(); }