why JTest can not generate negative int for me

LegacyForumLegacyForum Posts: 1,669 ✭✭
edited December 2016 in Jtest
My class has a addBalance(int money) method, why test cases generated by JTest always generate positive param for this method, why not generate negative param
calss constructor seen below:
public BankAccountSimple(String customer, int deposit) {
if (!validCustomer(customer)) {
throw new IllegalArgumentException("Bad Customer Name");
}
this.customer = customer;
balance = deposit;
}

method seen below:
public void withdraw(int amount) {
balance = balance - amount;
}




test case seen below:

/**
* Test for method: withdraw(int)
* @see BankAccountSimple#withdraw(int)
* @author Jtest
*/
public void testWithdraw3() {
BankAccountSimple THIS = new BankAccountSimple("0", 0);
// jtest_tested_method
THIS.withdraw(0);
int var0 = THIS.getBalance();
assertEquals(0, var0); // jtest_unverified
boolean var1 = THIS.isVIP();
assertEquals(false, var1); // jtest_unverified
}

/**
* Test for method: withdraw(int)
* @see BankAccountSimple#withdraw(int)
* @author Jtest
*/
public void testWithdraw4() {
BankAccountSimple THIS = new BankAccountSimple("0", 7);
// jtest_tested_method
THIS.withdraw(7);
int var0 = THIS.getBalance();
assertEquals(0, var0); // jtest_unverified
boolean var1 = THIS.isVIP();
assertEquals(false, var1); // jtest_unverified
}

Comments

  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    Jtest's automatically generated test cases serve two purposes. First, Jtest analyzes the internal structure of the method and tries to generate a set of inputs that will expose an uncaught runtime exception such as a NullPointerException, StringIndexOutOfBoundsException, etc. Second, Jtest generates test cases to try and achieve as much coverage as possible. Jtest analyzes the method branches and will generate inputs to the method to execute each of the different branches. For the example you provided with the withdraw method. Jtest analyzes the method and tries to generate inputs to cover the balance = balance - amount; statement and also tries to expose a hidden runtime exception for that line.

    Notice that these types of tests are not testing the specification of the method, making sure that you get the intended result when subtracting amount from balance. Since Jtest is unable to discern the correct specification just by analyzing the methods, it is up the user to provide the specification tests. Since all of the Jtest test cases are stored in the form of a Junit class, you can easily add your own test cases to the test class or extend one of the automatically generated test cases.

    One way to allow Jtest to automatically validate the specification of the code would be to incorporate design by contract into the methods. By adding preconditions and postconditions to the methods, Jtest will automatically create test cases that verify the functionality described in your DbC contracts and will report situations where DbC contracts are violated. More information about Design by Contract can be found in the Jtest Documentation under Jtest User's guide | Concepts | Design by Contract. The language specification can be found in the Jtest Documentation under Jtest User's guide | Reference | The Design by Contract Specification Language.
Sign In or Register to comment.