Eindoofus
asked on
Why is my JUnit class not compiling? And what can I do to make it work properly?
Here is the class I am testing:
And here is the test class for the Crate class that I wrote:
When I attempt to compile my test the compiler complains that "variable ct may not have been initialized" on the line with "assertEquals("errortxtONE" , 0.0, ct.getWeight(), 0.00001);" yet, the line with "assertEquals("errortxtTWO" , 0.0, ct.getWeight(), 0.00001);" doesn't come back with an error message.
The compiler outputs the following:
What am I doing wrong? Why can't I use "ct.getWeight()"? And what do I need to do to my code to make this work properly? I need to somehow check to make sure the state of the Crate class "weight" data member didn't change on lines "assertEquals("errortxtONE" , 0.0, ct.getWeight(), 0.00001);"and "assertEquals("errortxtTWO" , 0.0, ct.getWeight(), 0.00001);"
public class Crate implements Cargo
{
private String identifier;
private double weight;
public Crate(String idIn, double wgtIn) throws InvalidArgumentException
{
setIdentifier(idIn);
setWeight(wgtIn);
}
@Override
public String getIdentifier()
{
return identifier;
}
private void setIdentifier(String idIn) throws InvalidArgumentException
{
if (idIn == null || idIn.isEmpty() || idIn.length() > 20)
throw new InvalidArgumentException("Invalid identifier passed to Crate.setIdentifier: " + idIn);
identifier = idIn;
}
@Override
public double getWeight()
{
return weight;
}
private void setWeight(double weightIn) throws InvalidArgumentException
{
if (weightIn <= 0.0)
throw new InvalidArgumentException("Invalid weight passed to Crate.setWeight: " + weightIn);
weight = weightIn;
}
}
And here is the test class for the Crate class that I wrote:
public class CrateTest {
public CrateTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of getIdentifier method, of class Crate.
*/
@Test
public void testGetIdentifier() {
Crate ct;
// Positive test
try {
ct = new Crate("id", 3.0);
assertEquals("errortxt", "id", ct.getIdentifier());
assertEquals("errortxt", 3.0, ct.getWeight(), 0.00001);
}
catch (Exception e) {
fail("Legitimate values were rejected when sent to constructor" + e.getMessage());
}
// Negative test for null String
try {
ct = new Crate(null, 3.0);
fail("errortxt");
}
catch (InvalidArgumentException e) {
assertEquals("errortxt", "Invalid identifier passed to Crate.setIdentifier: " + null, e.getMessage());
assertEquals("errortxtONE", 0.0, ct.getWeight(), 0.00001);
}
// Negative test for empty String
try {
ct = new Crate("", 3.0);
fail("errortxt");
}
catch (InvalidArgumentException e) {
assertEquals("errortxt", "Invalid identifier passed to Crate.setIdentifier: " + "", e.getMessage());
assertEquals("errortxtTWO", 0.0, ct.getWeight(), 0.00001);
}
}
/**
* Test of getWeight method, of class Crate.
*/
@Test
public void testGetWeight() {
}
}
When I attempt to compile my test the compiler complains that "variable ct may not have been initialized" on the line with "assertEquals("errortxtONE"
The compiler outputs the following:
Testsuite: shipexample.implementors.CrateTest
Tests run: 2, Failures: 0, Errors: 1, Time elapsed: 0.053 sec
Testcase: testGetIdentifier(shipexample.implementors.CrateTest): Caused an ERROR
Uncompilable source code - variable ct might not have been initialized
java.lang.RuntimeException: Uncompilable source code - variable ct might not have been initialized
at shipexample.implementors.CrateTest.testGetIdentifier(CrateTest.java:64)
Test shipexample.implementors.CrateTest FAILED
test:
Deleting: Omitted\TEST-shipexample.implementors.CrateTest.xml
BUILD SUCCESSFUL (total time: 3 seconds)
What am I doing wrong? Why can't I use "ct.getWeight()"? And what do I need to do to my code to make this work properly? I need to somehow check to make sure the state of the Crate class "weight" data member didn't change on lines "assertEquals("errortxtONE"
ASKER
It works but I was told that I need to test the state of the object after it's creation. Doesn't that defeat the purpose since it's not testing the state of the object in the try block?
Not really, because null is not an object and you would get null pointer exceptions in your assertions if the object was not actually created.
When ct = new ...
Statement is in the same brace as Assert(...) Then compiler is sure that ct will be initialized;
If they are in separate braces then compiler cannot be sure that initialization will happen befire use;
There fore compiler complains;
If you assign =null as suggested at the very top, as was suggested then comnpiler will be happy
Statement is in the same brace as Assert(...) Then compiler is sure that ct will be initialized;
If they are in separate braces then compiler cannot be sure that initialization will happen befire use;
There fore compiler complains;
If you assign =null as suggested at the very top, as was suggested then comnpiler will be happy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
instead of
Open in new window