JUnit Assert.assertNotSame()方法示例

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

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) 方法示例

import static org.junit.Assert.assertNotSame;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class AssertNotSameExample {
 private String processMap(final String key){
     final Map<String, String> map = new HashMap<>();
     map.put("key1", "value1");
     map.put("key2", "value2");
     map.put("key3", "value3");
     map.put("key4", "value4");
     map.put("key5", "value5");
     map.put("key6", "value6");
     map.put("key7", "value7");
     map.put("key8", "value8");
     return map.get(key);
 }
 
 @Test
    public void checkSameReferenceTest(){
        final AssertNotSameExample example = new AssertNotSameExample(); 
        assertNotSame(example.processMap("key1"), example.processMap("key2"));
    }
}

输出

相关文章