Firstly, arrays are meaningless, they allow you to get rid of them: all they do is provide values โโfor the layout data. How you build a mock of objects was discussed ad nauseum, but obviously the code for creating fake athletes should be inside the unit test. I would use Joshua Bloch's static builder for the Athlete class, but you only have two attributes right now, so just pass them in the constructor. It will look like this:
class Athlete { private String name; private String country; private List<Dive> dives; public Athlete(String name, String country){ this.name = name; this.country = country; } public String getName(){ return this.name; } public String getCountry(){ return this.country; } public String getDives(){ return this.dives; } public void addDive(Dive dive){ this.dives.add(dive); } }
Then for the Dive class:
class Dive { private Athlete athlete; private Date date; private double score; public Dive(Athlete athlete, double score){ this.athlete = athlete; this.score = score; this.date = new Date(); } public Athlete getAthlete(){ return this.athlete; } public Athlete getAthlete(){ return this.athlete; } public Athlete getAthlete(){ return this.athlete; } }
Then create a unit test and just create classes and manipulate them, make sure they work. They are not doing anything right now, so all you can do is say that they keep the dives that you put into them. Example:
@Test public void testThatDivesRetainInformation(){ Athlete art = new Athlete("Art", "Canada"); Dive art1 = new Dive(art, 8.5); Dive art2 = new Dive(art, 8.0); Dive art3 = new Dive(art, 8.8); Dive art4 = new Dive(art, 9.2); assertThat(art.getDives().size(), is(5)); }
You can then go through the tests and add tests for such things to make sure you cannot build a dive without an athlete, etc.
You can move the design of the athletes into the test setup method so you can use it everywhere. Most IDEs have support for this with refactoring.
source share