java JUnit 非空测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16607975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:27:08  来源:igfitidea点击:

JUnit not null test

javajunit

提问by vicR

I'm trying to implement an easy junit test for a method. My intention is to test if the method parameter is not null. So that the test fails if the parameter is null.

我正在尝试为一个方法实现一个简单的 junit 测试。我的目的是测试方法参数是否不是null. 如果参数为 ,则测试失败null

the method is:

方法是:

/*
 * @return the db entry object
*/
getEntry( String user, String grp, int status);

My first try to implement the test is

我第一次尝试实施测试是

public void testGetEntry() {
    String userId = null;
    getEntry( userId, "grp1", 3);
    asserNotNull(userId);
}

I think thats not the correct way. thx for any help :)

我认为那不是正确的方法。感谢任何帮助:)

回答by Julien Bodin

You can't test that. You can test a beavhior like : "Check that a IllegalArgumentException is thrown if a null parameter is passed". A unit test for testing if a parameter is not null does not make sense, it is not the purpose of unit testing : https://en.wikipedia.org/wiki/Unit_testing

你不能测试那个。您可以测试像这样的行为:“如果传递了空参数,请检查是否抛出了 IllegalArgumentException”。用于测试参数是否为空的单元测试没有意义,这不是单元测试的目的:https: //en.wikipedia.org/wiki/Unit_testing

Here, "test if the method parameter is not null" should be done in the method.

这里应该在方法中进行“测试方法参数是否为空”。

回答by jontejj

You should definitely check out NullPointerTesterfrom Guava. You use it in your tests like this:

你一定要检查出NullPointerTester番石榴。您可以在测试中使用它,如下所示:

new NullPointerTester().testAllPublicInstanceMethods(YourClass.class)

Then you need to annotate your methods with JSR-305s @NonNull

然后你需要用JSR-305s @NonNull注释你的方法

getEntry(@NonNull String user, @NonNull String grp, int status);

Then use PreconditionscheckNotNull in your production code.

然后在您的生产代码中使用PreconditionscheckNotNull。

getEntry(@NonNull String user, @NonNull String grp, int status)
{
     Preconditions.checkNotNull(user, "user must be specified");
     Preconditions.checkNotNull(grp, "grp must be specified");
     //return entry;
}

This will make other classes receive a NullPointerException when they misuse your class. When they receive this they'll know that they did something wrong.

这将使其他类在滥用您的类时收到 NullPointerException。当他们收到这个时,他们就会知道他们做错了什么。

回答by ramroum89

In order to check that your method doesn't have a null parameter, you need to use Mockito and mock the class( let's call it C.java) where this method ( let's call it m(param) ) is declared. Of course class C should NOT be the class you want to test (a mock is a "dummy" implementation).

为了检查您的方法是否没有空参数,您需要使用 Mockito 并模拟声明了此方法(我们称之为 m(param) )的类(我们称之为 C.java)。当然,C 类不应该是您要测试的类(模拟是“虚拟”实现)。

If you are in the case where you are calling the method m of class C and you want to verify that the argument "param" of m(param) is not null, here is how to do it :

如果您正在调用 C 类的方法 m 并且您想验证 m(param) 的参数“param”不为空,请执行以下操作:

C cMock= Mockito.mock(C.class);
verify(cMock).m(isNotNull());

Here is some useful documentation :

这是一些有用的文档:

http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Matchers.html

http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Matchers.html

http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Mockito.html

http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Mockito.html

I hope this helps

我希望这有帮助

Regards

问候