简体   繁体   English

JUnit测试失败

[英]JUnit Test Failure

I am trying to run a test on the class below: 我正在尝试对以下课程进行测试:

package chapter03.backend; 包Chapter03.backend;

import java.util.Map;

public class CharacterCounter {
    public static Map<Character, Integer> countCharacters(String text) {

         if (text == null) {
              throw new IllegalArgumentException("text must not be null");
            }
        return null;

    }
}

I have written this test class: 我写了这个测试课:

package chapter03.backend;

import static org.junit.Assert.*;

import org.junit.Test;

public class CharacterCounterTests {

    @Test(expected=IllegalAccessException.class)
    public void testNullInput() {
        CharacterCounter.countCharacters(null);
    }

}

When I run it, it keeps failing. 当我运行它时,它一直在失败。 Here is a screen shot of the error: 这是错误的屏幕截图:

在此处输入图片说明

I will appreciate pointers on this. 我将对此表示赞赏。

IllegalAccessException is not IllegalArgumentException and is not a subclass of it either. IllegalAccessException不是IllegalArgumentException ,也不是其子类。

You throw indeed IllegalArgumentException in your tested method but you assert in the test that IllegalArgumentException is thrown. 您确实在测试的方法中抛出了IllegalArgumentException ,但是在测试中断言抛出了IllegalArgumentException
Consequently, the assertion fails. 因此,断言失败。

You should assert that IllegalArgumentException is expected: 您应该断言IllegalArgumentException是预期的:

@Test(expected=IllegalArgumentException.class)
public void testNullInput() {
    CharacterCounter.countCharacters(null);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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