出现这个错误还是因为自己对Junit测试不熟悉。
Results :
Failed tests: testHello(com.maven.Tests): expected:<Hello[]> but was:<Hello[ maven]>
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.231 s
[INFO] Finished at: 2017-01-07T23:00:09+08:00
[INFO] Final Memory: 7M/17M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project MavenTest: There are test failures.
[ERROR]
[ERROR] Please refer to D:\java8\MavenTest\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
其实看下错误信息就会发现测试用例要求的是返回Hello但你的测试用例的值却是Hello maven
@Test
public void testHello(){
Hello hello = new Hello();
String results = hello.sayHello("maven");
assertEquals("Hello", results);
}
Hello和results不等所以出现错误,
在这里顺便说下assertEquals和assertSame
1)提供的接口数量不完全相同。
assertEquals支持boolean,long,int等等java primitiveType变量。
assertSame只支持Object。
2)比较的逻辑不同,结果可能不同。
assertSame是对象直接比较。assertEquals能利用被比较对象提供的比较逻辑来进行比较。
使得同样的条件下,两者的运行结果不一定相同。
简单解释如下:
assertEquals(Object A, Object B) 的比较逻辑:
如果 A,B都是Null,返回true。否则调用 A.equals(B)来判断。
assertSame(Object A, Object B)的比较逻辑:
以A == B运算的结果来判断。
A.equals(B) 和 A==B 的差别在于。
如果A没有重写java.lang.Object的equals方法,
那么就是两个java对象的内存地址比较,比较结果和 A==B的结果相同。
如果A重写了equals方法(比如GregorianCalendar,BigDecimal类),
那么比较的结果不一定和A==B的结果相同。