richy rich
richy rich

Reputation: 13

What do i do to create a unit test for the code below

public String getBloodPressureLevel() {

    if (this.systolic > MedicalRecord.AT_RISK_SYSTOLIC_UPPER_BOUND
            || this.diastolic > MedicalRecord.AT_RISK_DIASTOLIC_UPPER_BOUND) {
        return "High";
    } else if ((this.systolic >= MedicalRecord.AT_RISK_SYSTOLIC_LOWER_BOUND
            && this.systolic <= MedicalRecord.AT_RISK_SYSTOLIC_UPPER_BOUND)
            || (this.diastolic >= MedicalRecord.AT_RISK_DIASTOLIC_LOWER_BOUND
                    && this.diastolic <= MedicalRecord.AT_RISK_DIASTOLIC_UPPER_BOUND)) {
        return "At risk";
    } else {
        return "Normal";
    }
}

Test cases i'm supposed to have

i'm just not sure how to go about this

Upvotes: 0

Views: 32

Answers (1)

GhostCat
GhostCat

Reputation: 140447

You start by reading a tutorial on how JUnit works, for example this here.

Then you think about the different corners of your production code, to then do things like

@Test
public void testForHigh() {
   YourClass underTest = new YourClass(/*systolic*/ 200, /*diastolic*/ 100);
   assertThat(underTest.getBloodPressureLevel(), is("High"));
}

( where is() is a hamcrest matcher, allowing to write more human readable test conditions and where I assume that you have some class that gets instantiated using a systolic and diastolic data point )

The real point here is: you have to research the tools you are supposed to use, and then you go and implement at least one method for each possible condition, representing the 11 different tests in your input.

( And no, I am not going to give you more code. The above is meant as inspiration to enable you to do this work yourself. )

Upvotes: 3

Related Questions