Maven JBehave:编码故事UTF8

ant*_*ine 5 encoding scenarios utf-8 jbehave maven

我们设法在eclipse中使用JBehave创建和运行具有国际化故事的测试.一切都很顺利.

但是当我们尝试使用maven插件运行它们时,我们无法解决编码问题(例如,不是从故事中读取"scénario",而是"Scéniio":显然是UTF8编码问题) .

有人找到了一种方法让JBehave使用maven插件阅读UTF8中的故事吗?

我们已经尝试过的:

  • 添加-Dfile.encoding = UTF-8选项
  • 使用UTF8更改关键字文件
  • 改变ISO =>中的整个项目编码,但是不适用于需要在UTF8中显示消息的生产部分

我们的Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
...

<properties>
    <jbehave.version>3.6.5</jbehave.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <resource.encoding>UTF-8</resource.encoding>
</properties>

<build>

    <testOutputDirectory>target/classes</testOutputDirectory>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
        </testResource>
        <testResource>
            <directory>src/test/story</directory>
        </testResource>
    </testResources>
    <plugins>
        ...
        <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
                <additionalBuildcommands>
                    <buildcommand>com.google.gdt.eclipse.core.webAppProjectValidator</buildcommand>
                </additionalBuildcommands>
                <additionalProjectnatures>
                    <projectnature>com.google.gwt.eclipse.core.gwtNature</projectnature>
                </additionalProjectnatures>
                <classpathContainers>
                    <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
                </classpathContainers>
                <additionalConfig>
                    <file>
                        <name>.settings/org.eclipse.core.resources.prefs</name>
                        <content>
                           <![CDATA[eclipse.preferences.version=1
                           encoding/<project>=UTF-8]]>
                        </content>
                    </file>
                </additionalConfig>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jbehave</groupId>
            <artifactId>jbehave-maven-plugin</artifactId>
            <version>${jbehave.version}</version>
            <executions>
                <execution>
                    <id>run-stories-as-embeddables</id>
                    <phase>test</phase>
                    <configuration>
                        <scope>test</scope>
                        <includes>
                            <include>**/*Story.java</include>
                        </includes>
                        <ignoreFailureInStories>true</ignoreFailureInStories>
                        <ignoreFailureInView>false</ignoreFailureInView>
                    </configuration>
                    <goals>
                        <goal>run-stories-as-embeddables</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack-jbehave-site-resources</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <overwriteReleases>false</overwriteReleases>
                        <overwriteSnapshots>true</overwriteSnapshots>
                        <artifactItems>
                            <artifactItem>
                                <groupId>org.jbehave.site</groupId>
                                <artifactId>jbehave-site-resources</artifactId>
                                <version>3.1.1</version>
                                <type>zip</type>
                                <outputDirectory>
                                    ${project.build.directory}/jbehave/view
                                </outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
                <execution>
                    <id>unpack-jbehave-reports-resources</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <overwriteReleases>false</overwriteReleases>
                        <overwriteSnapshots>true</overwriteSnapshots>
                        <artifactItems>
                            <artifactItem>
                                <groupId>org.jbehave</groupId>
                                <artifactId>jbehave-core</artifactId>
                                <version>${jbehave.version}</version>
                                <outputDirectory>
                                    ${project.build.directory}/jbehave/view
                                </outputDirectory>
                                <includes>
                                    **\/*.css,**\/*.ftl,**\/*.js
                                </includes>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    ...

    <!-- JBehave Dependencies -->
    <dependency>
        <groupId>org.jbehave</groupId>
        <artifactId>jbehave-core</artifactId>
        <version>${jbehave.version}</version>
    </dependency>

    <!-- Test Frameworks Dependencies -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.8.4</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

小智 3

我在 org.jbehave.core.io.LoadFromClasspath 类的子类化方面取得了一些成功,我在配置中将其用作故事加载器,即

\n\n
MostUsefulConfiguration().useStoryLoader(new LoadFromClasspathUtf8());\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的子类,具有正确的方法覆盖:

\n\n
import java.io.IOException;\nimport java.io.InputStream;\n\nimport org.apache.commons.io.IOUtils;\nimport org.jbehave.core.io.InvalidStoryResource;\nimport org.jbehave.core.io.LoadFromClasspath;\n\npublic class LoadFromClasspathUtf8 extends LoadFromClasspath {\n\n    public LoadFromClasspathUtf8(Class<?> loadFromClass) {\n        super(loadFromClass);\n    }\n\n    public LoadFromClasspathUtf8(ClassLoader classLoader) {\n        super(classLoader);\n    }\n\n    @Override\n    public String loadResourceAsText(String resourcePath) {\n        InputStream stream = resourceAsStream(resourcePath);\n        try {\n            return IOUtils.toString(stream, "UTF-8");\n        } catch (IOException e) {\n            throw new InvalidStoryResource(resourcePath, stream, e);\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我说“我取得了一些成功”,因为当我查看 jbehave 执行的日志时,重音法语字符如 \xc3\xa8、\xc3\xa0、\xc3\xa9 等被替换为 ?,但是,jbehave 仍然将其与使用常规 RegexStoryParser 的步骤正确匹配。我没有花时间去调查这是为什么,但我很满意我的故事现在可以正常工作。

\n\n

我还将 file.encoding 系统属性添加到我的插件配置中,以明确我打算使用 UTF-8 编码。

\n