This tutorial explains how ecFeed models can be used to provide data for the JUnit test runner. Basic knowledge of Java is required to fully understand the functionality. The following examples use the Eclipse IDE, but any Java IDE that supports JUnit can be used.
It is assumed that the previous tutorials were followed and you have access to the model created at the end of the previous tutorial. If you do not have it, you can get it here.
Note, that JUni5 is required to run ecFeed tests.
First, a new Java project must be created. Do it in your favorite IDE and call it 'tutorial'.
To be able to run the JUnit test, you have to implement it as Java code. Apart from the class com.example.test.LoanDecisionTest
, enums for all user type parameters must be created. You need to create the following files:
com.example.test.DocumentType.class
:
package com.example.test;
public enum DocumentType{
PASSPORT, DRIVERS_LICENSE, PERSONAL_ID
}
com.example.test.EmploymentStatus.class
:
package com.example.test;
public enum EmploymentStatus{
UNEMPLOYED, SELF_EMPLOYED, WORK_CONTRACT_TEMPORARY, WORK_CONTRACT_INDEFINITE, RETIRED
}
com.example.test.Gender.class
:
package com.example.test;
public enum Gender{
MALE, FEMALE
}
com.example.test.LoanDecision.class
:
package com.example.test;
public enum LoanDecision{
POSITIVE, NEGATIVE, INCONCLUSIVE
}
Finally, let's implement the LoanDecisionTest class:
com.example.test.DocumentType.class
:
package com.example.test;
public class LoanDecisionTest {
public void generateCustomerData(String familyName, String firstName, Gender gender, int age, String documentSerialNumber, DocumentType documentType) {
System.out.println("generateCustomerData(" + familyName + ", " + firstName + ", " + gender + ", " + age + ", " + documentSerialNumber + ", " + documentType + ")");
}
public void processLoanApplication(int age, String firstName, String familyName, Gender gender, EmploymentStatus employmentStatus, long lastYearIncome, LoanDecision loanDecision) {
System.out.println("processLoanApplication(" + age + ", " + firstName + ", " + familyName + ", " + gender + ", " + employmentStatus + ", " + lastYearIncome + ", " + loanDecision + ")");
}
}
You can download this file and unpack it in the source folder of your project. After that, you should have the following project tree:
The data provider connects to the ecfeed.com server to get data for executed tests. Before that, the server must generate a keystore that will be used for secure communication. Go to the settings page for your account and click "Generate keystore". Choose a label for the keystore (for example "my keystore") and click on "Generate". Download the generated keystore to the "ecfeed" folder in your home directory. It will be used to authenticate you on the server. The keystore contains your private key used in communication. It is not stored on the server. If you loose it, you need to generate a new one.
Before going to test execution, the 'ecfeed' dependency, which can be found here, must be added to the project.
Next, to execute JUnit tests with parameters provided by ecFeed, each method should be annotated with @EcFeedTest
. Additionally, you have to provide the model number using the @EcFeedModel
annotation. Do it in the LoanDecisionTest
class (replace "XXXX" with the model id of your model - it can be copied from the address bar of your browser):
package com.example.test;
import com.ecfeed.junit.annotation.*;
@EcFeedModel("XXXX-XXXX-XXXX-XXXX-XXXX")
public class LoanDecisionTest {
@EcFeedTest
public void generateCustomerData(String familyName, String firstName, Gender gender, int age, String documentSerialNumber, DocumentType documentType) {
System.out.println("generateCustomerData(" + familyName + ", " + firstName + ", " + gender + ", " + age + ", " + documentSerialNumber + ", " + documentType + ")");
}
@EcFeedTest
public void processLoanApplication(int age, String firstName, String familyName, Gender gender, EmploymentStatus employmentStatus, long lastYearIncome, LoanDecision loanDecision) {
System.out.println("processLoanApplication(" + age + ", " + firstName + ", " + familyName + ", " + gender + ", " + employmentStatus + ", " + lastYearIncome + ", " + loanDecision + ")");
}
}
Now run the tests. Voila! By default the runner asks the ecFeed server to generate cartesian product of all choices.
The generator can be configured by the @EcFeedInput
annotation, which can be added to methods or to the main class (in the second case all methods are affected):
@EcFeedInput(input="'dataSource':'genRandom', 'properties':{'length':'5'}")
This will cause that for each method in the class, random generator will generate 5 test cases.
If your model contains already generated tests, you can use them directly for executing methods with JUnit. You need to change the data source in the annotation to static and tell ecFeed which test suites you want to use. Make sure that the method processLoanApplication
in the model contains some tests and add the following annotation:
@EcFeedInput("'dataSource':'static', 'testSuites':'ALL'")
If you want to execute some specific test suites, you can list them in the annotation in the following way:
@EcFeedInput("'dataSource':'static', 'testSuites':['suite 1', 'suite 2']")
So far, the test data was generated on the ecFeed server. It can be generated locally as well. To do it, you need to export the model. Open it on te web page and select 'File' -> 'Export'. Next, save the file in the src folder in the 'tutorial' project.
All you need to do now it to tell the JUnit runner that the data should be generated locally. It can be done with the @EcFeedModel
annotation:
@EcFeedModel("src/tutorial.ect")
The annotation can be applied to a class or a method. In the second case, it will overwrite the class annotation for this method only.
It is possible to generate test data without access to the ecFeed server. In this case, the embedded generator can be used. It provides a set of predefined values for every method parameter and enum. To activate it, simply omit the @EcFeedModel
annotation.