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);
}
}