java Junit 正负测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15544639/
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 20:00:30  来源:igfitidea点击:

Junit Positive and Negative Testing

javaunit-testingjunit

提问by ToniHopkins

I have an assignment in which I need to create some tests for my code. We have to use Junit tests so I have decided to do positive and negative testing of my methods.

我有一项作业,需要为我的代码创建一些测试。我们必须使用 Junit 测试,所以我决定对我的方法进行正面和负面的测试。

For example one test for getting the name from an ArrayList I have positive and negative testing:

例如,从 ArrayList 获取名称的一项测试我进行了正面和负面测试:

Postive:

正面:

public void testGetNamePositive() { 
    Entry entry1 = new Entry("Susan Holmes", "122 Harringdale", "Workington", "CA14 2GH"); 
    assertEquals("Susan Holmes",entry1.getName()); 
} 

Negative:

消极的:

public void testGetNameNegative() { 
    Entry entry1 = new Entry("Susan Holmes", "122 Harringdale", "Workington", "CA14 2GH"); 
    assertFalse( entry1.getName() == "Alison"); 
} 

The test I am unsure about is this one:

我不确定的测试是这个:

public void testEntryCreated() {
    Entry entry1 = new Entry("Susan Holmes", "122 Harringdale", "Workington", "CA14 2GH");
    assertNotNull(entry1);
}

Is there a negative test to this?

对此是否有负面测试?

回答by Evgeniy Dorofeev

Entry class public constructor is supposed to have some arguments validation, eg name cannot be null. So you can write a negative test like this

入口类公共构造函数应该有一些参数验证,例如名称不能为空。所以你可以写一个像这样的负面测试

@Test(expected = NullPointerException.class)
public void testCreateEntryWithNameIsNullThrowsNPE() {
    Entry entry1 = new Entry(null, "122 Harringdale", "Workington",
            "CA14 2GH");
}

note that I used JUnit 4

请注意,我使用了 JUnit 4

回答by moxn

I'd argue, that the testEntryCreatedtest is already not necessary. You are here not testing the behavior of your class but rather the capability of the JVM to properly create objects. Instead, focus on testing how you want your objects to behave.

我认为,testEntryCreated测试已经没有必要了。您在这里不是在测试类的行为,而是在测试 JVM 正确创建对象的能力。相反,专注于测试您希望对象如何表现。

回答by John Snow

Im not quite sure what you are trying to test here. All that you are currently testing is that the name variable gets set correctly.

我不太确定您要在这里测试什么。您当前正在测试的只是 name 变量设置正确。

When you unit test you want to test specific methods in isolation (the smalest testable unit) with both negative and positive input.

当您进行单元测试时,您希望使用负输入和正输入单独测试特定方法(最小的可测试单元)。

Your example of negative test is as follows:

您的阴性测试示例如下:

public void testGetNameNegative() 

{ 
    Entry entry1 = new Entry("Susan Holmes", "122 Harringdale", "Workington", "CA14 2GH"); 
    assertFalse( entry1.getName() == "Alison"); 
} 

This is not a negative test case. What you would like to test for negative is for example that a name can not contain numbers or be an empty string:

这不是一个负面的测试案例。例如,您要测试是否为负数是名称不能包含数字或为空字符串:

public void testGetNameNegative() 

{ 
    Entry entry1 = new Entry("123", "122 Harringdale", "Workington", "CA14 2GH"); 
    //Expect some behaviour
    Entry entry1 = new Entry("", "122 Harringdale", "Workington", "CA14 2GH"); 
    //Expect some other behavior
} 

What you expect here is probably some kind of exception since you are "testing" the constructor. If this was a method you could expect some kind of return value instead of an exception

您在这里期望的可能是某种异常,因为您正在“测试”构造函数。如果这是一种方法,您可以期待某种返回值而不是异常