프로젝트

일반

사용자정보

5장 JUnit 실습

Prof. Jong Min Lee이(가) 16일 전에 추가함

  • SimpleStartup.java (복사하여 사용)
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cse.oop2.ch05.simplestartup;

/**
 * p.148
 *
 * @author jongmin
 */
public class SimpleStartup {

    int[] locationCells;
    int numOfHits = 0;

    public void setLocationCells(int[] locs) {
        locationCells = locs;
    }

    public String checkYourself(String stringGuess) {
        int guess = Integer.parseInt(stringGuess);
        String result = "miss";
        for (int cell : locationCells) {
            if (guess == cell) {
                result = "hit";
                numOfHits++;
                break;
            }
        }
        if (numOfHits == locationCells.length) {
            result = "kill";
        }
        System.out.println(result);
        return result;
    }
}
  • SimpleStartupTest.java (JUnit5 사용하여 만들 코드)
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/UnitTests/JUnit5TestClass.java to edit this template
 */
package cse.oop2.ch05.simplestartup;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

/**
 *
 * @author Prof. Jong Min Lee
 */
public class SimpleStartupTest {

    public SimpleStartupTest() {
    }

    @BeforeAll
    public static void setUpClass() {
    }

    @AfterAll
    public static void tearDownClass() {
    }

    @BeforeEach
    public void setUp() {
    }

    @AfterEach
    public void tearDown() {
    }

    /**
     * Test of setLocationCells method, of class SimpleStartup.
     */
    @Test
    public void testSetLocationCells() {
        System.out.println("setLocationCells");
        int[] locs = {2, 3, 4};
        SimpleStartup instance = new SimpleStartup();
        instance.setLocationCells(locs);
        assertArrayEquals(locs, instance.locationCells);
    }

    /**
     * Test of checkYourself method, of class SimpleStartup.
     */
    @Test
    public void testCheckYourself() {
        System.out.println("checkYourself");
        String stringGuess = "2";  // 수정
        SimpleStartup instance = new SimpleStartup();

        int[] locs = {2, 3, 4};  // 추가
        instance.setLocationCells(locs);

        String expResult = "hit ";
        String result = instance.checkYourself(stringGuess);
        assertEquals(expResult, result);
    }

}