Creating a Java class
In this section, you will create a new Java class and add methods using
code generation actions.
- In the Package Explorer view, select the JUnit project.
Click the New Java Package button in the toolbar, or select New > Package from the project's
context menu .
- In the Name field, type test as the
name for the new package. Then click Finish.
- In the Package Explorer view, select the new test package
and click the New Java Class button in the toolbar.
- Make sure that JUnit appears in the Source Folder
field and that test appears in the Package
field. In the Name field, type MyTestCase.
- Click the Browse button next to the Superclass
field.
- In the Choose a type field in the Superclass Selection
dialog, type Test to narrow the list of available superclasses.
- Select the TestCase class and click OK.
- Select the checkbox for Constructors from superclass.
- Click Finish to create the new class.
- The new file is opened in the editor. It contains the new class,
the constructor and comments. You can select options for the creation and
configuration of generated comments in the Java preferences (Window > Preferences > Java > Code Style > Code Templates).
- In the Outline view select the new class MyTestCase. Open
the context menu and select Source > Override/Implement Methods....
- In the Override Methods dialog, check setUp() and tearDown()
and click OK. Two methods are added to the class.
- Change the body of setUp() to
container= new Vector();
- container and Vector are underlined with a problem
highlight line as they cannot be resolved. A light bulb appears on the marker
bar. Set the cursor inside Vector and press Ctrl+1 (or use
Edit > Quick Fix from the menu bar). Choose Import 'Vector' (java.util).This adds the missing import declaration.

Set the cursor inside container and press Ctrl+1. Choose Create
field 'container' to add the new field.
- In the Outline view, select the class MyTestCase. Open the
context menu and select Source > Generate Getters and Setters....
- The Generate Getter and Setter dialog suggests that you create the methods getContainer
and setContainer. Select both and click OK. A getter
and setter method for the field container are added.
- Save the file.
- The formatting of generated code can be configured in Window
> Preferences > Java > Code Style > Formatter. If you use a prefix
or suffix for field names (e.g. fContainer), you can specify this in Window
> Preferences > Java > Code Style so that generated
getters and setters will suggest method names without the prefix or suffix.
Java views
Java editor
Using quick fix
Creating Java elements
Generating getters and setters
New Java Class wizard
Source actions
Quick Fix
Override Methods dialog
Generate Getter and Setter dialog
Code formatter preference page
Code style preference page
Code templates preference page