2016-05-14 44 views
1

我使用eclipse並使用Junit 4測試用例製作了兩個測試類。 每個測試類都能正常工作,現在我需要製作一個TestSuite來運行兩個測試類。 2個測試類的名稱是:GuessManager.class & AsciiPicutre.class 所以我做到了,加入了一個名爲的TestSuite這裏類是類代碼:每個Junit測試都能正常工作,但是當試圖製作套件時 - 沒有任何工作

package hangman; 

import org.junit.runner.RunWith; 
import org.junit.runners.Suite; 

@RunWith(Suite.class) 
@Suite.SuiteClasses({GuessManager.class, AsciiPicture.class }) 
public class TestSuite 
{ 

} 

當我嘗試運行它,我得到我的兩個測試類的初始化錯誤 這裏是相關的截圖: enter image description here

任何想法我做錯了什麼?我相信這是關於我的TestSuite類的東西,因爲每個測試都可以直接運行它們。 另外測試不是基於另一個。

編輯: 這裏是我的兩個測試類:

package hangman; 

import static org.junit.Assert.*; 

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.PrintStream; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 


public class AsciiPictureTest { 

    AsciiPicture canvas; 
    AsciiPicture pic; 

    @Before 
    public void setUp() throws Exception { 
     canvas = new AsciiPicture(6, 4, '0'); 
     pic = new AsciiPicture(1, 1, '*'); 

    } 

    @After 
    public void tearDown() throws Exception { 
    } 

    @Test 
    public void testOverlay() throws IOException { 
     for (int i = 0; i < canvas.height; i++) { 
      for (int j = 0; j < canvas.width; j++) { 

       // Initial the expected result as char 2d - array 
       char[][] expected = CreatePicture(canvas.width, canvas.height, '0'); 

       // Put a '*' in specific location to imitate the result of the overlay function 
       expected[i][j] = '*';  

       // Initial the canvas to 'blank' canvas 
       canvas = new AsciiPicture(6, 4, '0'); 

       // Making the actual overlay at index[i][j] 
       canvas.overlay(pic, j, i, ' '); 

       // Checking if the expected is equal to actual by checking each one of the characters 
       asciiPictureCharArrAssertEquals(canvas, expected); 
      } 
     } 
    } 
    @Test 
    public void testPrint() throws IOException { 
     // Defining ByteArratOutputStream for "catching" the data that will be printed 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 

     // Setting the out of "System" to ByteArrayOutputStream we just defined 
     System.setOut(new PrintStream(out)); 

     // Printing the data to the new configured output 
     canvas.print(System.out); 

     // Defining and building the expected String 
     String expectedOutput = "000000" + System.lineSeparator() + 
           "000000" + System.lineSeparator() + 
           "000000" + System.lineSeparator() + 
           "000000" + System.lineSeparator(); 

     // Checking if what was printed is equal to what should be printed 
     assertEquals(out.toString(), expectedOutput); 

    } 
    public char[][] CreatePicture(int width, int height, char bgChar) { 
     char[][] picture = new char[height][]; 
     for (int i = 0; i < height; ++i) { 
      picture[i] = new char[width]; 
      for (int j = 0; j < width; ++j) 
       picture[i][j] = bgChar; 
     } 
     return picture; 
    } 
    public boolean asciiPictureCharArrAssertEquals(AsciiPicture actual, char[][] expected) { 
     for (int i = 0; i < expected.length; i++) { 
      for (int j = 0; j < expected[0].length; j++) { 
       char expectedChar = expected[i][j]; 
       char actualChar = actual.get(j, i); 
       assertEquals(expectedChar, actualChar); 
      } 
     } 
     return true; 
    } 

} 

和第二測試:

package hangman; 

import static org.junit.Assert.*; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 

import com.sun.net.httpserver.Authenticator.Success; 

import hangman.GuessManagerContract.GuessResponse; 

/** 
* @author 
* 
*/ 
public class GuessManagerTest { 

    private GuessManager gm; 
    private static final String CORRECT_WORD = "Test"; 
    private static final char INCORRECT_LETTER = 'k'; 
    private static final char CORRECT_LETTER = 't'; 
    private static final int NUMBER_OF_GUESSES = 10; 

    /** 
    * @throws java.lang.Exception 
    */ 
    @Before 
    public void setUp() throws Exception { 
     gm = new GuessManager(CORRECT_WORD , NUMBER_OF_GUESSES); 
    } 

    /** 
    * @throws java.lang.Exception 
    */ 
    @After 
    public void tearDown() throws Exception { 
    } 

