View RSS Feed

JUnit

JUnit unit testing framework which is a central element of the Extreme Programming (XP) testing practice.

  1. Motivation for unit tests

    by , 05-21-2011 at 10:53 PM (My Java Tips)
    Unit tests are essential part of software development especially. In complex and large softwares, introducing new changes in very tricky as it can break some other part. If you have clearly defined test cases, then you can check the working of newly introduced change and make sure it works as expected.

    Unit tests are small snippets of code that are written to check a particular method or class. With unit tests in place, when a change is made to the code we can simply run all the tests ...
    Tags: unit test Add / Edit Tags
    Categories
    JUnit
  2. Importing junit.jar

    by , 05-21-2011 at 10:52 PM (My Java Tips)
    Before you start writing unit tests, you must import junit.jar, so we have access to the testing framework. Follow these steps to import the required jar:

    • Right-click on the project name, and choose Properties.
    • In the tree on the left, select Java Build Path.
    • Next, choose Add External JARs… and browse to find junit.jar.


    It will be located in "<eclipsedir>\plugins\org.junit_<version number>\junit.jar".

    Once you successfully ...
    Tags: jar Add / Edit Tags
    Categories
    Eclipse , JUnit
  3. Sharing setUp() and tearDown() code for all tests - I

    by , 05-21-2011 at 10:29 PM (My Java Tips)
    You may wish to share the code written in setUp() and tearDown() methods of your JUnit tests. This clearly will indicate that you have excessive coupling in your design. Coupling is not bad always but if more tests share the same test fixture state, then this indicates that the classes under test have some undesirable dependencies.

    You may wish to remove these dependencies but its another stories. As I said, coupling or dependencies are not always bad. But if you wish to keep coupling ...
    Tags: setup, teardown Add / Edit Tags
    Categories
    JUnit
  4. Sharing setUp() and tearDown() code for all tests - II

    by , 05-21-2011 at 10:28 PM (My Java Tips)
    This post presents an example that shows how to use @BeforeClass and @AfterClass annotation.

    Java Code:
        public class SimpleTest {
        
            private Collection collection;
    	
            @BeforeClass
            public static void oneTimeSetUp() {
                // one-time initialization code        
            }
    
            @AfterClass
            public static void oneTimeTearDown() {
                // one-time cleanup code
    ...
    Tags: setup, teardown Add / Edit Tags
    Categories
    JUnit
  5. Running JUnit using Ant

    by , 05-21-2011 at 10:26 PM (My Java Tips)
    You may want to define build script using ant for your application. I will write about how to create ant script to run ant.

    Step 1: define ant properties

    XML Code:
    <property name="src" value="./src" />
    <property name="lib" value="./lib" />
    <property name="classes" value="./classes" />
    <property name="test.class.name" value="com.xyz.MyTestSuite" />
    ...
    Tags: ant Add / Edit Tags
    Categories
    JUnit
  6. Costs of Unit Testing

    by , 05-21-2011 at 10:09 PM (My Java Tips)
    Unit testing brings a lot of benefits but there is some cost for this. Lets talk about this.

    Unit tests require skill and time. Often managers dont appreciate unit tets because they regard it something othere than development. Ofcource client in not interested in unit tests but management needs to understand that these tests will save a lot of debugging effort in future.

    For unit testing to really deliver, all developers need to use it. Mostly due to lack of communicationm ...
    Tags: unit testing Add / Edit Tags
    Categories
    JUnit