简体   繁体   中英

JAVA:Catching exception when unit testing and parameter parsing

   @Test
    public void testToString(){
        System.out.println("toString");
        Address add = new Address("Blackthorn","Kings Lynn","PE30");
        BusinessOrganisationDetails instance  = new BusinessOrganisationDetails("PEA-1234",
                "Smith",add,10,"EA",12);
        String expResult = "";
        String result = instance.toString();
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }

This is a test method for my BusinessOrganisationDetails class, I was wondering when testing if it is necessary to create an instance of that class and put in the specific variables when testing. It requires the fields below.

//        String customerID, String companyName,
//            Address address,int companyDiscount,
//            String regionalCode,double totalPrice

As you can see it requires a field of type address. So is it necessary to make an instance of the address class here as well as I've done in the above code. I am getting an error because when creating an instance of the BusinessOrganisationDetails class It says I need to do a try catch for the IllegalCustomerIDException class. I'm unsure as to the best approach to this given my scenario

Can you explain better the scenario? I didn't get what are you trying to test and what are exactly your concerns about. Anyway based on what I understood I would say that a try catch block in a test is not a problem, you can use it also to test that the exception is correctly thrown, for example if a wrong customer ID is passed as parameter to the constructor. Or if you just assume that in your test that exception shouldn't be thrown you can insert throws IllegalCustomerIDException in the test method signature.

There's nothing wrong with try/catch in tests. There are 2 ways to test if your code finishes without exceptions:

@Test
public void testToString() throws IllegalCustomerIDException {
    System.out.println("toString");
    Address add = new Address("Blackthorn","Kings Lynn","PE30");
    BusinessOrganisationDetails instance  = new BusinessOrganisationDetails("PEA-1234",
            "Smith",add,10,"EA",12);
    String expResult = "";
    String result = instance.toString();
    assertEquals(expResult, result);
}

@Test
public void testToString()  {
    System.out.println("toString");
    try {
        Address add = new Address("Blackthorn","Kings Lynn","PE30");
        BusinessOrganisationDetails instance  = new  BusinessOrganisationDetails("PEA-1234",
            "Smith",add,10,"EA",12);
        String expResult = "";
        String result = instance.toString();
        assertEquals(expResult, result);
    } catch(BusinessOrganisationDetails e) {
            fail(e.getMessage);
    }
}

If you wan't to test if the exception is thrown, you can use a Rule with an ExcpectedException

@Rule
public ExpectedException exception = ExpectedException.none();

@Test
public void testToString() throws IllegalCustomerIDException {
    System.out.println("toString");
   exception.expect(IllegalCustomerIDException.class);
    exception.expectMessage("unkwon customer id 4711");
    Address add = new Address("Blackthorn","Kings Lynn","PE30");
    BusinessOrganisationDetails instance  = new BusinessOrganisationDetails("PEA-1234",
            "Smith",add,10,"EA",12);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM