Simple Unit Test With Netbeans 6.1 and JUnit

Agustus 27, 2008

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.

:D

Entry Filed under: Java. Tag: , , .

5 Comments Add your own

  • 1. agungfirmansyah  |  Agustus 27, 2008 at 1:36 pm

    wah keren nih.
    Tp ada satu pertanyaan.

    Gmn cara nulis code di wp supaya format tampilannya jadi kaya gitu? :smile:

    Balas
  • 2. parvian  |  Agustus 27, 2008 at 8:36 pm

    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

    Balas
  • 3. Riyanto Jayadi  |  Agustus 28, 2008 at 2:16 pm

    Weleh……Welehh,,
    Masih sempat-sempat nya aja bikin hal keren
    Memang Mas Gembel

    Balas
  • 4. agungfirmansyah  |  Agustus 30, 2008 at 8:37 pm

    Wah, terimakasih Mas…

    Balas
  • 5. dewi  |  Desember 29, 2008 at 11:23 am

    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

    Balas

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Who Am I??

Masih mahasiswa sistem informasi ITS...

 

Spreadfirefox Affiliate Button

 



 

Click to view my Personality Profile page

 

View Kiki Ahmadi's profile on LinkedIn

 

Profil
Profil Facebook Kiki Ahmadi

My Slideshare

RSS JavaBonek

RSS Planet Klaser

Artikel Terbaru

Posting Laris

I used these



 



 

Kalender bro!!!

Agustus 2008
S S R K J S M
« Jul   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

Arsip

Blogroll

Inspiring Blog

JavaUserGroup

NetbeansUserGroup

Open Source Movementarians

Temen temen Sistem Informasi ITS

The Greatest Chat Friend

tugupahlawan.com

Yang komen

indresvari di About Me
obliviousme di Band : Efek Rumah Kaca
ciput mardianto di About Me
ciput mardianto di My CV
rina di Danau Matano : The Introd…

My Netbeans

Halaman

Meta