使用void方法进行JUnit测试

d8tt03nd  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(184)

我 想 为 我 的 void 方法 创建 一 个 JUnit 测试 。 这个 方法 没有 参数 。 用 有 返回 值 的 方法 进行 测试 对 我 来说 很 清楚 , 但是 void 方法 给 我 带来 了 困难 , 特别 是 当 它们 包含 Map 和 列表 时 。
在 我 的 示例 中 , 我 希望 检查 HashMap 中 是否 出现 了 单词 " car " , 并 在 最 后 输出 出现 单词 " car " 的 所有 索引 。

public class Car {

    private Map<Integer, String> carHashMap = new HashMap<>();
    private ArrayList<Integer> showCarsInMap = new ArrayList<>();

    public void putCarText() {
        carHashMap.put(1, "this car is very fast car");
        carHashMap.put(2, "i have a old car");
        carHashMap.put(3, "my first car was an mercedes and my second car was an audi");
        carHashMap.put(4, "today is a good day");
        carHashMap.put(5, "car car car car");
    }

    public void countWordCar() {
        for (Map.Entry<Integer, String> entrySection : carHashMap.entrySet()) {

            if (entrySection.getValue().contains("car")) {

                showCarsInMap.add(entrySection.getKey());

            }
        }
    }

    public void printShowCarsInMap() {

        System.out.println(showCarsInMap);

    } 
}

中 的 每 一 个
我 已经 创建 了 一 个 JUnit 测试 类 , 但是 我 不能 再 进一步 了 。 我 想 检查 在 我 的 测试 中 单词 " car " 出现 在 哪个 索引 中 。 这个 例子 Java testing a method with JUnit (void) 帮 了 我 一 点 忙 , 但是 因为 我 没有 参数 , 所以 我 仍然 很 难 创建 一 个 合适 的 测试 用例 。

class CarTest {

    Car car;
    Map<Integer, String> carHashMap;

    @BeforeEach
    void setUp() {
        car = new Car();
        carHashMap = new HashMap<>();
        ArrayList<Integer> showCarsInMap = new ArrayList<>();

        carHashMap.put(1, "this car is very fast car");
        carHashMap.put(2, "i have a old car");
        carHashMap.put(3, "my first car was an mercedes and my second car was an audi");
        carHashMap.put(4, "today is a good day");
        carHashMap.put(5, "car car car car");

    }

    @Test
    void countWordCarSize() {
        assertEquals(5, carHashMap.size());
    }

    @Test
    void countWordCar() {
        assertEquals();
    }
}

格式
据 我 所 知 , 有 各种 可能 性 与 " Mockito " , 因为 我们 没有 在 大学 里 , 它 可能 会 或 不 应该 使用 。

e5nqia27

e5nqia271#

大多数情况下,您无法测试某个东西的事实意味着代码设计不正确。
在您的示例中,Car类的行为完全是内部的,如果没有其他工具,就不可能对其进行测试。
但是简单的重新设计可能会帮助你达到你的目标。你可以在例子中把你的计数方法移到一个单独的类中,并在你的汽车类中使用它。

public class CarCounter {
    public List<Integer> countWordCar(Map<Integer, String> carMap) {
        List<Integer> showCarsInMap = new ArrayList<>();
        for (Map.Entry<Integer, String> entrySection : carMap.entrySet()) {
            if (entrySection.getValue().contains("car")) {
                showCarsInMap.add(entrySection.getKey());
            }
        }
        return showCarsInMap;
    }
}

然后你可以这样测试它:

class CarCounterTest {
    @Test
    void countCars() {
        Map<Integer, String> carHashMap = new HashMap<>();
        carHashMap.put(1, "this car is very fast car");
        carHashMap.put(2, "i have a old car");
        carHashMap.put(3, "my first car was an mercedes and my second car was an audi");
        carHashMap.put(4, "today is a good day");
        carHashMap.put(5, "car car car car");
        CarCounter counter = new CarCounter();

        List<Integer> results = counter.countWordCar(carHashMap);

        assertEquals(4, results.size());
    }

}

在你的汽车课上这样使用计数器:

public class Car {
    ...
    private final CarCounter carCounter;
        public Car(CarCounter carCounter) {
        this.carCounter = carCounter;
    }
    ...
        public void countWordCar() {
        showCarsInMap = carCounter.countWordCar(carHashMap);
    }
    ...
}

为了避免设计难以测试/维护的类,你可以看看TDD方法。它基本上意味着你在实际实现之前先运行单元测试。如果你编写的方法易于测试和使用,你会立刻发现。你可以在这里阅读更多关于它的信息:https://www.guru99.com/test-driven-development.html
此外,要设计可维护的类,请看一下SOLID概念。它们是设计可扩展和可维护代码时应遵循的集合或规则。您可以在此处阅读更多相关内容:https://medium.com/mindorks/solid-principles-explained-with-examples-79d1ce114ace

相关问题