JAVA学习笔记_Junit测试简单使用_assertEquals

时间:2022-05-24 16:58:43

一开始,是通过输出结果判断输出结果是否正确来判断,console虽然输出是true,false 和预期的是一样,但是junit显示都是成功的,并没有出现报错,达不到使用junit测试的效果。

junit 出现failures 和 errors 的情况:

      Failure指的是由于预期的结果与实际运行的测试的结果不同而导致的,例如当使用assertEquals()或其它assertXXX()方法断言失败时,就会报出Failure,如果发现Faulure,你就要去检查你的测试方法或者是被测试方法中编写的逻辑是否有误。

Error指的是编写程序时没有考虑到的问题。在执行测试的断言之前,程序就因为某种类型的意外而停止,比喻说我们在操作数组的时候,因为存取超出索引会引发ArrayIndexOutOfBoundsException,这个时候程序就会报出Error,程序将无法运行下去,提前结束,这个时候你要检查被测试方法中是不是有欠缺考虑到地方。

测试代码:

package com.keving.test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.keving.dao.TypeDao;
import com.keving.po.NoteType;

public class TypeDaoTest {
TypeDao typeDao = new TypeDao();
NoteType noteType = new NoteType();
@Before
public void init(){
noteType.setTypeName("SS");
noteType.setTypeId(1);
}

@Test
public void testUpdateType() {
// System.out.println(typeDao.updateType(noteType ));
assertEquals(true, typeDao.updateType(noteType ));
}
}

JAVA学习笔记_Junit测试简单使用_assertEquals

asserEquals 和 assertSamse

assertEquals(Object A,  Object B) 
A.equals(B)

assertSame(Object A, Object B)
A == B