Results 1 to 3 of 3
  1. #1
    someone2088 is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Adding tests to a Java class

    Hi,

    I have been given a Java program, and been asked to write a set of tests for one of the classes, but it's been a long time since I've done any programming, and I'm a bit unsure of where to start...

    This is the class that I have been asked to write tests for:

    Java Code:
    package uk.ac.aber.dcs.cs31310.qs;
    
    public class Question {
    
    	private String id; 
    	
    	private String text; 
    	
    	public Question(String id, String text) {
           this.id = id; 
           this.text = text; 
    	}
    	
    	public String getId() { 
    		return id; 
    	}
    	
    	public String getText() { 
    		return text; 
    	}
    	
    	public void setText(String text) { 
    		this.text = text; 
    	}
    
    	@Override
    	public boolean equals(Object other) { 
    		
    		if(other instanceof Question) { 
    	       Question otherQuestion = (Question)other; 
    	       return this.id.equals(otherQuestion.id); 
    		}
    		
    		return false; 
    	}
    	
    }
    There a couple of things I'm not sure about, i.e. should I add the tests to this class, or should I create a separate class to do the testing?
    Also, how would I then go about testing each of the methods in the class?

    The task description is simply:

    Write a set of tests for the Question class. You should do this for all of the methods that are currently in the Question class1. If you identify any bugs with the code, you should fix the bugs and state what you have fixed in your report.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default Re: Adding tests to a Java class

    You'd test it by answering the question: does each method do what it's supposed to do? So your first step would be to write out, in your own words, what each method should do. What is true before the method is called? What is true afterwards? Then write code that tests those assumptions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Adding tests to a Java class

    And the testing code would be in a different class.
    It would create an instance of Question and then have tests that will call a method with some parameter or other and then check whether the question object has done the correct thing (ie the data has been changed correctly).

Similar Threads

  1. help adding to array from another class
    By Grid_iso in forum New To Java
    Replies: 0
    Last Post: 05-09-2011, 11:00 AM
  2. Adding 2 Implements to the class
    By jboy in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 06:19 AM
  3. Replies: 8
    Last Post: 08-11-2009, 04:57 PM
  4. Adding a new class to a jar file
    By Raul Menendez in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 05:52 PM
  5. Replies: 0
    Last Post: 04-05-2008, 01:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •