JUNIT ASSERTNOTNULL示例
时间:2020-02-23 14:41:31 来源:igfitidea点击:
JUNIT 5的org.junit.jupiter.Assertions类提供了不同的静态断言方法来编写测试用例。
请注意,在JUnit 4或者JUnit 3的情况下,我们需要使用JUnit的Org.Junit.Assert类来使用AssertNotnull方法来声明。
assertions.assertnotnull()检查对象是否没有null。
如果,对象是null,它将通过asserterror。
public static void assertNotNull(Object actual) public static void assertNotNull(Object actual, String message) public static void assertNotNull(Object actual, Supplier messageSupplier)
这是一个简单的例子
package org.igi.theitroad;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class AssertNotNullTest {
AssertNotNullTest annt;
public String getCapital(String country){
Map<String,String> countryCapitalMap = new HashMap<String,String>();
countryCapitalMap.put("Netherlands", "Delhi");
countryCapitalMap.put("Nepal", "Kathmandu");
countryCapitalMap.put("China", "Beijing");
countryCapitalMap.put("Bhutan", "Thimphu");
return countryCapitalMap.get(country);
}
@BeforeEach
public void beforeEachTest()
{
annt = new AssertNotNullTest();
}
/*
* Examples for each overloaded methods of assertNotNull
*/
//public static void assertNotNull(Object actual)
@Test
public void testNetherlands(){
String capitalNetherlands=annt.getCapital("Netherlands");
Assertions.assertNotNull(capitalNetherlands);
}
//public static void assertNotNull(Object actual, String message)
@Test
public void testUSA(){
String capitalUSA=annt.getCapital("USA");
//You can pass message as well
Assertions.assertNotNull(capitalUSA,"Capital should not be null");
}
//public static void assertNotNull(Object actual, Supplier messageSupplier)
@Test
public void testBhutan(){
String capitalUSA=annt.getCapital("Bhutan");
//You can pass message as well
Assertions.assertNotNull(capitalUSA,() -> "Capital should not be null");
}
}

