Results 1 to 4 of 4
  1. #1
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default How to test set methods in JUnit

    How do I test a set method in JUnit? Because whenever I try to make the test case I get an error that says "Type mismatch: cannot convert from void to int"?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: How to test set methods in JUnit

    As I've said in your other post, when asking questions about an error message, you have to provide an SSCCE that demonstrates exactly what you're doing.
    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
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default Re: How to test set methods in JUnit

    Here is the set method and it's class:

    Java Code:
    public class Date {
    
    private static final int FIRST_MONTH = 1;
    
    private int day, month, year;
    
    public void setDay(int day) {
    		this.day = day;
    
    		int daysInMonth = numberOfDaysInMonth(month, year);
    		if (this.day < FIRST_DAY) {
    			this.day = FIRST_DAY;
    		} else if (this.day > daysInMonth) {
    			this.day = daysInMonth;
    		}
    
    public static int numberOfDaysInMonth(int month, int year) {
            int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
              
            if (month == 2 && isLeapYear(year)) {
                return 29;
            } else {
                return daysInMonth[month-1];
            }
        }
     
    public static boolean isLeapYear(int year) {
            return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
        }   
    }
    This is the test case

    Java Code:
    public class TestDate {
    	
    	int fifteenth = 15;
    
    @Test
    	public void testSetDay15() {
    		int day = fifteenth;
    		int result = today.setDay(day);
    	}

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

    Default Re: How to test set methods in JUnit

    You test them by checking that whatever get equivalent(s) return the correct value.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JUnit test and ArrayIndexOutOfBoundsException
    By Hazza in forum New To Java
    Replies: 17
    Last Post: 02-22-2013, 02:21 PM
  2. JUnit Test in Eclipse
    By Wolfsy in forum Eclipse
    Replies: 1
    Last Post: 03-31-2012, 01:12 AM
  3. Replies: 1
    Last Post: 08-03-2009, 05:31 PM
  4. JUnit Test Help!
    By pharo in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 05:15 PM
  5. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 01:24 AM

Tags for this Thread

Posting Permissions

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