开发者

jUnit, Testing the "new" reference

开发者 https://www.devze.com 2023-04-04 10:47 出处:网络
Suppose you have a method like public Something copy () { return new Something(); } In your jUnit, you have

Suppose you have a method like

public Something copy () {
  return new Something();
}

In your jUnit, you have

Something s1 = new Something();
Something s2 = s1.copy()

Other then

assertTrue (s1 != s2);
assertTrue (s2 开发者_开发百科!= null);
assertTrue (s1.toString().equals(s2.toString)));

Is there any additional way you can confirm that a NEW reference is returned by a copy()?


The first one is sufficient to check they're not the same, but there's also an assertion built into JUnit:

assertNotSame(s1, s2);


Sure use Assert.assertTrue(s1, s2)

Moreover this is the way to compare. It calls Something.equals() internally and throws exception is equals returns false.


If you want to use Hamcrest-style: sameInstance


Another great way for testing things like these is the Mockito test framework (http://code.google.com/p/mockito/)

It allows you to verify that only certain things get called N number of times, or not at all with something like:

Mockito.verify(mockOne, times(1)).mockedFunction();

Which verifies that mockOne had its mockedFunction called exactly once.

0

精彩评论

暂无评论...
验证码 换一张
取 消