This lesson explains the basics of the guided test creation process. You will learn how to use UTA to:
- create a test,
- instantiate objects,
- update the test code,
- isolate the test,
- improve the test,
- make assertions.
In this lesson, you will create a unit test for the apply() method, which takes a transaction object. Creating a unit test for this method and isolating the test from its dependencies may be tedious and complex. Unit Test Assistant will guide to create an isolated test easier and faster.
- In the Package Explorer, to go jtest-examples> src/main/java> examples> nbank.
- Double-click Account.java to open the class in the editor.
- Click anywhere within the apply() method.
- Click the Regular action in the Unit Test Assistant view to create a JUnit test and automatically perform the initial test setup.
The test includes declarations of the required objects and variables, as well as default values indicated by comments.
- Select the line that contains the 'customer' object and lick Instantiate "customer" action in the Unit Test Assistant view to instantiate the object.
UTA will automatically update the test code:
- Replace the default UTA values with meaningful data, as indicated by the comments in the test code.
- Scroll down to locate the helper method that UTA automatically generated during test creation to mock the transaction object that is passed to the apply() method. UTA can use Mockito and Powermock frameworks for test isolation.
- Replace the default UTA values by setting the
trueand and updating the value of the
- Click Run testApply in the Unit Test Assistant view to execute the test.
The Recommendations view will display information that your test does not include any assertions. As the next step, you can manually add your own assertions or use UTA to generate the assertions for you by using the tracking option. It allows UTA to monitor an object and compare its state before and after the execution of a method. The insight into how particular values changed during execution enables UTA to provide you with valid assertions
- Click Track apply action link in the 'Assertions' recommendation to monitor the method call and display available assertions.
- Click the Make assertions action link to update your test code with the available assertions.
- Rerun the test.