断言Junit中的字符串

时间:2013-03-05 07:09:03

标签: java

我想我搞砸了。我在JUnit测试类中有这个代码:

    import static org.junit.Assert.*;

    import java.util.ArrayList;

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


    public class HangmanLogicTest {

ArrayList<Integer> negIntList = new ArrayList<Integer>();
ArrayList<Integer> intList = new ArrayList<Integer>();

HangmanLogic hl = new HangmanLogic();

String booty = "b----";


// creates a list of integers from 1 to 10000
private void makePosInts()
{

    int min = 1;
    int max = 10000;
    for (int i = min; i <= max; i++) {
        intList.add(i);
    }
}


// testing the constructor to see if it will accept valid strings 
@Test
public void testString_HangmanLogic() throws InvalidConfigurationException {

    for (String i : FileReader.validWords) { // FileReader.validWords is a list of all valid words
        HangmanLogic h = new HangmanLogic(i, 1);
    }
}

// testing the constructor to see if it will accept valid integers 
@Test
public void testInt_HangmanLogic() throws InvalidConfigurationException {

    for (int i : intList) { 
        HangmanLogic h = new HangmanLogic("booty", i);
    }
}

// testing the constructor to see if it will throw exception for null string parameter
@Test(expected = InvalidConfigurationException.class)
public void testNullStringException_HangmanLogic() throws InvalidConfigurationException {

    HangmanLogic h = new HangmanLogic(nullString, 1);

}

// testing the constructor to see if it will throw exception for empty string parameter
@Test(expected = InvalidConfigurationException.class)
public void testEmptyStringException_HangmanLogic() throws InvalidConfigurationException {
    HangmanLogic h = new HangmanLogic("", 1);
}


// testing the constructor to see if it will throw an exception for 0 in the int parameter
@Test(expected = InvalidConfigurationException.class)
public void test0Exception_HangmanLogic() throws InvalidConfigurationException {
    HangmanLogic h = new HangmanLogic("booty", 0);
}

// testing the constructor to see if it will throw an exception for negative integer parameters
@Test(expected = InvalidConfigurationException.class)
public void testNegIntException_HangmanLogic() throws InvalidConfigurationException {


        HangmanLogic h = new HangmanLogic("booty", (-1));


}


// testing getKnownKeyPhrase() to see if the if conditional works at HangmanLogic line 55
@Test
public void testIf_getKnownKeyPhrase() throws InvalidConfigurationException{

    hl.keyPhrase = "booty";
    HangmanLogic.guessedCharacters.add('b');
    assertTrue(hl.getKnownKeyPhrase().equals("b----"));

}

// testing

}

方法getKnownKeyPhrase()是以下代码:

public String getKnownKeyPhrase()
{
    char[] encodedKeyPhrase = new char[keyPhrase.length()];
    String lowerCaseKeyPhrase = keyPhrase.toLowerCase();

    for(int i = 0; i < keyPhrase.length(); i++)
    {
        char cur = lowerCaseKeyPhrase.charAt(i);
        if(guessedCharacters.contains(cur))
            encodedKeyPhrase[i] = cur;
        else
            encodedKeyPhrase[i] = '-';
    }

    return (new String(encodedKeyPhrase) );
}

其中encodedKeyPhrase是一个char数组,看起来像这样{b, - , - , - , - }

这是错误跟踪:

java.lang.NullPointerException
at HangmanLogicTest.testIf_getKnownKeyPhrase(HangmanLogicTest.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我一直收到空指针错误。

我做错了什么?

谢谢!

0 个答案:

没有答案