Question: I make a Pokemon.java class and a PokemeonTest. Need to pass all the junit test. But I am fail the toStringTest1 , 2 and 3.
I make a Pokemon.java class and a PokemeonTest. Need to pass all the junit test. But I am fail the toStringTest1 , 2 and 3. Don't know why.
This is part of my Pokemon.java code.
public String toString(){ DecimalFormat decimal = new DecimalFormat("000"); String string = ""; string =string + "Species: " + this.species + " "; if(this.species.compareTo(this.name) != 0){ string =string + "Name: " +this.name + " "; } string =string + "Number: " + decimal.format(this.number) + " "; string =string + "Type: " + this.type1; if(this.type2.length() > 0){ string = string + " | " + this.type2; } string =string + " "; string =string + "HP: " + this.hitPnts + " "; string =string + "CP: " +this.comPwr +" "; return string; }
And This is part of the PokemonTest.java code.
@Test public void toStringTest1(){ Pokemon p = new Pokemon("Bulbasaur", 1, "Grass", "Poison"); int hP = p.getHitPnts(); int cP = p.getComPwr(); Assert.assertEquals("toString format is incorrect for Pokemon with no name", "Species: Bulbasaur Number: 001 Type: Grass | Poison HP: "+hP+" CP: "+cP, p.toString()); } /* testing toString method * checks that toString is properly formatted with Name and empty type2. */ @Test public void toStringTest2(){ Pokemon p = new Pokemon("Charmander", "Lisa's Charmander", 4, "Fire", ""); int hP = p.getHitPnts(); int cP = p.getComPwr(); Assert.assertEquals("toString format is incorrect for Pokemon with name, no type 2", "Species: Charmander Name: Lisa's Charmander Number: 004 Type: Fire HP: "+hP+" CP: "+cP, p.toString()); } /* testing toString method * checks that toString is properly formatted with no Name and empty type2. */ @Test public void toStringTest3(){ Pokemon p = new Pokemon("Caterpie", 10, "Bug", ""); int hP = p.getHitPnts(); int cP = p.getComPwr(); Assert.assertEquals("toString format is incorrect for Pokemon with no name, no type 2", "Species: Caterpie Number: 010 Type: Bug HP: "+hP+" CP: "+cP, p.toString()); }
My question is any mistake in my tostring part in Pokemon.java code make test1,2,3 fail?
JUnit: File: PokemonTest.iava X 15 tests 12 passed 3 failed (0.325 s) Testing Complete XPokemonTest 15 tests 12 passed 3 failed (0.325 s) hpRangeTest passed (0.107 s) getSpeciesTest passed( 1 ms) noNameTest passed (1 ms) PokemonTest.toStringTest1 orgjunit.ComparisonFailure: toString format is incorrect for Pokemon with no name expected:<.ison hp: cp: but was: cprangetest passed ms getnumbertest setnametest gettype1test poweruptest getnametest1 getnametest2 gettype2testi at pokemontest.tostringtest1 gettype2test2 tostringtesti failed x tostringtest2 tostringtest3>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