    @Test 
    public void testGetBadGuessesLeft() { 
     GuessResponse gr; 
     // First Case - Initial value equal to number passed to constructor 
     assertEquals(NUMBER_OF_GUESSES, gm.getBadGuessesLeft()); 

     // Second Case - After one bad guess - number of guesses decreased by one 
     gr = gm.getGuessResponse(INCORRECT_LETTER);      // Guess letter which isn't in word 
     if (gr != GuessResponse.GUESS_BAD) fail("Should result in bad guess but didn't"); 
     assertEquals(NUMBER_OF_GUESSES - 1 , gm.getBadGuessesLeft()); 

     // Third Case - After one good guess - number of guesses remains the same 
     gr = gm.getGuessResponse(CORRECT_LETTER);      // Guess letter which is in word 
     if ((gr != GuessResponse.GUESS_GOOD)&&(CORRECT_WORD.chars().distinct().count() != 1)) fail("Should result in good guess but didn't"); 
     assertEquals(NUMBER_OF_GUESSES - 1 , gm.getBadGuessesLeft()); 
    } 

    @Test 
    public void testGetGuessResponseGood() { 

     // First Case - a correct letter has been guessed , 
     //testing for correct GuessResponse and no change in mount of bad Guesses Left 
     int currentBadGuessesLeft = gm.getBadGuessesLeft(); 
     if (CORRECT_WORD.chars().distinct().count() != 1) { 
      assertEquals(GuessResponse.GUESS_GOOD , gm.getGuessResponse(CORRECT_LETTER)); 
     } 
     else { 
      assertEquals(GuessResponse.GUESS_WIN , gm.getGuessResponse(CORRECT_LETTER)); 
     } 
     assertEquals(currentBadGuessesLeft , gm.getBadGuessesLeft()); 
    } 
    @Test 
    public void testGetGuessResponseBad() { 
     // Second Case - a bad letter has been guessed , 
     //testing for correct GuessResponse and decrease by one in mount of bad Guesses Left 
     int currentBadGuessesLeft = gm.getBadGuessesLeft(); 
     assertEquals(GuessResponse.GUESS_BAD , gm.getGuessResponse(INCORRECT_LETTER)); 
     assertEquals(currentBadGuessesLeft - 1 , gm.getBadGuessesLeft()); 
    } 
    @Test 
    public void testGetGuessResponseWin() { 
     // Third Case - a wining letter has been guessed 
     //testing for correct GuessResponse and no change in mount of bad Guesses Left 
     //int currentBadGuessesLeft = gm.getBadGuessesLeft(); 

     // Making a for loop for putting all the correct letters but the last wining letter 
     for (int i = 0; i < CORRECT_WORD.chars().distinct().count() - 1 ; i++) { 
      char currentLetter = CORRECT_WORD.charAt(i); 
      if (i != CORRECT_WORD.chars().distinct().count() - 1) assertEquals(GuessResponse.GUESS_GOOD , gm.getGuessResponse(currentLetter)); 
      else assertEquals(GuessResponse.GUESS_WIN , gm.getGuessResponse(currentLetter)); 
     } 
    } 

     // When we get there is only one letter left before winning ,find it and check for win 
     //char currentLetter = CORRECT_WORD.charAt(CORRECT_WORD.length() - 2); 
     //assertEquals(GuessResponse.GUESS_WIN , gm.getGuessResponse(currentLetter)); 
     //System.out.println("Im here"); 
    @Test 
     public void testGetGuessResponseLose() { 
     // Forth Case - a bad letter has been guessed and only one guess left , 
     //testing for correct GuessResponse and decrease by one in mount of bad Guesses Left 
     int currentBadGuessesLeft = gm.getBadGuessesLeft(); 
     int limit = gm.getBadGuessesLeft(); 

     // Making a for loop for guessing all the incorrect letters but the last losing letter 
     for (int i = 0; i < limit - 1; i++) { 
      assertEquals(GuessResponse.GUESS_BAD , gm.getGuessResponse(INCORRECT_LETTER)); 
      assertEquals(currentBadGuessesLeft - 1 , gm.getBadGuessesLeft()); 
      currentBadGuessesLeft = gm.getBadGuessesLeft(); 
     } 

     // When we get there is only one letter left before winning ,find it and check for win 
     assertEquals(GuessResponse.GUESS_LOSE , gm.getGuessResponse(INCORRECT_LETTER)); 
     assertEquals(0 , gm.getBadGuessesLeft()); 
    } 



} 

謝謝

+0

發表您的測試類。錯誤似乎很清楚。 –

+0

@RobertMoskal我已經添加了他們,但既然他們工作的很好,當我直接運行他們,而不是從套件我認爲問題不在他們, – Noam

回答

1

你把測試類在您的套房,而不是測試課程。更改GuessManager.classAsciiPicture.classGuessManagerTest.classAsciiPictureTest.class

package hangman; 

import org.junit.runner.RunWith; 
import org.junit.runners.Suite; 

@RunWith(Suite.class) 
@Suite.SuiteClasses({GuessManagerTest.class, AsciiPictureTest.class }) 
public class TestSuite { 

} 
+0

該死的,你是我的一秒鐘:) –

+0

@RobertMoskal對不起;) –

+0

@羅伯特莫斯卡爾,奧利維耶Gregoire - 謝謝你們兩個,我是一個白癡 – Noam

相關問題