简体   繁体   English

在 JUnit 上测试空值

[英]Testing a null value on JUnit

I am trying to test whether a user has entered the correct competition ID for a sport.我正在尝试测试用户是否为一项运动输入了正确的比赛 ID。 Below is the method I have made for this:以下是我为此制定的方法:

protected boolean containCompetitionId (Set<RestrictionFailure> failures,  CompetitionSetup competitionId) {

    ScenarioDefinition.Competition competitionScenarioDef = new ScenarioDefinition.Competition();

    if (competitionScenarioDef.getId() == null) {
        failures.add(new RestrictionFailure("Competition ID does not exist", competitionId));
        return false;
    }
    else if(scenarioManager.findCompetitionById(competitionScenarioDef.getId()) == null) {
        failures.add(new RestrictionFailure("Competition ID does not exist", competitionId));
        return false;
    }
    else if(competitionScenarioDef.getId() != competitionId.getCompetitionId() ) {
        failures.add(new RestrictionFailure("Competition ID does not exist", competitionId));
        return false;
    }
    return true;
}

I want to make a Junit test for this method, how would I put a competition Id of value null in to my test and pass it on to the method i have made to test whether it generates the message ("Competition ID does not exist", competitionId) ?我想为此方法进行 Junit 测试,我如何将值为null的竞争 ID 放入我的测试中并将其传递给我为测试它是否生成消息而创建的方法(“竞争 ID 不存在”) , 竞争 ID) ?

You need to take advantage of dependency injection .您需要利用依赖注入 It's a fancy name for a relatively simple concept.这是一个相对简单的概念的奇特名称。 Essentially: do not create new objects within a method and, instead, have the caller of the method provide the object.本质上:不要在方法中创建新对象,而是让方法的调用者提供对象。

What you've seen observed is that by creating the object within the method, you have no control over how it's created, or what data it contains.您所看到的是,通过在方法中创建对象,您无法控制它的创建方式或它包含的数据。 This makes it difficult to test.这使得测试变得困难。 Dependency injection solves this problem.依赖注入解决了这个问题。

Your method would become:你的方法会变成:

protected boolean containCompetitionId (
    ScenarioDefinition.Competition competitionScenarioDef, // added
    Set<RestrictionFailure> failures,
    CompetitionSetup competitionId)
{

You can now inject any number of competitionScenarioDef s and test your method with any number of scenarios.您现在可以注入任意数量的competitionScenarioDef并使用任意数量的场景测试您的方法。

A test for this method might look somewhat like this.这种方法的测试可能看起来有点像这样。 I've had to make up a few different things because you didn't provide code for all your classes (which is not necessarily a bad thing) so just think of it as pseudocode.我不得不编造一些不同的东西,因为您没有为所有类提供代码(这不一定是一件坏事),所以只需将其视为伪代码即可。

@Test
public void myTest()
{
    Foo foo = new Foo(); // whatever class contains your containCompetitionId method 

    Set<RestrictionFailure> failures = new HashSet<>();

    foo.containCompetitionId(
       new ScenarioDefinition.Competition(/*id: */ null)
       failures,
       new CompetitionSetup(...)
    );

    Assert.assertEquals(1, failures.size());
    Assert.assertEquals("Competition ID does not exist", failures.get().getFailureReason());
}

assertNull(Object actual) Asserts that actual is null. assertNull(Object actual) 断言该实际为空。 https://junit.org/junit5/docs/5.0.1/api/org/junit/jupiter/api/Assertions.html https://junit.org/junit5/docs/5.0.1/api/org/junit/jupiter/api/Assertions.html

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

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