为数组中小于20的数组创建JUnit,尝试创建假定的数组,然后进行测试
Creating a JUnit for an array with a number less than 20 in the array, trying to create the assumed array, to then test
作业:编写一个 JUnit 测试,假设您有一个 int 值数组,并且您只希望 JUnit 测试在任何值小于 20 时失败。
我知道它只要求 JUnit 假设其他方法已经创建。但我还是想创建它们。但是我不知道该怎么做。到目前为止,这是我的代码:
package ArrayJU;
public class ArrayJUTest {
public static void main(String[] args){
ArrayJUTest array = new ArrayJUTest();
int arr[] = {23,25,50,68,3};
System.out.println(array.arrayLessThan(arr));
}
public boolean arrayLessThan(int array[]){
for (int element : array) {
if(element>20){
return true;
}
else{
return false;
}
}
}
}
对于 arrayLessThan Eclipse 告诉我我需要 return 一个布尔值,但是我不知道如何在没有 for 循环的情况下遍历数组。如果我在 for 循环之外 return 为 true 或 false,它将破坏我尝试使用 if/else 语句的目的。我该怎么做?对你的帮助表示感谢。
JUnit 测试:
package ArrayJU;
import static org.junit.Assert.*;
import org.junit.Test;
public class JUnitArrayTest {
@Test
public void JUnitArTest(){
int[] arr = {32,52,89,12};
ArrayJUTest arra = new ArrayJUTest();
boolean poop = arra.arrayLessThan(arr);
assertEquals(false, poop);
}
}
Eclipse(实际上是 java 编译器)正在抱怨,因为在您的 for
循环之后,您的方法没有 return boolean
。编译器不知道该方法永远不会走那么远,因为它在第一次迭代期间总是 return 。无论如何这是一个问题,因为您的方法永远不会超越第一个数组元素。
编写这样的循环的典型方法是遵循以下几行:
public boolean arrayLessThan(int[] array) {
for (int element: array) {
if (element < 20) {
return false;
}
}
return true;
}
但除此之外,您还忽略了 JUnit 的要点。它是一个框架,您需要将测试编写为测试 classes 的方法,这些方法是以该框架要求的非常特定的方式编写的。您不必编写自己的 main
函数 - 该框架提供了一个功能,它可以查看您的代码以找到您所有的测试 classes 以及在每个 class 中实现的测试,然后运行这些为你测试。
您应该 google 获取 JUnit 的文档/教程/示例,然后重试。
这个问题似乎更多地是关于 "why does this not compile and what the method return for an empty set" 而不是 "how do I write a JUnit test"。我建议阅读一些 JUnit 教程(例如 this tutorial from mkyong)以了解它们。我会尽量回答我认为是第一个问题。
首先要根据您的描述注意循环的正确性。该循环目前总是return一个基于数组第一个值的值:
public boolean arrayLessThan(int array[]){
for (int element : array) {
if(element>20){
return true;
}
else{
return false;
}
}
}
根据您的描述,只有 return false
如果 任何 项与您的 predicate 匹配(一项少于 20 ).您还会收到编译器错误,因为它不会 return 任何空数组(0 个元素)。这是更改它的一种方法:
public boolean arrayLessThan(int array[]){
for (int element : array) {
if(element < 20){
return false;
}
}
return true;
}
And if I return a true or a false outside the for loop it will defeat
the purpose of what I'm trying to do with the if/else statements
嗯,不是真的。这取决于您希望如何对 0 元素数组案例进行建模。对其建模的最佳方法是 return true
因为您不能指向不满足条件的元素。这被称为 vacuous truth。
您可以阅读 Java 8 个流的很好的解释 anyMatch
和 allMatch
以及空洞的真相 。
我很困惑,有两个问题...
首先,这不是 junit 测试。
它们看起来像这样:
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
class HypotheticalClassTest {
private HypotheticalClass hypClass;
@Before
public void setup() {
hypClass = new HypotheticalClass();
}
@Test
public void ensureNoNumsLessThanTwenty() {
int[] result = hypClass.hypotheticalMethod();
// Some assertions. Check out the org.junit.Assert class.
}
}
其次,你是方法arrayLessThan
让我们一步一步来:
public boolean arrayLessThan(int array[]){
for (int element : array) { // For each int in array...
if(element>20){ // If element is greater than 20
return true; // Return true (returning stops the loop,
// do you want to stop on the
// first element greater than
// 20?)
} //
else{ // otherwise
return false; // Return false
}
} // Assuming there are no elements in the
// array (length 0) then this is where it
// comes. There's no return here, there
// needs to be, then it will stop
// complaining.
}
现在查看它,我们发现它无法编译,因为没有针对空数组情况的 return 语句。我们还看到它只检查第一个元素!查看 continue
的作用,它将解决只检查第一个元素的问题,或者以不同的方式编写您的条件。
作业:编写一个 JUnit 测试,假设您有一个 int 值数组,并且您只希望 JUnit 测试在任何值小于 20 时失败。
我知道它只要求 JUnit 假设其他方法已经创建。但我还是想创建它们。但是我不知道该怎么做。到目前为止,这是我的代码:
package ArrayJU;
public class ArrayJUTest {
public static void main(String[] args){
ArrayJUTest array = new ArrayJUTest();
int arr[] = {23,25,50,68,3};
System.out.println(array.arrayLessThan(arr));
}
public boolean arrayLessThan(int array[]){
for (int element : array) {
if(element>20){
return true;
}
else{
return false;
}
}
}
}
对于 arrayLessThan Eclipse 告诉我我需要 return 一个布尔值,但是我不知道如何在没有 for 循环的情况下遍历数组。如果我在 for 循环之外 return 为 true 或 false,它将破坏我尝试使用 if/else 语句的目的。我该怎么做?对你的帮助表示感谢。
JUnit 测试:
package ArrayJU;
import static org.junit.Assert.*;
import org.junit.Test;
public class JUnitArrayTest {
@Test
public void JUnitArTest(){
int[] arr = {32,52,89,12};
ArrayJUTest arra = new ArrayJUTest();
boolean poop = arra.arrayLessThan(arr);
assertEquals(false, poop);
}
}
Eclipse(实际上是 java 编译器)正在抱怨,因为在您的 for
循环之后,您的方法没有 return boolean
。编译器不知道该方法永远不会走那么远,因为它在第一次迭代期间总是 return 。无论如何这是一个问题,因为您的方法永远不会超越第一个数组元素。
编写这样的循环的典型方法是遵循以下几行:
public boolean arrayLessThan(int[] array) {
for (int element: array) {
if (element < 20) {
return false;
}
}
return true;
}
但除此之外,您还忽略了 JUnit 的要点。它是一个框架,您需要将测试编写为测试 classes 的方法,这些方法是以该框架要求的非常特定的方式编写的。您不必编写自己的 main
函数 - 该框架提供了一个功能,它可以查看您的代码以找到您所有的测试 classes 以及在每个 class 中实现的测试,然后运行这些为你测试。
您应该 google 获取 JUnit 的文档/教程/示例,然后重试。
这个问题似乎更多地是关于 "why does this not compile and what the method return for an empty set" 而不是 "how do I write a JUnit test"。我建议阅读一些 JUnit 教程(例如 this tutorial from mkyong)以了解它们。我会尽量回答我认为是第一个问题。
首先要根据您的描述注意循环的正确性。该循环目前总是return一个基于数组第一个值的值:
public boolean arrayLessThan(int array[]){
for (int element : array) {
if(element>20){
return true;
}
else{
return false;
}
}
}
根据您的描述,只有 return false
如果 任何 项与您的 predicate 匹配(一项少于 20 ).您还会收到编译器错误,因为它不会 return 任何空数组(0 个元素)。这是更改它的一种方法:
public boolean arrayLessThan(int array[]){
for (int element : array) {
if(element < 20){
return false;
}
}
return true;
}
And if I return a true or a false outside the for loop it will defeat the purpose of what I'm trying to do with the if/else statements
嗯,不是真的。这取决于您希望如何对 0 元素数组案例进行建模。对其建模的最佳方法是 return true
因为您不能指向不满足条件的元素。这被称为 vacuous truth。
您可以阅读 Java 8 个流的很好的解释 anyMatch
和 allMatch
以及空洞的真相
我很困惑,有两个问题...
首先,这不是 junit 测试。
它们看起来像这样:
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
class HypotheticalClassTest {
private HypotheticalClass hypClass;
@Before
public void setup() {
hypClass = new HypotheticalClass();
}
@Test
public void ensureNoNumsLessThanTwenty() {
int[] result = hypClass.hypotheticalMethod();
// Some assertions. Check out the org.junit.Assert class.
}
}
其次,你是方法arrayLessThan
让我们一步一步来:
public boolean arrayLessThan(int array[]){
for (int element : array) { // For each int in array...
if(element>20){ // If element is greater than 20
return true; // Return true (returning stops the loop,
// do you want to stop on the
// first element greater than
// 20?)
} //
else{ // otherwise
return false; // Return false
}
} // Assuming there are no elements in the
// array (length 0) then this is where it
// comes. There's no return here, there
// needs to be, then it will stop
// complaining.
}
现在查看它,我们发现它无法编译,因为没有针对空数组情况的 return 语句。我们还看到它只检查第一个元素!查看 continue
的作用,它将解决只检查第一个元素的问题,或者以不同的方式编写您的条件。