1

我正在使用 eclipse 并使用 Junit 4 测试用例制作了两个测试类。每个测试类都可以正常工作,现在我需要制作一个可以运行两个测试类的 TestSuite。2 个测试类的名称是:GuessManager.class 和 AsciiPicutre.class 所以我通过添加一个名为 TestSuite 的类来做到这一点,这是类代码:

package hangman;  

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

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

}  

当我尝试运行它时,我的两个测试类都出现初始化错误这是相关的屏幕截图: 在此处输入图像描述

知道我做错了什么吗?我相信这与我的 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());
    }



}

谢谢

4

1 回答 1

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  {  

}  
于 2016-05-14T15:07:51.023 回答