#How do you do testing in JUnit?

32 messages · Page 1 of 1 (latest)

median drift
#

I know how to use assertEquals when inputting a method, but what about testing for a setter (that is a void method)?

twilit gazelleBOT
#

This post has been reserved for your question.

Hey @median drift! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

median drift
#
public class BusStopTest {

    BusStop busStop; // do not initialise this variable, it will be done for you

    /*
     * Does getName return the bus stop's name?
     * Does setName change the bus stop's name?
     */
    @Test
    public void getSetNameTest() {
        assertEquals("hello" ,busStop.getName());
        assertTrue(busStop.setName());
    }
#
package unittestingsimple;

// IMPORTANT: do not change the below BusStop class
public class BusStop {
    private String name; // human-readable name
    private int id; // unique identifier
    private double latitude; // north-south coordinate
    private double longitude; // east-west coordinate

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
}
// ^^^ IMPORTANT: do not change the above BusStop class
#

setName is a void method

#

so junit doesnt compile?

still nest
#

Just use the setter to change something about the object, then test getters to verify that they reflect the change

median drift
#

ok thats what i thought but it went awry b4

#

ill try again

#

ohhh

#

i tried to getname

#

set name

#

then get name

#

but if they dont provide the name

#

and u can set then get

#

uve done both

median drift
#
package unittestingarrays;

/* The import below is a support class which emulates JUnit behaviour.
*  Outside of MyJavaTutor you would need to say
*  import org.junit.*;
*  to import JUnit functionalities. */
import static unittestingarrays.Support.*;

public class ArrayUtilitiesTest {

    ArrayUtilities arrayUtilities; // do not initialise this variable, it will be done for you

    /* Does the findMiddle method correctly return the middle value? */
    @Test
    public void findMiddleFindsMiddleTest() {
        // add code here
    }

    /*
     * Does the findMiddle method return the correct value when the array has
     * an even number of elements?
     */
    @Test
    public void findMiddleFindsMiddleEvenTest() {
        // add code here
    }

    /* Does the findMiddle method handle a null array correctly? */
    @Test
    public void findMiddleAllowsNullTest() {
        // add code here
    }

    /* Does the findMiddle method handle an empty array correctly? */
    @Test
    public void findMiddleAllowsEmptyTest() {
        // add code here
    }

    /* Does the sum method handle summing even values correctly? */
    @Test
    public void sumEvenTest() {
        // add code here
    }

    /* Does the sum method handle summing odd values correctly? */
    @Test
    public void sumOddTest() {
        // add code here
    }
}
#

ok ive tried everything i can think of

#

i dont understand how to do this without knowing the input??

#
package unittestingarrays;

// IMPORTANT: do not change the below ArrayUtilities class
public class ArrayUtilities {
    /**
     * Returns the value in the middle of the given array of non-negative
     * values.
     *
     * If the array of values has an even length, the value closer to the
     * end of the array of the two middle values is returned.
     * If the array is null or empty, -1 is returned.
     *
     * @param values non-negative values to find middle value in
     * @return value in the middle of the given array; -1 if null or empty
     */
    public int findMiddle(int[] values) {
        if (values == null || values.length == 0) {
            return -1;
        }
        int middleIndex = values.length / 2;
        return values[middleIndex];
    }

    /**
     * Returns the sum of the elements of the given array, summing either only
     * even values or only odd values.
     *
     * @param values values to sum
     * @param even if true, sum even values, otherwise, sum odd values
     * @return sum of even or odd values in the array
     */
    public int sum(int[] values, boolean even) {
        int sum = 0;
        for (int value : values) {
            if (even) {
                // summing only even values
                if (value % 2 == 0) {
                    // this value is even, add it to the sum
                    sum += value;
                }
            } else {
                // summing only odd values
                if (value % 2 == 1) {
                    // this value is odd, add it to the sum
                    sum += value;
                }
            }
        }
        return sum;
    }
}
// ^^^ IMPORTANT: do not change the above ArrayUtilities class
#

so ive tried likek

#

assigning int[] a = {1,2,3}

#

and running the method on that

#

or

#

ArrayUtilities a = {1,2,3}

#

or going arrayUtilities.findingMiddle()

#

but none of these work?

#

idk if theres smth obvious i dont know about junit?

still nest
#
int[] a = {1,2,3};
int result = arrayUtilities.findingMiddle(a);
#

is that what you tried?