[JAVA-UT] 2. The scientific steps of writing UT

This section demonstrates how to write a ut.
First give a class Result.

public Class Result {
  private String downLoadStatus;
  private String failureReason;

  public Result(String status, String reason) {
    downLoadStatus= status;
     failureReason= reason;
  }

  @Override
  public String toString() {
    return "downLoadStatus: "+ downLoadStatus+ ", "
               +  "failureReason: " + failureReason;
        }
}

In class Result:
attribute downLoadStatus, indicating the status of download, such as "Ongoing", "Ok", "Fail".
The attribute failureReason indicates the specific reason for the failure, such as "cannot connect".
The method toString is used to combine the two property values ​​into a string.

Next, start writing ut!
Step 1: Determine your goals.
Remember, ut is a method-level test.
In this case, ut is a test of the toString method.
Can the toString method combine two values ​​into one value according to the specified format?
That's what ut is trying to solve.
That is what it aims to achieve.

Step 2: Come up with a hypothesis.
Assuming toString functions correctly.

Step 3: Infer the conclusion.
If the toString function is normal, then when an object is instantiated with "Fail", "cannot connect", and the toString method is executed, its return value should be equal to expectedString.
Expressed in java statement as follows:

 Result result = new Result("Fail", "cannot connect");
 String expectedString = "downLoadStatus: Fail, failureReason: cannot connect";

Step 4: Take the test.
On the result object, execute the toString method and record the actual value.

Expressed in java statement as:
String realString = result.toString();

Step 5: Compare the results.
Is the actual return value of toString equal to expectedString?
Compare the actual return value of the toString method with expectedString.
Expressed in java statement as:
Assert.assertEquals(expectedString, realString);
if equal, assertEquals does nothing.
If they are not equal, an exception is thrown.
By comparing the results, we can see whether toString functions as expected.
If an exception was thrown, it was different than expected.

Step 6: Write the code
First, create a class.
Named ResultTest, indicating that this class is for testing Result.

public class ResultTest {

}

Second, create a method.
Put the java statements from steps 1-5 in this method:

  @Test
  public void should_returnString() {
    // given
    Result result = new Result("Fail", "cannot connect");
    String expectedString = "downLoadStatus: Fail, failureReason: cannot connect";

    // when
    String realString = result.toString();

    // then
    Assert.assertEquals(expectedString, realString);
  }

This method is annotated with @Test.
A @Test annotated method, in Junit, is used to verify a function.
A class can contain several Test methods.

Please pay attention to the declaration of this method:
public
return value void
parameter is empty
These are the rules and must be followed.

The final code is as follows:

public class ResultTest {
  @Test
  public void should_returnString() {
    // given
    String expectedString = "downLoadStatus: Fail, failureReason: cannot connect";
    Result result = new Result("Fail", "cannot connect");

    // when
    String realString = result.toString();

    // then
    Assert.assertEquals(expectedString, realString);
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325168989&siteId=291194637