<Previous Exercise | TOC | Next Exercise>
Exercise 2: Add a test fragment with a simple JUnit test
In this exercise, we will add a simple JUnit test in a fragment to the tychodemo.bundle.
We will run the test from eclipse as a JUnit plugin test and then show how the test can
be built and run by Tycho simly by adding a Maven project file pom.xml.
A failed test will also fail the headless build.
As a starting point for your convenience, the result of step 1 Create an RCP bundle has been refactored:
Common settings have been extracted to a parent POM tychodemo.parent from which all modules will inherit and which defines the reactor modules.
- Select File > Switch Workspace > Other... and choose folder Exercise_02_Add_Test_Fragment.
- Import the 2 projects from this folder into the new empty eclipse workspace using File > Import > Maven > Existing Maven Projects
- Right-click on tychodemo.parent, Run As > Maven install
The build should succeed and build 2 modules:
- Create a new fragment project tychodemo.bundle.tests with fragment host tychodemo.bundle:
File > New > Project > Plug-in Development > Fragment Project
Notes:
- Make sure the new project root folder is located next to the existing modules
(Uncheck "Use default location" in the project creation wizard and enter location if necessary)
- As opposed to classic maven projects, tests are always in a separate module
because otherwise we would pollute productive code with test-scoped dependencies in MANIFEST.
We use a fragment because:
- otherwise we would have to export packages of classes under test in MANIFEST
- we want to be able to test package-private members
- In the fragment project, create a new JUnit test case tychodemo.SimpleTest
- File > New > Other > JUnit Test Case
- choose tychodemo.bundle.ApplicationWorkbenchAdvisor as class under test
- choose getInitialWindowPerspectiveId() as test method for which a stub will be created
- confirm "Add org.junit to required bundles" if prompted
- Run the test: Right-click, Run As > JUnit Plugin Test
- expected result: the test should fail with "java.lang.AssertionError: Not yet implemented"
- Note: make sure that both tychodemo.bundle as well as tychodemo.bundle.tests are active
in the "Plugins" tab of the launch configuration
(Use "Launch with: Plug-ins selected below only")
- Add the fragment module to the reactor and run the build:
- Add the module to <modules> section of the parent pom.xml:
<module>../tychodemo.bundle.tests</module>
- Right-click tychodemo.parent, Run As > Maven install
Expected result: build should fail because of the same test failure we just saw in eclipse:
- Implement the test:
- assert that tychodemo.bundle.perspective is the return value of
tychodemo.bundle.ApplicationWorkbenchAdvisor.getInitialWindowPerspectiveId()
- test should now succeed when executed in eclipse:
- Run As > Maven install should also succeed
<Previous Exercise | TOC | Next Exercise>