Creating stubs for Java components
Stubs are classes that provide replacement implementations for the actual classes that the code you are testing interacts with. You can define a stub of a Java class and reuse that stub in multiple tests. Once you create the stub, you define its behavior in the stub data table or you enter code in the user code class that is associated with the stub.
To create a stub for a Java component:
- Click File > New > Other > Component Test > Java > Java Component Stub and click Next.
- On the first wizard page, do one of the following:
- Select the test project that will contain the stub and click Next.
- Or click New to create a new test project.
- Select the Java source files or libraries that you want to stub and click Finish. If the stub component already exists, the wizard prompts you to replace the existing stub. If you select Yes or Yes to All, then any changes you have made to the existing stub will be overwritten. If you select No or No to All, then the existing files remain unchanged and a new stub is not generated.
- In the Test Navigator view, double-click the test suite that you want to add the stub to. This opens the Test Suite editor.
- In the Test Suite editor, click the Stubs tab, click Add, select the stub you want to add to the test suite, and click Finish.
Note: To create a new stub and add it automatically to the test suite, click New and proceed with Step 3.
To replace the stub with the real class at any time, simply remove the stub from the test suite.
Related concepts
Stub data tables
Stub behavior
Related tasks
Editing stubs in a test suite