java - Why does my custom member variable of a class not update in other classes? -


so i'm working on project , need design game has player. inside game object, i've initialized player , set getter player:

player declaration getplayer method

the player knows of own settlements. whenever found new settlement, settlement should added specific player's settlements arraylist. so, inside player class, i've initialized arraylist of settlements, , method adds settlement arraylist.

player's settlements array addtosettlement

when write test multiple found settlement calls, settlements add not persisting outside of function call inside of game object:

found settlement

i know scoping or static declaration error of kind, don't understand why implementation wouldn't work. appreciated!

edit: here perhaps better example of isn't passing.

map.foundnewsettlement(new coordinate(1,1)); map.foundnewsettlement(new coordinate(-2,3)); system.out.print(map.getplayer().getsettlements().size()); 

the size printed 0 when should 2

instance fields reinitialized @ each test method launched.
game instance not persistent between test methods.

if second test testing "adding second settlement" works, should first add settlement (that create initial context of test) before calling method test : addsettlement().

besides, test method should not rely on previous side effects performed test method.
keep test methods independent keep isolated , maintainable.

so, should create initializing context each tested method.
@ beginning, can perform task directly in tested method.
later if start repeat concerning initializing context, may introduce private method it.

here simple example show how create context junit :

private game game;  // executed before each test method @before public void setup(){     game game = new game();     game.addplayer(new player(...)); }  @test public void addasecondsettlement(){     // context     game.getplayer().addsettlement(new settlement(1));     // action     game.getplayer().addsettlement(new settlement(2));     //assertion     assert.assertequals(2, game.findlastsetllement().getid()); } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -