2017-04-04 2 views
-2

필자가 작성한 BinaryTree 클래스의 JUnit 테스트는 어떻게 개발합니까?Junit을 사용하여 바이너리 트리를 테스트하는 방법은 무엇입니까?

Junit에서 이진 트리를 테스트하는 방법을 더 잘 이해할 수 있도록 예제를 제공하거나 알려주십시오.

package binaryTree; 

import javax.xml.soap.Node; 

public class BinaryTree<T extends Comparable<T>> implements BTree<T> { 
    private TreeNode root; 
    Node current = (Node) root; 
    @Override 

    public void insert(T value) { 
     if (root == null) { 
      root = new TreeNode(value); 
      } else if (value.compareTo(value()) < 0) { 
      root.getleft().insert(value); 
      } else { 
      root.right().insert(value); 
      } 
    } 

    @Override 
    public T value() { 
     if (this.root != null) { 
      return (T) this.root.value(); 
     } else { 
      return null; 
     } 
    } 

    @Override 
    public BTree<T> left() { 
     if (this.root != null) { 
      return this.root.getleft(); 
     } else { 
      return null; 
     } 
    } 

    @Override 
    public BTree<T> right() { 
     if (this.root != null) { 
      return this.root.right(); 
     } else { 
      return null; 
     } 
    } 
} 
+0

나는 더미'add' 함수를 테스트하는 간단한 예제를 제공했다. 'BinaryTree' 클래스를 가져 와서 테스트 케이스를 작성해야합니다. 즉, 당신이 무엇을 요구하는지 명확히해야한다고 생각합니다. ** 이진 트리를 테스트하는 방법 ** 또는 간단한 JUnit 테스트를 작성하는 방법 **에 대해 확신이 서지 않습니까? 최소, 완료 및 검증 가능한 예제가 인정됩니다. https://stackoverflow.com/help/mcve – sam

답변

1

확실히 @ tpitsch의 게시물에있는 문서를 읽으십시오. 그러나 여기에 간단한 예제가 있습니다.

import static org.junit.Assert.*; 
import org.junit.Test; 

// This test class contains two test methods 
public class SimpleTest { 

    private int add(int a, int b) { 
     return a + b; 
    } 

    @Test public void test1() throws Exception 
    { 
     System.out.println("@Test1"); 
     assertEquals(add(1, 1), 2); 
    } 

    @Test public void test2() throws Exception 
    { 
     System.out.println("@Test2"); 
     assertEquals(add(100, -30), 70); 
    } 
} 

우리는 함수 add을 테스트 중입니다.

@Test 주석이있는 각 함수는 JUnit 테스트 메소드입니다. 각 테스트 메소드는 별도의 JUnit 테스트로 실행됩니다. 기능 이름 test1()test2()은 중요하지 않습니다.

테스트 방법에서는 add 기능이 예상대로 실행되는지 확인하는 assertions (예 : assertEquals())을 배치 할 수 있습니다.