We help IT Professionals succeed at work.
Get Started

Why is my JUnit class not compiling? And what can I do to make it work properly?

Eindoofus
Eindoofus asked
on
584 Views
Last Modified: 2013-12-13
Here is the class I am testing:

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

Open in new window


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() {
    }
}

Open in new window


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:

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)

Open in new window


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



Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE