简体   繁体   English

在Spring Webflow单元测试中设置messages.properties

[英]Setting messages.properties in a Spring Webflow unit test

I'm using Spring Web Flow 2.0.7, and just started setting up my unit tests. 我正在使用Spring Web Flow 2.0.7,并且刚刚开始设置我的单元测试。 I've got my flows directory, containing the flow xml files and my messages.properties files on my classpath. 我有我的流程目录,在我的类路径上包含流程xml文件和messages.properties文件。

It's loading fine, but it doesn't seem to be automatically picking up the messages.properties file for the flow, and thus my validation unit tests are failing because it can't resolve a message for the given code. 它的加载情况很好,但似乎并没有自动为该流程选择me​​ssages.properties文件,因此我的验证单元测试失败了,因为它无法解析给定代码的消息。

Here's the relevant part of the exception: 这是例外的相关部分:

org.springframework.context.NoSuchMessageException: No message found under code 'error_alnum_char_count_not_atleast' for locale 'en_US'.

This seems like a fairly common scenario, to use the codes resolved in messages.properties to generate error messages. 使用在messages.properties中解析的代码来生成错误消息,这似乎是一种很常见的情况。 So...what's the recommended way to load the messages.properties file? 那么...推荐的加载messages.properties文件的方式是什么?

I had to create a ValidationContext to use inside custom web flow validators. 我必须创建一个ValidationContext才能在自定义Web流验证器中使用。 I solved using reflection 我用反射解决了

protected MessageSource validationMessages;

/**
 * Creates a validationContext
 * We have to use reflection because there's no way to set
   the messageSource inside {@link MockRequestContext} otherwise
 *
 * @return {@link ValidationContext}
 * @throws IllegalAccessException
 * @throws NoSuchFieldException
 */
protected ValidationContext getValidationContext() throws IllegalAccessException, NoSuchFieldException {
    final MockRequestContext requestContext = new MockRequestContext();
    requestContext.setExternalContext(context);
    final DefaultMessageContext defaultMessageContext = new DefaultMessageContext(validationMessages);
    final Field messageContextField = MockRequestContext.class.getDeclaredField("messageContext");
    final boolean accessible = messageContextField.isAccessible();
    messageContextField.setAccessible(true);
    messageContextField.set(requestContext, defaultMessageContext);
    messageContextField.setAccessible(accessible);
    return new DefaultValidationContext(requestContext, "validateInfo", mock(MappingResults.class));
}

I know how old this question is, but having found a solution I thought I'd post it for others: 我知道这个问题有多老了,但是找到了解决方案之后,我想我应该把这个问题发布给其他人:

Object obj = builderContext.getApplicationContext()
                .getBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME);
        obj = builderContext.getApplicationContext().getBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME);
        if (obj != null && obj instanceof StaticMessageSource) {
            StaticMessageSource messageSource = (StaticMessageSource) obj;
            messageSource.setUseCodeAsDefaultMessage(true);
        }

The above code, placed into your configureFlowBuilderContext method, will resolve all messages to the code itself. 上面的代码放入您的configureFlowBuilderContext方法中,将所有消息解析为代码本身。 This does not resolve your messages correctly, but will prevent the failure you listed and allow your tests to pass. 这不能正确解决您的消息,但是会阻止您列出的失败并允许测试通过。

If there's a better way, I'd really be interested in hearing about it, as this is really only a workaround. 如果有更好的方法,我真的很想听听它,因为这实际上只是一个解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM