2017-01-23 1 views
0

클라이언트가 제공 한 행과 열이 행렬 배열의 경계에 있는지 확인하기 위해 간단한 점검을 시도하고 있습니다. 왜 이런 일이 될 수도에 junit.framework.AssertionFailedError자바 | 행렬의 경계 검사

어떤 생각 그러나 나는

실패가 계속?

시험기 :

public void testBoundsForGet() { // m is a 4x3 matrix 
     try { 
      m.get(-1, 2); 
      fail("get should not have succeeded"); 
     } 
     catch(MatrixException ex) { 
      assertTrue(ex.getMessage().equals("Row index (-1) out of bounds")); 
     } 

체크 경계 방법 :

protected void checkBounds(int row, int column) { 
     if((this.numRows < row) || (row < 0)){ 
      throw new MatrixException(String.format("The row (%s) is out of range", row)); 
     } 

     if((this.numColumns < column) || (column < 0)){ 
      throw new MatrixException(String.format("The column (%s) is out of range", column)); 
     } 

    } 
+1

당신이 던지는하고 예외가 행 (-1) range'를 벗어'라고하지만, 당신은'행 지수는 말한다 확인하는 주장을 작성하는 (-1) outs bounds' – Stik

+0

Stik의 덧글에 추가하기 : 나는 문자열 평등을 검사하기 위해 assertEquals로 전환 할 것입니다. 그것은 당신에게 그것이 무엇을 기대하고 당신에게 진실/허위를 제공하는 대신에 무엇을 얻었는지 보여줄 수있는 이점을 가지고 있습니다. –

+0

의견을 주셔서 감사합니다. 나는 지금 수업을 듣고 있지만 내 고침을 게시 할 것입니다. –

답변

-1

I 네 총 시험보고 싶어 : 이하 상기 0의 최소값보다 로우와 컬럼에 대해 각각 하나씩 문제의 행렬의 최대치 다음과 같이 나는 그 테스트를 작성하는 것 :

@Test(expected = MatrixException.class) 
public void testCheckBounds_NegativeRow() { // m is a 4x3 matrix 
    // You must have defined m somewhere else as a member variable. 
    m.get(-1, 2); 
} 

@Test(expected = MatrixException.class) 
public void testCheckBounds_LargerThanMaxRow() { // m is a 4x3 matrix 
    // You must have defined m somewhere else as a member variable. 
    m.get(10, 2); 
} 

@Test(expected = MatrixException.class) 
public void testCheckBounds_NegativeColumn() { // m is a 4x3 matrix 
    // You must have defined m somewhere else as a member variable. 
    m.get(1, -2); 
} 

@Test(expected = MatrixException.class) 
public void testCheckBounds_LargerThanMaxColumn() { // m is a 4x3 matrix 
    // You must have defined m somewhere else as a member variable. 
    m.get(1, 20); 
} 

@Test 
public void testCheckBounds_Success() { 
    // what should you asset here? It's not a useful method. I'd return boolean 
    m.get(1,1); 
} 
+1

죄송합니다.이 항목을 투표 한 사람은 JUnit의 올바른 관용을 모르고있었습니다. – duffymo