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

    Default Problem compiling and executing to get the JUnit “Green Bar”

    I have to use Test Driven development with JUnit and am having trouble getting this code to run and compile. Below is my test and production classes.

    Java Code:
    package edu.hello.test;
    import junit.framework.*;
    
    public class MidtermCourseTest extends TestCase {
    	void testCourse () {
    		Course course = new course ("AAA_AAB", "101");
    		
    		assertEquals(String "w" , "String "x");
    		assertEquals("String "y" , "String "z");
    	}
    }
    Java Code:
    package edu.hello.production;
    
    class Course {
    	private String department;
    	Course (String department, String number) {
    		this.department = department;
    		this.number = number;
    	}
    	
    	String getDepartment() {
    		return department;

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Problem compiling and executing to get the JUnit “Green Bar”

    Java Code:
            assertEquals(String "w" , "String "x");    // Extra double quote ( " )  ... and String should not be here... java expects an Object "w" is a String Object and will be treated accordingly.
            assertEquals("String "y" , "String "z");   // Two extra double quotes
    Please include error messages you've received if you want timely help. assertEquals has several signatures, none of which are (String, String). This will use (Object, Object) and probably(I don't have JUnit installed here to verify) not give you the correct results try assertTrue("y".equals("y")) for example.

  3. #3
    trpkob is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Problem compiling and executing to get the JUnit “Green Bar”

    -error.jpg

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Problem compiling and executing to get the JUnit “Green Bar”

    Too many '"'. If you see the code blocks above, anything blue is the string, so you've got "w", "String ", "String ", " , ", and "z"
    Last edited by SJF; 10-28-2012 at 03:10 AM.

  5. #5
    kuriozal is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problem compiling and executing to get the JUnit “Green Bar”

    Firstly, what do you want to check with this test? I see you have class named Course, and constructor with departmend and number. So maybe something like this:

    Java Code:
    assertEquals(course.getDepartment(), "AAA_AAB");
    assertEquals(course.getNumber(), "101"); // " " if number is stored as a String

  6. #6
    trpkob is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Problem compiling and executing to get the JUnit “Green Bar”

    Thank you kuriozal! That is exactly what I am looking to do. I am still getting a compile error however.

    -error_2.jpg

  7. #7
    kuriozal is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problem compiling and executing to get the JUnit “Green Bar”

    Eh why are you writing tests if you don't know very basics of Java?

    Java is case sensitive so you can't invoke new course() if you want to invoke new Course().

    Additionally, the visibility of Course's constructor and methods is set to default so your test doesn't see them. You have to change it to public.

    So:
    Java Code:
    Course course = new Course ("AAA_AAB", "101");
    Last edited by kuriozal; 10-28-2012 at 05:51 PM.

  8. #8
    trpkob is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Problem compiling and executing to get the JUnit “Green Bar”

    Thank you for all your help kuriozal! I got it running!

Similar Threads

  1. problem with JUnit test
    By exltus in forum Advanced Java
    Replies: 12
    Last Post: 12-19-2011, 08:49 PM
  2. JUnit problem
    By SeanC in forum New To Java
    Replies: 0
    Last Post: 01-13-2011, 02:11 AM
  3. JUnit 4 / JUNIT_HOME Problem
    By albertkao in forum Eclipse
    Replies: 0
    Last Post: 11-22-2010, 10:43 PM
  4. junit test problem
    By moamen in forum Eclipse
    Replies: 2
    Last Post: 03-14-2010, 10:41 PM
  5. Replies: 2
    Last Post: 12-31-2009, 01:45 PM

Posting Permissions

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