Simple Unit Test With Netbeans 6.1 and JUnit

Most people think,
Great God will come from the skies,
Take away everything
And make everybody feel high.
But if you know what life is worth,
You will look for yours on earth:
And now you see the light,
You stand up for your rights.
        – Get Up Stand Up, Bob Marley

Hi guys! Udah lama gak utak atik java karena project disini pake .NET. Kemaren malam ketika sedang nganggur dan utak atik netbeans bwat ngerefresh pikiran, saya menemukan fitur netbeans keren yaitui automated code generation untuk unit testing menggunakan framewrk JUnit . Just like Jeff Atwood said ” A feature is not  a feature until some user used it “,  saya juga baru sadar kemaren klo ada fitur seperti itu di netbeans 6.1. Dengan fitur ini, developer dimudahkan untuk membuat unit test karena Netbeans akan otomatis membuat test class dan test casenya. Lets start coding for further details.

Untuk yang belum familiar dengan unit testing dan JUnit, bisa membaca artikel dibawah ini untuk pendahuluan :

Mari kita mulai dengan membuka netbeans 6.1.

Buat sebuah java project baru

Gak wajib sebenarnya, cuman memudahkan aja. Lanjut ke langkah selanjutnya.

Buat class dengan return valued dan void method

Setelah mensetup project netbeans, selanjutnya kita buat class yang berisi return valued method dan void method. Tujuannya adalah melihat hasil generasi kode netbeans terhadap dua method yang berbeda tersebut. Anda juga bisa bereksperimen dengan menggunakan Main method atau method yang mereturn Object. Untuk kali ini, kita buat sangat simple saja. Sebuah class yang berisi operasi dasar matematika.


public class SimpleMath {
            
        public int doAddition(int a, int b ){
                                 
            return a + b ;
        }
        public int doSubtraction(int a, int b){
                  
            return a / b;
           
        }
        public void printAddition(int a, int b){
       
                 System.out.println("var1 = "+a+" , var2 = "+b+" " +
                         "hasilnya adalah = "+doAddition(a, b));
                       
        }
    }

Keep the class really really simple karena yang akan kita pelajari disini adalah bagaimana netbeans membuat kode test class secara automatis.

Let the magic do the test code

Pilih menu create Junit test (Tools > Create JUnit test. Setelah itu muncul dialog window yang berfungsi mengatur parameter test class yang dibuat. Setelah itu klik oke dan voila!! test class sudah otomatis dibuat di dalam folder Test Package. Anda juga dapat menggunakan keyboard shortcut Ctrl+Shift + U.

Netbeans menu

Masukkan parameter test

Edit parameter test pada test class. Sebelum menjalankan test class jangan lupa untuk meremove method fail() di masing masing method test case.

Jalankan test class

Jalankan test class pada test package. Palette netbeans unit test akan keluar dan menunjukkan apakah class SimpleMath lolos atau tidak dari unit test.

Test result palette

Mudah sekali bukan. Fitur test code generation ini akan sangat membantu karena membuat test case merupakan salah satu pekerjaan yang membosankan dan melelahkan sehingga seringkali dilewatkan oleh developer. With netbeans, its just as simple as pressing Ctrl + shift + U.

Saya juga membuat file .ppt nya in case topik ini akan dipresentasikan entah dimana (heheheh). PPT nya bisa di download di :

My Slideshare Account 
simple-java-unit-testing-with-junit-4

Sekian dulu temen temen. Have a nice coding day.

😀

7 thoughts on “Simple Unit Test With Netbeans 6.1 and JUnit

  1. answer for agungfirmansyah :
    you can use this script :
    your code here

    you can fill any code type in there like html, java etc

    P.S put those script at visual mode… not on html mode ok?

    nice post mr. kiki…

    Regards,

    parvian

  2. mas, aq kemarin ngopy file yang .class aja, tp aq bingung cara bukanya di netbeans. yang ketemu hanya open project. tolong ya..
    maklum masih pemula

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.