2016-05-14 40 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

相关问题