Hello World from OSS Silicon Valley


HowToUse/JUnit/4.8

_ Prerequisite

_ Install&Setup

Step.1
(We use maven here)

_ HowToUse

_ Create project.

Step.1
Launch Cygwin.
Step.2
Create the most simple maven project.
$ mvn archetype:create \
 -DgroupId=org.test \
 -DartifactId=junit
CreateMavenProject_fig1.png
Step.3
Sample production code App.java and AppTest.java will be created. You can execute testing by the following command.
$ mvn test
CreateMavenProject_fig2.png

_ Import mvn project to eclipse

Step.1
Luanch eclipse.
Step.2
Select File -> Import.
ImportMavenProject_fig1.png
Step.3
Select "Existing Maven Projects" and click "next" button.
ImportMavenProject_fig2.png
Step.4
Click "Browse" button and select created maven project folder.
ImportMavenProject_fig3.png
ImportMavenProject_fig4.png
Step.5
Click "Finish" button. And you have imported eclipse project.
ImportMavenProject_fig5.png

_ Prepare source code.

Step.1
Create sample production code, "Calc" as follows.
CreateCode_fig1.png

For the source code, you can refer sample code - Calc

Step.2
Next, create test code. Choose src/test/java package folder and select "File"->"New"->"JUnit Test Case" (lower one).
CreateCode_fig2.png

JUnit4 is 4 phase testing framework and we can use the following annotation.

ANNOTATIONDESCRIPTION
@TestTest Case
@IgnoreIgnore this method from test Case
@Before (SetUp)Before each Test Case, this method will be called out.
@After (TearDown)After each Test Case, this method will be called out.
@BeforeClass SetUpBeforeClass)Before all of the Test Cases in this Class, this method will be called once.
@AfterClass (TearDownAfterClass)After all of the Test Cases are finished, this method will be called once.
@RunWithThis is required for test suite program.
Step.3
Select "New JUnit 4 test" and click "Finish" button.
Step.4
When the JUnit 4 is not on the build path, eclipse ask if we setup it or not. Here, we choose "Not now" and click "OK" button.
CreateCode_fig4.png
Step.5
Edit test code. For assertion method ("assertThat"), it is recommendableto use "is", one of the method in CoreMatchers class which compares expected result and actual result using Java's "equal" method. You can refer sample code from sample code - CalcTest.java

Now, we don't have junit library, so eclipse show errors, but you don't need to mind it. We will solve this using maven in the next step.

CreateCode_fig3.png
Step.5
Open POM.xml and edit it as following.
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.8.1</version>
  <scope>test</scope>
</dependency>

You can refer sample code from sample code - POM.xml

After you edit the POM.xml and save it, maven automatically download necessary library to local machine and error messages eclipse shows will be disappeared.

Step.6
Execute the following command. Maven download JUnit4 and setup it to the build-path based on the description in POM.xml.
$ cd <Maven project directory>
$ mvn test
CreateCode_fig7.png

Now we have JUnit in project, too.

CreateCode_fig6.png

_ Create Test Suite

Step.1
As long as you use mvn test, all of the test code in test source director will be executed, but you can also create test suite code as follows. Create new class named "AllTest" in test code directory.
Step.2
Edit test suite code using @RunWith annotation. You can refer sample code - AllTest.java
Step.3
When you run mvn test, you have the result for 8test cases because AllTest is composed of 4 test cases.
CreateTestSuite_fig1.png

_ Author

S.Yatsuzuka

Last-modified: 2015-08-17 (Mon) 02:41:34 (3174d)