• 검색 결과가 없습니다.

AI CAUJINHYUK JO JUNIT

N/A
N/A
Protected

Academic year: 2022

Share "AI CAUJINHYUK JO JUNIT"

Copied!
25
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

JUNIT

AI CAU

JINHYUK JO

(2)

LIST

What is JUnit?

Features of JUnit How to JUnit3 What is JUnit4?

Hamcrest in JUnit4

(3)

WHAT IS JUNIT

Junit is..

Framework for TDD Feature

Test Fixture

Assertions

Test Runner

(4)
(5)

JUNIT 설치

(6)

JUNIT 설치

(7)

JUNIT3

TestCase를 상속

AccountTest extends TestCase

TestMethod Name은 test 시작

testGetAddress()

testCalculateValue()

(8)

JUNIT3

Test Fixture

setUp()

각 test Method가 실행되기 전에 실행이 됨

tearDown()

각 test Method가 실행된 후에 실행이 됨

(9)

JUNIT3

구조

Public class DaoTest extends TesetCase{

Connection connection;

protected void setUp() throws Exception{

connection = Connection.getConnection();

}

public void testA() … public void testB() …

protected void tearDown() throws Exception{

connection.close();

} }

setUp -> testA -> tearDown ->setUp -> testB -> tearDown

 실행순서

(10)

JUNIT3

Public class DaoTest extends TesetCase{

Connection connection;

public DaoTest (){

System.out.println(“생성자 실행”);

}

protected void setUp() throws Exception{

connection = Connection.getConnection();

}

public void testA() … public void testB() …

protected void tearDown() throws Exception{

connection.close();

} }

만일 생성자를 만든다면…

생성자 실행 생성자 실행

(11)

JUNIT3

Assertions

assertEquals(expected, actual)

기대하는 값과 실제 값을 비교하여 같으면 테스트 성공 (기본값이 외 객체비교가능)

assertTrue(boolean condition)

조건이 true이면 테스트 성공

assertNull(Object object)

객체가 null이면 테스트성공

assertSame(Object expected,Object actual)

기대하는 객체와 결과값 비교하여 같으면 성공(같은 인스턴스)

Fail

해당 케이스는 즉시 실패

테이스케이스를 구현하지 못하거나 준비 중일 경우 fail을 사용 해당 케이스는 건너뛰게 하게함.

(12)

JUNIT3

Test Runner

 Test case들을 실행시키기 위한 실행자(runner)

 JAVA통합개발환경(IDE)에서 내장지원

 swingUI, textUI, AwtUI 의 테스트 실행클래스를 제공

Public static void main(String[] args){

junit.swingui.TestRunner.run(DisplayTest.class) junit.textui.TestRunner.run(DisplayTest.class) junit.awtui.TestRunner.run(DisplayTest.class) java –CP junit.jar; . Junit.textui.TextRunner Displaytest

(13)

JUNIT3

Test Suite

 여러개 테스트클래스를 한꺼번에 테스트 할 수 있음

 메소드는 반드시 public static Test suite()

 addTestSuite(ClassName.class) addTestSuite(TestSuite.suite())

Public static Test suite(){

TestSuite suite = new TestSuite();

suite.addTestSuite(DisplayTest.class); //테스트케이스추가 suite.addTestSuite(DisplayReceiverTest.class);

suite.addTest(AnotherSuiteTest.suite());//다른 테이스 스위트추가 return suite;

}

(14)

JUNIT4

Feature

Java5 이상에서 이용가능

junit.framework.TeseCase를 상속 안함

org.junit.* 과 org.junit.Assert.* 을 import

Annotation 사용

(15)

JUNIT4

Annotation

메소드의 명명규칙 제약해소

testXXX() -> @Test

setUp() -> @Before

tearDown() -> @After

유연해진 픽스쳐(fixture)

@BeforeClass, @AfterClass

테스트메소드의 갯 수 상관없이 한번씩만 실행됨

(16)

JUNIT4

Exception

Use: @Test(expected=Exception클래스명.class)

특정 예외발생을 테스트

특정 예외가 발생해야 테스트케이스 성공

Timeout

Use: @Test(timeout=100)

테스트에 시간제한을 건다.

주어진 시간에 테스트완료를 못하면 실패

(17)

JUNIT4

Ingnore

Use: @Ignore

테스트에서 제외

RunWith

Use: @RunWith(Suite.class)

Junit의 기본 TestRunner대신, 지정된 클래스를 이용

SuiteClasses

Use: SuiteClasses({TestCase1.class,TestCase2.class})

여러 개의 테스트 클래스를 한 꺼번에 수행

(18)

HAMCREST

문맥적으로 자연스럽게 도와준다 비교표현식(>,<,>=,<=,,,) 필터링,유효성검증등

테스트 케이스 작성시 개발자의 의도를 정확히 알 수 있음.

JUnit4.4 라이브러리에 포함

(19)

HAMCREST

비교-1

 assertEquals(“YoungJim”,customer.getName());

 assertThat(customer.getName(), is(“YoungJim”));

비교-2

 assertNotNull(resource.newConnection());

 assertThat(resource.newConnection(), is(notNullValue())

비교-3

 assertTrue(account.getBalance()>0);

 assertThat(account.getBalance(), isGreaterThan(0));

(20)

DEMO

package com.junit;

import java.util.List;

public class PublicExam {

public int sumValues(List<Integer> list) { int sum = 0;

for(Integer i : list) { sum += i;

}

return sum;

} }

(21)

DEMO

(22)
(23)

DEMO

(24)

PUBLICEXAMTEST.JAVA

public class PublicExamTest { private static PublicExam publicCase;

@BeforeClass

public static void beforeSetup() { publicCase = new PublicExam();

}

@Test

public void testSumValues() {

List<Integer> list = new ArrayList<Integer>();

list.add(1);

list.add(2);

int actual = publicCase.sumValues(list);

Assert.assertEquals(3, actual);

} }

(25)

DEMO

참조

관련 문서

afx_msg void OnCreate()(LPCREATESTRUCT); // 메시지 핸들러 prototype afx_msg void OnLButtonDown(UINT, CPoint); // 메시지 핸들러 prototype DECLARE_MESSAGE_MAP()

Step 1 Press [SETUP] to enter &lt;SETUP&gt; page Step 2 Keep test clip or cable open circuit?. When correction is done, the data

void dfsTraversal(DepthFirstSearch *pDFS, Vertex *pV, Vertex *pTarget, VertexList *pVTL);..

자바에서 처리하는 예외는 모두 Exception 클래스에서 파 Exception 클래스는 Throwable 클래스의 하위 클래스임 결국 모든 예외 클래스가 Throwable의 하위

[r]

동작인식(AI) 놀이를

강한 AI 는 단순한 컴퓨터가 아니라 프로그램된 컴퓨터 정신이라 는 것이 깃든 AI 를 말한다.. 인간의 사고와 같이 컴퓨터

인공지능(AI)에게 내가