1 2 Previous Next 15 Replies Latest reply: Apr 18, 2008 8:54 AM by mlk RSS

    How to create test classes with JUnit?

    807591
      Hello,

      I need to test my classes with JUnit. My editor is BlueJ.

      This is my class:
      package model;
      
      import java.util.*;
      
      public class Category
      {
          private String name;
          private List<Appointment> appointments;
          private List<ToDo> toDos;
          
          public Category(String name)
          {
              this.name = name;
              appointments = new ArrayList<Appointment>();
              toDos = new ArrayList<ToDo>();
          }
          
          public String getName()
          {
              return this.name;
          }
          
          public void setName(String newName)
          {
              this.name = newName;
          }
          
          public void addAppointment(Appointment a)
          {
              appointments.add(a);
          }
          
          public void removeAppointment(Appointment a)
          {
              appointments.remove(a);
          }
          
          public void addToDo(ToDo t)
          {
              toDos.add(t);
          }
          
          public void removeToDo(ToDo t)
          {
              toDos.remove(t);
          }
          
          public List<Appointment> getAppointments()
          {
              return appointments;
          }
          
          public List<ToDo> getToDos()
          {
              return toDos;
          }
      }
      This is my JUnit test class code so far:
      package model;
      import java.util.*;
      
      
      /**
       * The test class CategoryTest.
       *
       * @author  (your name)
       * @version (a version number or a date)
       */
      public class CategoryTest extends junit.framework.TestCase
      {
         private Category cat;
         
         
         
          protected void setUp()
          {
              cat = new Category("Kategori");
          }
      
          public void testGetAndSetName()
          {
              assertTrue(cat.getName().equals("Kategori"));
              cat.setName("Kategori 2");
              assertTrue(cat.getName().equals("Kategori 2"));
              assertFalse(cat.getName().equals("Kategori"));
          }
      }
      Any hint would be appreicted.

      Best regards,
      Turtle
        • 1. Re: How to create test classes with JUnit?
          807591
          You should read the API docs for JUnit, in particular the class org.junit.Assert. Notice, for instance, that there's a bunch of overloaded assertEquals methods that would be preferable to using the equals methods of various classes under test. Other than that, you seem to already know how to create test classes, so what's the problem? Read [this book|http://www.amazon.co.uk/Test-Driven-Development-Practical-Guide/dp/0131016490/ref=sr_1_1?ie=UTF8&s=books&qid=1208506543&sr=8-1] for a good guide on unit testing
          • 2. Re: How to create test classes with JUnit?
            mlk
            I would not write a unit test for that. It has no logic to be tested.
            • 3. Re: How to create test classes with JUnit?
              807591
              It's simply a task out teacher has requested.

              What's the exact difference between creating a TestDriver class and then using JUnit?

              Thanks,
              Jonas
              • 4. Re: How to create test classes with JUnit?
                791266
                @Op. Tell your teacher that you are using an old version of JUnit. The newer version that is annotation based is much better.
                • 5. Re: How to create test classes with JUnit?
                  807591
                  Thanks, but we have to use this version.

                  We need to finish this task on Saturday so any help is much appreciated:)
                  • 6. Re: How to create test classes with JUnit?
                    mlk
                    jonasse wrote:
                    It's simply a task out teacher has requested.
                    In that case what you have done so far is fine. As George has posted use the assertEquals rather than assertTrue( a.equals( b ) ).
                    What's the exact difference between creating a TestDriver class and then using JUnit?
                    Stock Answer Time. What do you think, and we will tell you if you are right or wrong.
                    • 7. Re: How to create test classes with JUnit?
                      807591
                      Thanks for the fast response.

                      My idea of using JUnit would be that it automatically tests your methods - and you can run all test classes at the same time using BlueJ. Right or wrong?

                      Any idea of how the test class would look for:

                      private List<Appointment> appointments;
                      private List<ToDo> toDos;

                      Thanks again - much appreciated!
                      • 8. Re: How to create test classes with JUnit?
                        807591
                        jonasse wrote:
                        Thanks for the fast response.

                        My idea of using JUnit would be that it automatically tests your methods - and you can run all test classes at the same time using BlueJ. Right or wrong?
                        Not at the same time, consecutively. But yep, automatically is the key (as in, once you actually run the suite)
                        • 9. Re: How to create test classes with JUnit?
                          mlk
                          jonasse wrote:
                          Any idea of how the test class would look for:
                          Same as the other tests. Give it some data then verify that the data is set correctly.

                          Also think about edge cases (maybe removing something that does not exist) and verify bad data leads to the correct error condition.
                          • 10. Re: How to create test classes with JUnit?
                            807591
                            Thanks.

                            If anyone could write the code it would be much appreiated since Im quite lost on this one.

                            Thanks,
                            Jonas
                            • 11. Re: How to create test classes with JUnit?
                              807591
                              jonasse wrote:
                              If anyone could write the code it would be much appreiated since Im quite lost on this one.
                              You sound just like my project manager
                              • 12. Re: How to create test classes with JUnit?
                                807591
                                Hehe:)

                                This is my code:
                                   private Category cate;
                                        
                                        
                                        protected void setUp() {
                                        text = new Appointment("Aftale",TimeStampGenerator.generate(1, 1, 2009), TimeStampGenerator.generate(1, 2, 2009));
                                        cate = new Category("Aftaler");
                                    }
                                    
                                    
                                    public void testSetAndGetText() {
                                        assertTrue(text.getText().equals("Aftale"));
                                        text.setText("Andre aftaler");
                                        assertTrue(text.getText().equals("Andre aftaler"));
                                    }
                                    
                                
                                    public void testSetAndGetCategory()
                                    {
                                        assertTrue(text.getText().equals("Aftale"));
                                        text.setText("Andre Aftaler");
                                        assertFalse(text.getText().equals("Aftale"));
                                    }
                                    
                                     public void testGetAndSetStartTime() {
                                         assertTrue(text.getStartTime().equals(TimeStampGenerator.generate(1,2,2009)));
                                         text.setStartTime(TimeStampGenerator.generate(1,2,2008));
                                         assertTrue(text.getStartTime().equals(TimeStampGenerator.generate(1,2,2008)));
                                         assertFalse(text.getStartTime().equals(TimeStampGenerator.generate(1,2,2009)));
                                        }
                                     
                                }
                                The last method compiles, but I get a failure when I run the test. Says no exception message???

                                Thanks in advance,
                                Jonas
                                • 13. Re: How to create test classes with JUnit?
                                  807591
                                  jonasse wrote:
                                  Hehe:)

                                  This is my code:
                                     private Category cate;
                                  
                                  
                                  protected void setUp() {
                                  text = new Appointment("Aftale",TimeStampGenerator.generate(1, 1, 2009), TimeStampGenerator.generate(1, 2, 2009));
                                  cate = new Category("Aftaler");
                                  }
                                  
                                  
                                  public void testSetAndGetText() {
                                  assertTrue(text.getText().equals("Aftale"));
                                  text.setText("Andre aftaler");
                                  assertTrue(text.getText().equals("Andre aftaler"));
                                  }
                                  
                                  
                                  public void testSetAndGetCategory()
                                  {
                                  assertTrue(text.getText().equals("Aftale"));
                                  text.setText("Andre Aftaler");
                                  assertFalse(text.getText().equals("Aftale"));
                                  }
                                  
                                       public void testGetAndSetStartTime() {
                                           assertTrue(text.getStartTime().equals(TimeStampGenerator.generate(1,2,2009)));
                                           text.setStartTime(TimeStampGenerator.generate(1,2,2008));
                                           assertTrue(text.getStartTime().equals(TimeStampGenerator.generate(1,2,2008)));
                                           assertFalse(text.getStartTime().equals(TimeStampGenerator.generate(1,2,2009)));
                                          }
                                       
                                  }
                                  The last method compiles, but I get a failure when I run the test. Says no exception message???

                                  Thanks in advance,
                                  Jonas
                                  So this is where TDD kicks in. Your mission - should you choose to become test-infected - is to write tests that fail, preferably even don't compile, and then write the code that makes them pass. Compilation is no guarantee the thing will work. In all honesty, that test is probably too busy. Ideally, each test case has exactly one assert in it. That way, you can know instantly what's gone wrong. If you reply "but my code can't be tested like that" it's probably time to refactor it so that it can
                                  • 14. Re: How to create test classes with JUnit?
                                    796447
                                    The last method compiles, but I get a failure when I run the test. Says no exception message???
                                    Is it against your religion or something, to tell us what the actual message was?

                                    All I can tell you from what you posted so far, is that "you did something wrong".
                                    1 2 Previous Next