Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: JUnit problems

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JUnit problems

    Hi

    This is my first time using JUnit and somehow this doesn't work. I have a class SoccerTeam and a
    testclass SoccerTeamTest. I always get an initialization error so what do I have to change?

    SoccerTeam:
    package Soccer;

    public class SoccerTeam {

    String name;
    int og, fg, matches;

    public SoccerTeam(String name) {
    this.name = name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getName() {
    return name;
    }

    public void setMatches(int number) {
    this.matches = number;
    }

    public int getMatches() {
    return matches;
    }

    public int getOg() {
    return og;
    }

    public void setOg(int og) {
    this.og = og;
    }

    public int getFg() {
    return fg;
    }

    public void setFg(int fg) {
    this.fg = fg;
    }

    public void addMatchResult(int og, int fg) {
    this.og += og;
    this.fg += fg;
    matches++;
    }

    @Override
    public String toString() {
    StringBuffer sb = new StringBuffer(256);
    sb.append(name).append(" ").append(og).append(":").append(fg)
    .append(" in ").append(matches).append(" matches.");
    String a = sb.toString();
    return a;
    }
    }

    TestClass

    package Tests;

    import static org.junit.Assert.*;

    import org.junit.Before;
    import org.junit.Test;

    import junit.framework.JUnit4TestAdapter;
    import Soccer.SoccerTeam;


    public class SoccerTeamTest {
    public static SoccerTeam t = new SoccerTeam("Deutschland");

    public static junit.framework.Test suite(){
    return new JUnit4TestAdapter(SoccerTeam.class);
    }

    @Before
    public void clear(){
    t.setOg(0);
    t.setFg(0);
    t.setMatches(0);
    }

    @Test
    public void oneGame(){
    t.addMatchResult(4, 2);
    assertEquals(t.getOg(), 4);
    assertEquals(t.getFg(), 2);
    assertEquals(t.getMatches(), 1);
    }

    @Test
    public void threeGames(){
    t.addMatchResult(0, 0);
    t.addMatchResult(5, 1);
    t.addMatchResult(2, 3);
    assertEquals(t.getMatches(), 3);
    assertEquals(t.getOg(), 7);
    assertEquals(t.getFg(), 4);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: JUnit problems

    I always get an initialization error
    Without the text of the error messages, its hard to say what the problem is.
    Can you compile the code with the javac command and post the full text of the error messages here?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JUnit problems

    This is what I got when I run the test:

    SoccerTournament
    Soccer.SoccerTeam
    initializationError(Soccer.SoccerTeam)
    java.lang.Exception: Test class should have exactly one public zero-argument constructor
    Last edited by degude; September 18th, 2011 at 04:18 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: JUnit problems

    Test class should have exactly one public zero-argument constructor
    Do you understand what the error message is saying?
    Does your code have more than one constructor?
    Does your constructor have any arguments?

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JUnit problems

    No that is my problem.
    That is all of my code.
    The constructor of SoccerTeam is:
    public SoccerTeam(String name) {
    this.name = name;
    }
    My testclass does not have one, do I need one?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: JUnit problems

    I'm not familiar with JUnit.
    The error message says there is a problem with your constructor.
    Change the constructor to have zero-arguments, use the set method and see what happens.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JUnit problems

    I changed the constructor so that is has no arguments, instead I wrote t.setName("..."); into the @Before ckear() method but now I get:

    java.lang.Exception: No runnable methods
    Last edited by degude; September 18th, 2011 at 06:21 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: JUnit problems

    Sorry, I don't know anything about how to use JUnit.

    Shouldn't it be: t.setName("...");

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JUnit problems

    Thanks for your help,
    I found my mistake
    It has to be: public static junit.framework.Test suite(){
    return new JUnit4TestAdapter(SoccerTeamTest.class);
    }

Similar Threads

  1. How to make Multiple JUnit reports
    By GodspeedPR in forum Java Theory & Questions
    Replies: 0
    Last Post: July 22nd, 2011, 09:04 AM
  2. Junit with input as xml
    By frendmansi in forum Java Theory & Questions
    Replies: 4
    Last Post: April 15th, 2011, 04:47 AM
  3. Junit and TestNG which one is best?
    By jammychen in forum Java Theory & Questions
    Replies: 0
    Last Post: November 7th, 2010, 04:32 AM
  4. JUnit test for ER
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2010, 09:26 PM
  5. [SOLVED] JUnit initialization error
    By raphytaffy in forum Java IDEs
    Replies: 2
    Last Post: September 20th, 2010, 05:33 PM