JUnit测试assertEqual NullPointerException

舞狮

我不确定测试用例为什么没有输出true两种情况都应该给出一个NullPointerException

我尝试这样做(不完全相同,但是它给出了和的输出true):

    String nullStr = null;

//@Test
public int NullOutput1() {
    nullStr.indexOf(3);
    return 0;
}

//@Test(expected=NullPointerException.class)
public int NullOutput2() {
    nullStr.indexOf(2);
    return 0;
}

@Test(expected=NullPointerException.class)
public void testboth() {
    assertEquals(NullOutput1(), NullOutput2());
}

亚军:

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunnerStringMethods {
    public static void main(String[] args) {
        Result result = JUnitCore.runClasses(TestJunitMyIndexOf.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        System.out.println(result.wasSuccessful());
    }
}

方法:

public static int myIndexOf(char[] str, int ch, int index) {
        if (str == null) {
            throw new NullPointerException();
        }
        // increase efficiency
        if (str.length <= index || index < 0) {
            return -1;
        }
        for (int i = index; i < str.length; i++) {
            if (index == str[i]) {
                return i;
            }
        }
        // if not found
        return -1;
    }

测试用例:

@Test(expected=NullPointerException.class)
public void testNullInput() {
    assertEquals(nullString.indexOf(3), StringMethods.myIndexOf(null, 'd',3));
}
David Koelle:

我相信您想在fail这里使用

@Test(expected=NullPointerException.class)
public void testNullInput() {
    fail(nullString.indexOf(3));
}

import static org.junit.Assert.fail;如果需要,请确保添加

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

RxJava Android的MVP单元测试的NullPointerException

通过使用硒@Before注解用JUnit 5的NullPointerException

变化的JUnit TestNG的上,NullPointerException异常

JerseyTest和JUnit引发NullPointerException

JUnit测试Android NullPointerException

JUnit测试Android NullPointerException

使用NullPointerException测试null是否不好呢?

模拟存储库JUnit时发生NullPointerException

MockMvc控制器测试并返回NullPointerException

使用jUnit测试RESTful Api时出现java.lang.NullPointerException

在JUnit 5测试中模拟EhCache NullPointerException

用于基于构造函数的自动装配的Mock的Junit Mockito NullPointerException

带有Retrofit的RxJava JUnit测试抛出NullPointerException

Junit 5 @MockBean中的NullPointerException

使用Spring Boot,Selenium和JUnit运行Cucumber测试时获取NullPointerException

运行JUnit4测试时出现NullPointerException,带有MockitoHint“ Unused ...”和“ args OK?”

NullPointerException:测试将Ebean与Mockito和JUnit,Kotlin结合使用的DAO类

类测试期间发生NullPointerException

JUnit中的奇怪NullPointerException

条件查询Mockito单元测试-NullPointerException

Spring 测试和 NullPointerException

创建测试数据时Envers NullPointerException

测试服务时 JUnit5 SpringBootTest NullPointerException

子类测试中的 JUnit4 NullPointerException,覆盖方法

调用 powermockito 测试方法时出现 NullPointerException

MockMvc perform(post()) 测试失败,出现 NullPointerException

MockMvc peform 在集成测试中返回 nullPointerException

我的测试类 SpringBoot 中的 NullPointerException

Junit NullPointerException,我不明白为什么