### Question:
MessageUtils { public static Optional<Method> getTargetMethod(Message m) { Method method; BindingOperationInfo bop = m.getExchange().getBindingOperationInfo(); if (bop != null) { MethodDispatcher md = (MethodDispatcher) m.getExchange().getService().get(MethodDispatcher.class.getName()); method = md.getMethod(bop); } else { method = (Method) m.get("org.apache.cxf.resource.method"); } return Optional.ofNullable(method); } private  MessageUtils(); static boolean isOutbound(Message message); static boolean isFault(Message message); static FaultMode getFaultMode(Message message); static boolean isRequestor(Message message); static boolean isPartialResponse(Message message); static boolean isEmptyPartialResponse(Message message); @Deprecated static boolean isTrue(Object value); static boolean getContextualBoolean(Message m, String key); static boolean getContextualBoolean(Message m, String key, boolean defaultValue); static int getContextualInteger(Message m, String key, int defaultValue); static Object getContextualProperty(Message m, String propPreferred, String propDefault); static boolean isDOMPresent(Message m); static Optional<Method> getTargetMethod(Message m);  }### Answer:
@Test public void getTargetMethodFromBindingOperationInfo() throws Exception { Method method = MessageUtilsTest.class.getMethod("getTargetMethodFromBindingOperationInfo"); Message message = new MessageImpl(); Exchange exchange = new ExchangeImpl(); message.setExchange(exchange); OperationInfo oi = new OperationInfo(); oi.setName(QName.valueOf("getTargetMethod_fromBindingOperationInfo")); BindingOperationInfo boi = new BindingOperationInfo(null, oi); ServiceImpl serviceImpl = new ServiceImpl(); MethodDispatcher md = new SimpleMethodDispatcher(); md.bind(oi, method); serviceImpl.put(MethodDispatcher.class.getName(), md); exchange.put(Service.class, serviceImpl); exchange.put(BindingOperationInfo.class, boi); Optional<Method> optMethod = MessageUtils.getTargetMethod(message); assertTrue(optMethod.isPresent()); assertEquals(method, optMethod.get()); }
@Test public void getTargetMethodFromProperty() throws Exception { Method method = MessageUtilsTest.class.getMethod("getTargetMethodFromProperty"); Message message = new MessageImpl(); message.setExchange(new ExchangeImpl()); message.put("org.apache.cxf.resource.method", method); Optional<Method> optMethod = MessageUtils.getTargetMethod(message); assertTrue(optMethod.isPresent()); assertEquals(method, optMethod.get()); }
@Test public void getTargetMethodNoMethodNoException() throws Exception { Message message = new MessageImpl(); message.setExchange(new ExchangeImpl()); assertFalse(MessageUtils.getTargetMethod(message).isPresent()); }