@MessageExceptionHandler public void handleExceptionWithHandlerMethodArg(IllegalStateException ex, HandlerMethod handlerMethod) { this.method = "handleExceptionWithHandlerMethodArg"; this.arguments.put("handlerMethod", handlerMethod); assertEquals("my cause", ex.getMessage()); }
@Test void invalidExpressionEvaluationType() { String methodName = "nonBooleanOrStringExpression"; IllegalStateException exception = assertThrows(IllegalStateException.class, () -> condition.evaluateExecutionCondition(buildExtensionContext(methodName))); Method method = ReflectionUtils.findMethod(getClass(), methodName); assertThat(exception.getMessage(), is(equalTo("@DisabledIf(\"#{6 * 7}\") on " + method + " must evaluate to a String or a Boolean, not java.lang.Integer"))); }
@Test void unsupportedStringEvaluationValue() { String methodName = "stringExpressionThatIsNeitherTrueNorFalse"; IllegalStateException exception = assertThrows(IllegalStateException.class, () -> condition.evaluateExecutionCondition(buildExtensionContext(methodName))); Method method = ReflectionUtils.findMethod(getClass(), methodName); assertThat(exception.getMessage(), is(equalTo("@DisabledIf(\"#{'enigma'}\") on " + method + " must evaluate to \"true\" or \"false\", not \"enigma\""))); }
private void testMetaClass(String xmlFile) { // expect the exception we threw in the custom metaclass to show it got invoked try { ApplicationContext ctx = new ClassPathXmlApplicationContext(xmlFile); Calculator calc = (Calculator) ctx.getBean("delegatingCalculator"); calc.add(1, 2); fail("expected IllegalStateException"); } catch (IllegalStateException ex) { assertEquals("Gotcha", ex.getMessage()); } }
private void assertContextConfigEntriesAreNotUnique(Class<?> testClass) { try { buildContextHierarchyMap(testClass); fail("Should throw an IllegalStateException"); } catch (IllegalStateException e) { String msg = String.format( "The @ContextConfiguration elements configured via @ContextHierarchy in test class [%s] and its superclasses must define unique contexts per hierarchy level.", testClass.getName()); assertEquals(msg, e.getMessage()); } }
@Test public void noCacheResolved() { Method method = ReflectionUtils.findMethod(SimpleService.class, "noCacheResolved", Object.class); try { this.simpleService.noCacheResolved(new Object()); fail("Should have failed, no cache resolved"); } catch (IllegalStateException ex) { assertTrue("Reference to the method must be contained in the message", ex.getMessage().contains(method.toString())); } }
private void assertExpressionIsBlank(String methodName) { IllegalStateException exception = assertThrows(IllegalStateException.class, () -> condition.evaluateExecutionCondition(buildExtensionContext(methodName))); assertThat(exception.getMessage(), containsString("must not be blank")); }
@Test public void multipleCacheManagerBeans() { try { load(MultiCacheManagerConfig.class); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("bean of type CacheManager")); } }
@Test public void noCacheManagerBeans() { try { load(EmptyConfig.class); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("no bean of type CacheManager")); } }
private void assertExceptionContains(String msg) throws Exception { try { listener.beforeTestMethod(testContext); fail("Should have thrown an IllegalStateException."); } catch (IllegalStateException e) { // System.err.println(e.getMessage()); assertTrue("Exception message should contain: " + msg, e.getMessage().contains(msg)); } }
@Test public void noCacheManagerBeans() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(EmptyConfig.class); try { ctx.refresh(); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("no bean of type CacheManager")); } }
@Test public void multipleCacheManagerBeans() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(MultiCacheManagerConfig.class); try { ctx.refresh(); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("no unique bean of type CacheManager")); } }
@Test public void maxSessions() { IntStream.range(0, 10000).forEach(i -> insertSession()); try { insertSession(); fail(); } catch (IllegalStateException ex) { assertEquals("Max sessions limit reached: 10000", ex.getMessage()); } }
@Test public void cannotResolveArg() throws Exception { try { getInvocable(Integer.class, String.class).invokeForRequest(request, null); fail("Expected exception"); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("Could not resolve parameter [0]")); } }
@Test public void testFriendlyErrorOnNoLocationClassMatching() { AspectJExpressionPointcut pc = new AspectJExpressionPointcut(); try { pc.matches(ITestBean.class); fail(); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("expression")); } }
@Test public void testFriendlyErrorOnNoLocation2ArgMatching() { AspectJExpressionPointcut pc = new AspectJExpressionPointcut(); try { pc.matches(getAge, ITestBean.class); fail(); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("expression")); } }
@Test public void testFriendlyErrorOnNoLocation3ArgMatching() { AspectJExpressionPointcut pc = new AspectJExpressionPointcut(); try { pc.matches(getAge, ITestBean.class, (Object[]) null); fail(); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("expression")); } }
@Test public void shouldNotSupportNullConvertibleTypesFromNonConditionalGenericConverter() { GenericConverter converter = new NonConditionalGenericConverter(); try { conversionService.addConverter(converter); fail("Did not throw IllegalStateException"); } catch (IllegalStateException ex) { assertEquals("Only conditional converters may return null convertible types", ex.getMessage()); } }
@Test // Based on SPR-13917 (spring-web) public void invocationErrorMessage() throws Exception { this.resolvers.addResolver(new StubArgumentResolver(double.class)); try { Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0.0)).method(); invoke(new Handler(), method); fail(); } catch (IllegalStateException ex) { assertThat(ex.getMessage(), containsString("Illegal argument")); } }
@Test public void startAsyncAfterCompleted() throws Exception { this.asyncRequest.onComplete(new AsyncEvent(new MockAsyncContext(this.request, this.response))); try { this.asyncRequest.startAsync(); fail("expected exception"); } catch (IllegalStateException ex) { assertEquals("Async processing has already completed", ex.getMessage()); } }