mvn test的工作方式与IntelliJ

时间:2015-05-18 11:39:17

标签: java maven intellij-idea

我有这个单元测试:

@Test
public void shouldReturnTheSameStringSinceHasNoAccents() {
    String data = "Hola";
    String expected = "Hola";

    String actual = normalizer.stripAccents(data);

    assertEquals(expected, actual);
}

@Test
public void shouldStripAccent() {
    String data = "¿Qué haces?";
    String expected = "¿Que haces?";

    String actual = normalizer.stripAccents(data);

    assertEquals(expected, actual);
 }

我正在使用Maven Surefire插件。如果我通过终端mvn clean package test,我在第二次测试中收到错误:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.grupogimeno.parse.util.StringNormalizerTests
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec <<< FAILURE!

Results :

Failed tests:
  shouldStripAccent(com.grupogimeno.parse.util.StringNormalizerTests)

Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

另一方面,我在IntelliJ中有这个基于Maven的运行/调试配置:

当我点击运行时,两个测试都通过了!

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.grupogimeno.parse.util.StringNormalizerTests
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.093 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

两种方式都使用M2_HOME env var来执行mvn

发生了什么事?

感谢。

1 个答案:

答案 0 :(得分:1)

可能是由源代码编码引起的,请参阅https://maven.apache.org/general.html#encoding-warning并将以下属性添加到您的pom中:

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>