1

This is my problem: I've been provided an application and I have to write test casesin order to test it using JUnit. For example: Instantiate an object with a property String name and this field cannot be longer tan 10 characters. How do I catch it into the test method? Here is my code: The class to be tested is:

package hdss.io;

import hdss.exceptions.HydricDSSException;

public class AquiferPublicData implements WaterResourceTypePublicData {
    private String myName;
    private float currentHeight;

    public AquiferPublicData (String name, float current)   throws HydricDSSException{

        try {
            if(name.length()>10) //name must be shorter than 11 chars
                throw new HydricDSSException("Name longer than 10");
            else{
                myName = name;      
                currentHeight = current;
            }
        } catch (HydricDSSException e) {            
        }
    }

    public String getMyName() {
        return myName;
    }
}

My test method is:

package hdss.tests;

import static org.junit.Assert.*;

import org.junit.Test;

import hdss.exceptions.HydricDSSException;
import hdss.io.AquiferPublicData;

public class AquiferPublicDataTest {

    @Test
    public void testAquiferPublicData() {
        String notValidName = "this-name-is-too-long";
        try {
            AquiferPublicData apd = new AquiferPublicData(notValidName, 10);
            fail("Was supposed to throw Exception if name is longer than 10 chars");
        } catch (HydricDSSException e) {
            assertEquals("Name longer than 10", e.getMessage());
        }
    }

}

And the Exception is:

package hdss.exceptions;

public class HydricDSSException extends Exception{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    String message;

    //Esfuerzo Actual: 1.5 minutos

    public HydricDSSException (String message){

        this.message = message;
    }

    //Esfuerzo Actual: 1.5 minutos

    public String getMessage(){

        return this.message;
    }

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
bog
  • 1,323
  • 5
  • 22
  • 34
  • before you post a question, make sure you have done a basic google search about it – Mritunjay Mar 14 '17 at 12:41
  • Igoogled and tryed to implement proposed solutions but didn't sucessed. Ijust don't know what to do – bog Mar 14 '17 at 12:46
  • Hint: that empty catch block `catch (HydricDSSException e) { }` in your code under test ... what exactly is that supposed to do? I mean: besides silently **dropping** the errors you claim in your throws list to to be thrown? In other words: your first bug is already there. And beyond that: empty catch blocks are very rarely a good idea! – GhostCat Mar 14 '17 at 15:12

1 Answers1

0

Your problem is the original constructor. You have the throws clause in the signature. You should not have a try/catch block in the constructor. Remove it.

public AquiferPublicData (String name, float current)   throws HydricDSSException{
    if(name.length()>10) throw new HydricDSSException("Name longer than 10");
    myName = name;      
    currentHeight = current;
}

You should write a JUnit test that proves the exception is thrown if the name is longer than 10.

public class AquiferPublicDataTest {

    @Test
    public void testConstructor_Success() {
        // setup 
        String name = "Jones";
        // exercise
        AquiferPublicData data = new AquiferPublicData(name, 0.0);
        // assert
        Assert.assertEquals(name, data.getMyName());
    }

    @Test(expects = HydricDSSException.class)
    public void testConstructor_NameTooLong() throws HydricDSSException {
        // setup 
        String name = "this one is too long";
        // exercise
        new AquiferPublicData(name, 0.0);
    }
}

What do you expect if the name is null? Is that allowed? You're going to get a null pointer exception now.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I am asked to add `throws HydricDSSException` after `public void testConstructor_NameTooLong()`. Is it ok ? – bog Mar 14 '17 at 13:04
  • Yes, that's correct. – duffymo Mar 14 '17 at 13:31
  • Question: exceptions like `NullPointerException` are thrown autmaticaly, so I just have to add into my test code: `if (e instanceof NullPointerException) {.....}`, after `if (e instanceof HydricDSSException)`, right? It seams to work... – bog Mar 14 '17 at 13:35
  • Wrong. Please stop putting stuff like this in comments. This is about programming by contract and pre-conditions. I would say that you should make clear what your contract is. If your object will not accept null or empty names, then it should check in the constructor and tell them. Read about IllegalArgumentException. – duffymo Mar 14 '17 at 13:37