gpt4 book ai didi

java - 如何使用 Junit 测试交换数组中 2 个元素的方法

转载 作者:行者123 更新时间:2023-11-28 20:28:46 26 4
gpt4 key购买 nike

<分区>

我在类中写了 2 个方法来交换数组中的 2 个元素。但我不确定如何在 JUnit 中测试我的方法,我想测试 2 个元素是否正确交换,并检查数组中的索引是否有效。最后一个测试来检查方法是否会交换字符串以及 double 。

import java.util.Arrays;

public class GenericExample {

int index1;
int index2;

public static <T> T [] swap(T[] array, int index1, int index2)
{
T temp = array[index1];
array [index1] = array [index2] ;
array [index2] = temp;

return array;

}

public static void main(String[] args) {
Integer [ ] array = {1, 2, 3, 4};
System.out.println("Array is: " + Arrays.toString(array));
swap(array,1,3);
System.out.println("Array is: " + Arrays.toString(array));
}

这是我创建的用于测试此代码的测试类。 TestClass

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com