@Test public void testValidMethod() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("validMethod"); Message<?> result = source.receive(); assertNotNull(result); assertNotNull(result.getPayload()); assertEquals("valid", result.getPayload()); }
@Test(expected = MessagingException.class) public void testNoMatchingMethodName() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("noMatchingMethod"); source.receive(); }
@Test(expected = MessagingException.class) public void testInvalidMethodWithArg() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("invalidMethodWithArg"); source.receive(); }
@Test(expected = MessagingException.class) public void testInvalidMethodWithNoReturnValue() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("invalidMethodWithNoReturnValue"); source.receive(); }
@Test public void testNullReturningMethodReturnsNullMessage() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("nullReturningMethod"); Message<?> message = source.receive(); assertNull(message); }
@Test public void testHeaderExpressions() { Map<String, Expression> headerExpressions = new HashMap<String, Expression>(); headerExpressions.put("foo", new LiteralExpression("abc")); headerExpressions.put("bar", new SpelExpressionParser().parseExpression("new Integer(123)")); MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("validMethod"); source.setHeaderExpressions(headerExpressions); Message<?> result = source.receive(); assertNotNull(result); assertNotNull(result.getPayload()); assertEquals("valid", result.getPayload()); assertEquals("abc", result.getHeaders().get("foo")); assertEquals(123, result.getHeaders().get("bar")); }