Latest videos

Categories > JUnit tutorial > JUnit tutorial, how to create a test in Eclipse IDE part 2

JUnit tutorial, how to create a test in Eclipse IDE part 2


Let's implement this test. In this test I must have an instance of a tested class, which is TransformationService. Because we can (and will) have more tests, it is very useful to declare it as an attribute.

Next we must decide where we will create an instance of this attribute. For this I must say something more about workflow of a test case and test. As you can see, one unit test is a method, annotated with @Test. There are several other annotations. They are all optional and if you don't use them, you can delete them and associated methods. Method annotated with @BeforeClass will be executed before any tests and only once for all tests in one test case. Next will be executed tests in following order. Before every test will be executed method annotated with @Before. Next will be executed method annotated with @Test and after that will be executed method with @After. This sequence will be applied to all tests. After all tests, will be executed method annotated with @AfterClass.

Each unit test should be designed in isolation, that means that is should not depend on other unit tests. So creation of an instance of a class under test is usually done in method annotated with @Before annotation.

Now we can implement a test. We supply some input data, call the tested method and compare results with expected results. We can use for that several supplied assert methods. These methods are static methods from class org.junit.Assert, which was imported using "import static" keyword during creation of a test case using Eclipse.

Now I have successfully tested method transformData(). In case of a failure, there would be displayed error message. At any time you can easily re-run test.

comments powered by Disqus

Recommended materials

Share / Subscribe


Free online web Java example projects

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects:



Created by , professional Java and Oracle DB trainer. All Rights Reserved.

Other projects

This web site is monitored by FOSS Site monitoring application

My GitHub account