Reputation:
I am writing a test case using JUnit
and I want to fail my test by catching an exception
. The method under test throws DataException
and catches Throwable
. In my test case I want to catch DataException
but my test passes if I don't fail it.
Method under test:
public List<DataSet> retrieveData(String action, Map<String, Object> params, ServiceContext ctx) throws DataException {
List<DataSet> o = null;
try {
if( FIND_BY_AGENCY_CODE.equals( action ) ) {
o = findByAgencyCode( params );
} else if( GET_CHILD_AGENCY_CODE.equals( action ) ) {
o = getChildAgencies( params );
} else if( GET_VALID_VALUES.equals( action ) ) {
o = loadValidValues( params );
} else if( GET_VALID_VALUE_COMBO.equals( action ) ) {
o = getValidValueCombo( params );
} else if( GET_CLAIM_UNITS.equals( action ) ) {
o = getClaimUnits( params );
} else if( GET_SP_LIMIT.equals( action ) ) {
o = getSPLimit( params );
} else if( GET_SP_PIP_APIP_TYPE.equals( action ) ) {
o = getSPPipApipType( params );
} else if( GET_SP_DEDUCTIBLE.equals( action ) ) {
o = getSPDeductible( params );
} else if( GET_SP_COVERAGE.equals( action ) ) {
o = getSPCoverage( params );
} else if( GET_SP_FORM_DISPLAY.equals( action ) ) {
o = getSPFormDisplay( params );
} else if( GET_CLAIM_UNIT.equals( action ) ) {
o = getClaimUnit( params );
} else if( GET_SP_STATE_COMPANY.equals( action ) ) {
o = getSPStateCompany( params );
} else if( GET_SP_COV_LIMIT_LOOKUP.equals( action ) ) {
o = getSPCovLimitLookup( params );
} else if( GET_SP_UND_LOOKUP.equals( action ) ) {
o = getSPUnderwriterLookup( params );
} else if( GET_SP_UND_BRANCH.equals( action ) ) {
o = getSPUndBranch( params );
}
} catch( Throwable t ) {
}
return o;
}
JUnit Test:
@Test
public void testRetrieveData() {
try {
AppContext.setApplicationContext(applicationContext);
IRetrievable dao = (IRetrievable)AppContext.getBean( "nextGenDao" );
ServiceContext ctx = new ServiceContext();
Map<String, Object> params = new HashMap<String, Object>();
params.put( ServiceConstants.AGENCY_CODE, "1234" );
@SuppressWarnings("unused")
List<DataSet> ds = dao.retrieveData( NextGenDao.FIND_BY_AGENCY_CODE, params , ctx );
fail();
} catch (DataException t) {
assertNotNull(t);
assertEquals("Unit Test", t.getMessage());
}
}
I am not exactly sure if this is the right approach to accomplish such a thing, but this is what I got so far.
Thanks
Upvotes: 0
Views: 61
Reputation: 96444
In your method under test, there is no possibility of it throwing anything, ever. Having all the code surrounded by a try block with catch (Throwable t) {}
means that anything extending Throwable (meaning all possible Exceptions and Errors) will get caught and eaten. So if you expect the test to catch a DataException, that won't happen. What will happen is that if an exception is thrown within the method, the list the method returns will be null.
Upvotes: 1
Reputation: 8529
If you want the test to fail after catching an exception put a fail() statement in the catch block in your test.
If you want the test to fail if it doesn't throw an exception, use the @Test(expected = DataException.class) annotation for that test.
Upvotes: 0