Fest Assert helps us much on conducting a fluency assertion. That's perfect. However, with the application under test (AUT) as Calculator, we need test several test cases.
Divide method
- Divide by zero
- Divide int/long by floating point number
- Divide large numbers....
It's hurt using pure java for it. We need a unit test framework to support that. XUnit framework is well-known so far. However, we have another one with an ambition of "Next Generation", TestNG. You may need to read more information of this framework at the
TestNG Website.
The following example we conduct unit tests for the divide method by using TestNG.
First of all, we need add dependency of TestNG to our project.
Please add
<groupId>org.testng</groupId> |
<artifactId>testng</artifactId> |
So, you pom.xml file now is
 |
Maven pom.xml with Fest Assert and TestNG
Next step, we need to create test class with TestNG, we don't need to create entry point method as main any more. the test method need an annotation as @Test
import org.testng.annotations.Test;
import static org.fest.assertions.api.Assertions.assertThat;
/**
* Created by nhien.truong on 2/28/14.
*/
public class DivideTestWithTestNG {
@Test
public void testDivide(){
Calculator calculator = new Calculator();
double param1=5;
double param2=7.5;
double expectedResult = param1/param2+1;
double actualResult = calculator.divide(param1,param2);
assertThat(actualResult)
.as("result of the divide method")
.isEqualTo(expectedResult);
}
}
Rightclick on the class in the left pane, choose run with TestNG, you will see the result like this
 |
| Result of TestNG |
|
Không có nhận xét nào:
Đăng nhận xét