简体   繁体   中英

Creating a JUnit for an array with a number less than 20 in the array, trying to create the assumed array, to then test

Assignment: Write a JUnit test assuming you have an array of int values and you only want the JUnit test to fail if any of the values are less than 20.

I know it only asks for the JUnit assuming the other methods are already created. But I want to create them anyway. However I do not know how to go about it. This is my code so far:

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;
            }

        }


    }

}

For the arrayLessThan Eclipse is telling me that I need to return a boolean, however I wouldn't know how to iterate through the array without a for loop. 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. How do I go about this? Your help will be greatly appreciated.

JUnit Test:

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 (really the java compiler) is complaining because after your for loop, your method doesn't return a boolean . The compiler doesn't figure out that the method never gets that far because it will always return during its very first iteration. Which is a problem anyway, since your method will never look beyond the first array element.

The typical way to code a loop like this is something along these lines:

public boolean arrayLessThan(int[] array) {
  for (int element: array) {
    if (element < 20) {
      return false;
    }
  }
  return true;
}

But beyond this, you're missing the point of JUnit. It's a framework, and you need to write your tests as methods of test classes that are written in a very specific manner required by that framework. You don't write your own main function - the framework provides one that looks through your code to find all your test classes and the tests implemented in each class, and then runs those tests for you.

You should google for documents / tutorials / examples of JUnit and then try again.

This question seems to be more about "why does this not compile and what the method return for an empty set" than "how do I write a JUnit test" . I would recommend reading up on some JUnit tutorials (like this tutorial from mkyong ) to get an understanding of them. I'll try to answer what I think is the first question.

The first thing is to note about the correctness of your loop based on your description. The loop will currently always return a value based on the first value of the array:

public boolean arrayLessThan(int array[]){
    for (int element : array) {

        if(element>20){
            return true;
        }
        else{
            return false;
        }

    }
}

Based on your description, it should only return false if any item matches your predicate (an item is less than 20). You are also getting a compiler error because it does not return anything for an empty array (0 elements). This would be one way to change it:

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

Well, not really. It depends on how you want to model the 0-element array case. The best way to model it would be to return true because you cannot point to an element that does not satisfy your condition. This is known as vacuous truth .

You can read a good explanation for Java 8 streams with anyMatch and allMatch and vacuous truth in this question/answer .

I'm confused, there are two issues...

The first is that this isn't a junit test.

They look like this:

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.
    }
}

Second, is you method arrayLessThan

Let's go through it step by step:

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.
}

Looking at it now we see it doesn't compile because there is no return statement for the case of an empty array. Also we see it only checks the first element! Look up what continue does, it will fix the issue of only checking the first element, or write your condition differently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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