简体   繁体   中英

Testing a Java Applet with JUnit

I'm completely new to Java and I'm trying to set up a test for it. But how do I call a method from the test class?

Right now I'm trying with a public method, and making a new instance of the class Hangman, but the call to the method doesn't work.

Hangman.java:

public class Hangman extends Applet implements ActionListener{
       public String[] getWordArray(){
        /* Enter the wordslist, separated by a | here: */
        String str = "computer|"
                + "radio|"
                + "calculator|"
                + "teacher|"
                + "bureau|"
                + "police|"
                + "geometry|"
                + "president";
        String[] temp;

        /* delimiter */
        String delimiter = "\\|";

        /* given string will be split by the argument delimiter provided. */
        temp = str.split(delimiter);

        return temp;
    }
}

HangmanTest.java:

public class HangmanTest {
    Hangman hangman = new Hangman();

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testGetWordArray() {
        int expected = 8;
        int actual = hangman.getWordArray().length();
        Assert.assertEquals(expected, actual);  
    }
}

You have a syntax error. It is int actual = hangman.getWordArray().length; , not int actual = hangman.getWordArray().length(); . The length of array is an attribute, not a method. All other datastructures (like ArrayList ) have a method for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM