junit with eclipse · knowledge of core java, unit testing is mandatory for this document. i have...

15
GRASSFIELD Junit with Eclipse JUNIT WITH ECLIPSE Version 0.1 GRASSFIELD.org MARCH 2008 PUBLIC 1

Upload: others

Post on 18-Oct-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

JUNIT WITH ECLIPSEVersion 0.1

GRASSFIELD.orgMARCH 2008

PUBLIC 1

Page 2: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

SOME RIGHTS RESERVEDThis work is licensed under the Creative Commons Attribution 2.5 India License. To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/in or send a letter to Creative Commons, 171

Second Street, Suite 300, San Francisco, California, 94105, USA.

PUBLIC 2

Page 3: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

VERSION HISTORYDATE VERSION DESCRIPTION AUTHOR

03/30/08 0.1 Initial Draft Pandian R

PUBLIC 3

Page 4: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

1 About this documentThis document can be used as a nutshell guide to write test cases in junit

The audiences are intended to be developers/senior developers and testers. Knowledge of core java, unit testing is mandatory for this document.

I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document.

I may be poor in documenting. Please keep me informed about the grammatical or typo errors.

2 About JUnitJUnit is a successful unit testing framework. Its makes the automated unit testing process easier. Also It is an important tool for test driven development. It can be downloaded from www.junit.org

PUBLIC 4

Page 5: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

Table of Contents1About this document............................................................................................................................ 42About JUnit......................................................................................................................................... 41Testing sequence................................................................................................................................. 6

1.1Writing a Testcase........................................................................................................................ 61.2Creating a Test Suite.................................................................................................................. 11

PUBLIC 5

Page 6: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

1 Testing sequenceWe need to follow the following procedures for the test automation.

1. Write the test case

2. write the test methods in the test case

3. Prepare the test suite

4. Run the test

1.1 Writing a TestcaseI have written a stand alone java file org.grassfield.junit.MainClass to add, subtract, multiply and divide two integers. Lets now create a test case with Eclipse.

● Create a new test case

PUBLIC 6

Illustration 1: Create a new Test Case

Page 7: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

● Select the properties

PUBLIC 7

Illustration 2: Test case properties

Page 8: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

● select the class under test

PUBLIC 8

Illustration 3: Selecting the class under test

Page 9: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

● select the methods to be tested

● Now the test case template is ready. We need to implement suitable methods correctly for testing.

● setUp() method is used to create the environment to test the scenario. Here i am instantiating proper variables

a=10;b=5;mainClass = new MainClass();

● Implement all the test methods. For example, it checks for the equality between the output of the method and the product.public void testAdd() {

assertTrue(mainClass.add(a, b)==15);}

● Similarly testSubtrace(), testMultiply(), testDivide() methods are implemented.

PUBLIC 9

Illustration 4: selecting the methods to be tested

Page 10: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

● Show Junit view. At first you need to enable testing view. Select windows>show view>other>java>junit

PUBLIC 10

Illustration 5: selecting junit view

Page 11: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

● Run the test. See some test cases got failed.

PUBLIC 11

Illustration 6: junit view

Page 12: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

● Incorrect methods have been rectified. Now all the cases are doing good

1.2 Creating a Test SuiteAs the test cases are being increased day by day, it will be very diffcult to group and maintain them. So, Test suites are useful to combine several tests and run them together. It is realtively simple task. The only thing we need to do is, we need to include a static method in all test classes

public static junit.framework.Test suite(){

return new JUnit4TestAdapter(TestMainClass.class);}

This method will be called by the TestRunner and the corresponding test will be added to suite.

PUBLIC 12

Illustration 7: all test cases passed

Page 13: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

To create a test suite, select File>new>other

PUBLIC 13

Illustration 8: test suite wizard

Page 14: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

select the test classes to be added to the suite

PUBLIC 14

Illustration 9: adding tests to suite

Page 15: JUNIT WITH ECLIPSE · Knowledge of core java, unit testing is mandatory for this document. I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document

GRASSFIELD Junit with Eclipse

Here is the test suite. For time being we have added only one test. Any number of tests can be added with this. Similarly, A Testsuite for a group of test suites is also possible.

PUBLIC 15

Illustration 10: running test suite