JUnit Assert.assertNotSame()方法示例

x33g5p2x  于2022-10-06 转载在 其他  
字(1.1k)|赞(0)|评价(0)|浏览(654)

JUnit 4教程

assertNotSame()方法属于JUnit 4 org.junit.Assert class。在JUnit 5中,所有的JUnit 4断言方法都移到org.junit.jupiter.api.Assertions类中。

void org.junit.Assert.assertNotSame(Object unexpected, Object actual)

断言两个对象不指代同一个对象。如果它们确实指的是同一个对象,会抛出一个没有消息的断言错误。 

参数。

unexpected - 你不期待的对象

  • actual - 与意外的对象进行比较。

Assert.assertNotSame(Object unexpected, Object actual) 方法示例

  1. import static org.junit.Assert.assertNotSame;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.junit.Test;
  5. public class AssertNotSameExample {
  6. private String processMap(final String key){
  7. final Map<String, String> map = new HashMap<>();
  8. map.put("key1", "value1");
  9. map.put("key2", "value2");
  10. map.put("key3", "value3");
  11. map.put("key4", "value4");
  12. map.put("key5", "value5");
  13. map.put("key6", "value6");
  14. map.put("key7", "value7");
  15. map.put("key8", "value8");
  16. return map.get(key);
  17. }
  18. @Test
  19. public void checkSameReferenceTest(){
  20. final AssertNotSameExample example = new AssertNotSameExample();
  21. assertNotSame(example.processMap("key1"), example.processMap("key2"));
  22. }
  23. }

输出

相关文章