Michael Tot Korsgaard
Michael Tot Korsgaard

Reputation: 4014

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);  
    }
}

Upvotes: 0

Views: 658

Answers (1)

Turing85
Turing85

Reputation: 20195

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.

Upvotes: 1

Related Questions