How to catch exceptions when using Lombok's #Cleanup? - java

I am using Lombok's #Cleanup and even when I find it very useful, I cannot figure out how can I catch an exception being thrown by the main block of code that makes use of the resource to be cleant.
My code is the following:
ScriptRunner runner = null;
try {
runner = scriptRunner();
for (Resource sqlFile : sqlScripts) {
runScript(sqlFile.getFile(), runner);
}
} catch (Exception ex) {
log.error("Error found when attempting to run script", ex);
} finally {
if (runner != null) {
runner.closeConnection();
}
}
With Lombok, I could simplify the code above this way:
#Cleanup("closeConnection") ScriptRunner runner = scriptRunner();
for (Resource sqlFile : sqlScripts) {
runScript(sqlFile.getFile(), runner);
}
But I would be missing the possibility of catching any exception that might arise when the for loop executes. Is there any way to do this in an elegant way with Lombok? (i.e. other than programmatically using "try /catch?)
Thanks very much in advance.

No. If you want to process the exception you need to catch it, and for that you'll need a try-catch block.
Full Disclosure: I am one of the Project Lombok developers.

Related

JUnit5 Test coverage for catch block of a Spring Boot Controller

I am trying to write test for my controller with the code below. I want to cover the test for the code in the catch block statement but I'm not able to write one. I want to return a Server Response with failure code and message in the catch block.
#PostMapping(COUNTERS)
public ResponseEntity<?> getCounters(#Valid #RequestBody ApartmentCounterListRequest requestData) {
try {
log.debug("Entering API for counter list");
ApartmentCounterListResponse apartmentCounterListResponse = counterService.getAllCounters();
return ResponseEntity.ok(apartmentCounterListResponse);
} catch (Exception exception) {
log.error("Exception in counter list :: ", exception);
ServerResponse serverResponse = ResponseBuilder.buildVendorFailureMessage(new ServerResponse(),
RequestResponseCode.EXCEPTION);
return ResponseEntity.ok(JsonResponseBuilder.enquiryResponse(serverResponse));
}
}
My test code is as follows:
#Test
#DisplayName("Should return ServerResponse with failure data.")
void Should_Return_Server_Response_On_Exception() throws Exception {
/*given*/
ApartmentCounterListRequest apartmentCounterListRequest = ApartmentTestUtil.getApartmentCounterListRequest.
apply("test", "test");
Mockito.when(counterServic.getAllCounters()).thenThrow(new Exception());
// ServerResponse serverResponse = ApartmentTestUtil.getServerExceptionServerResponse.get();
/*then*/
mockMvc.perform(
post(COUNTER_URL)
.contentType(APPLICATION_JSON)
.content(objectMapper.writeValueAsString(apartmentCounterListRequest)))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.resultCode", Matchers.is("-6")));
verify(counterService, times(1)).getAllCounters();
}
When I run this test I am getting the following error:
org.mockito.exceptions.base.MockitoException:
Checked exception is invalid for this method!
Invalid: java.lang.Exception
I have gone through some of the following posts but haven't found a suitable answer yet.
Unit testing code in catch block of a Spring Controller
Java - How to Test Catch Block?
Unit testing code in catch block of a Spring Controller
JUnit for both try and catch block coverage
Can anyone help me write test that covers the catch block or tell me the way to do it?
I have this try catch in my controller to handle any unexpected exceptions. And for different api's I have to send a response with different response code and messages which doesnot allow me to use Exception handler.
The method you are mocking does not declare a checked exception, therefore Mockito is not able to throw one from there. Try to have the mock throw an unchecked exception (i.e. RuntimeException).
You can try to use willAnswer
Mockito.when(counterServic.getAllCounters()).thenAnswer(x -> {throw new Exception() });
Maybe this is a bit misused as Answer is used for more complex for when return

Android unreported exception IOException error

I am new to android apps development. Recently,Im writing an application which able to show public ip based on Ipify. So far, i already:
Download the required jar file and put inside libs folder
I also compile file within gradle
Then i import required class it to my class
How to use Ipify, according to its website:
import org.ipify.Ipify;
public class HelloIP {
public static void main(String[] args) throws IOException {
System.out.println(Ipify.getPublicIp());
}
}
I write the following method to be invoked from another class:
public static String getPublicIp() throws IOException{
String ip = Ipify.getPublicIp();
return ip;
}
Another Class
//Get wifi
getWifiName wifiInfo = new getWifiName();
String myIP = wifiInfo.getPublicIp();
However, i keep getting:
Error:(52, 43) error: unreported exception IOException; must be caught
or declared to be thrown
I tried to modify the code and use the following try and catch, but still got the same error.
public static String getPublicIp() throws IOException{
String myip = Ipify.getPublicIp();
try{
return myip;
}
catch (IOException e){
System.out.println("General I/O exception: " + e.getMessage());
e.printStackTrace();
}
}
Im not too good in catch and throw exception, and already spent the whole day for this.I dont have idea anymore to fix this error..T.T
public static String getPublicIp() {
try{
return Ipify.getPublicIp();
}catch (IOException e){
System.out.println("General I/O exception: " + e.getMessage());
e.printStackTrace();
}
return null;
}
In case it didn't help, clean project in your IDE. You may have some data cached and it might be a reason.
Your problem is in another class! As you have declared the method getPublicIp() to throw IOException that class is afraid of receiving the Exception and therefor requests catching it.
In Java you have two types of Exceptions. Checked and unchecked. Checked Exceptions must be caught.
In Java Exceptions are used for marking unexpected situations. For example parsing non-numeric String to a number (NumberFormatException) or calling a method on a null reference (NullPointerException). You can catch them in many ways.
Unchecked Exceptions are those which extend RunTimeException. They are used for marking unexpected states usually caused by user's input. They shouldn't cause harm and should be worked out with business logic. You don't have to catch them, but sometimes you should.
On the other hand there are Checked Exceptions which mark dangerous situations. For example the application being unable to open a file. As those situations are found dangerous, you must catch them.
try{
//some code
} catch (NumberFormatException e1) {
e.printStackTrace() //very important - handles the Exception but prints the information!
} catch (NullPointerException e2) {
e.printStackTrace();
}
or using the fact, that they all extend Exception:
try {
//somecode
} catch (Exception e) {
e.printStackTrace;
};
or since Java 7:
try {
//somecode
} catch (NullPointerException | NumberFormatException e) {
e.printStackTrace;
};

Testing if custom exception was thrown when a run time exception is thrown using mockito

I have this code where I'm catching some exception and throwing a custom exception instead.
#Override
public void config() throws CustomException{
File jsonFile = new File("config.json");
try {
ConfigMapper config = mapper.readValue(jsonFile, ConfigMapper.class);
try {
this.instanceId = Integer.parseInt(config.getConfig().getClientId());
this.configParams = config.getConfig().getConfigParams();
} catch (NumberFormatException ex) {
throw new CustomException("Please provide a valid integer for instance ID", ex);
//LOGGER.log(Level.SEVERE, "error initializing instanceId. Should be an integer " + e);
}
} catch (IOException ex) {
throw new CustomException("Error trying to read/write", ex);
// LOGGER.log(Level.SEVERE, "IOException while processing the received init config params", e);
}
}
I need to write a unit test for this and below is how I wrote it.
#Test
public void should_throw_exception_when_invalid_integer_is_given_for_instanceID(){
boolean isExceptionThrown = false;
try{
Mockito.doThrow(new NumberFormatException()).when(objectMock).config();
barcodeScannerServiceMock.config();
} catch (CustomException ex) {
isExceptionThrown = true;
}
assertTrue(isExceptionThrown);
}
But its throwing a number format exception and not the CustomException as I want it to be. But this makes sense as I'm using the mock object to throw the exception as a result of which my code logic is not executed. But if that's the case, how do I test this scenario? Please advice.
1.) Remove the line Mockito.doThrow(new NumberFormatException()).when(objectMock).config();
2.) Change the Client-ID in your JSON-File to something that cannot be converted to an Integer.
this.instanceId = Integer.parseInt(config.getConfig().getClientId()); will fail due to that and thus throw an exception.
One advice regarding names: The name of your test method should be what's written in the Java-Doc. Just name it "testCustomException" & explain the methods function in the Java-Documentation. There are Naming-Conventions in Java (click here) which are basically general guidelines.
Practicing these is very helpful as it allows you to quickly get into your code again after not working on it for a month or so due to the increased readability.

Cannot get JAXBContext?

In my java project i have a jar that i generated with some classes. I am able to instantiate the instance of the class that is in my jar:
Alert a = new Alert();
But wen i try to do this:
JAXBContext context = JAXBContext.newInstance(Alert.class);
I get run time exception like this:
java.lang.InternalError:
Error occured while invoking reflection on target classes. Make sure all referenced classes are on classpath: interface javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter
Exception: null
Any idea what could be the issue?
Thank you
This happened to me as I had defined XmlAdapter implementations that were in a different JAR and not on the classpath. When trying to create the JAXBContext, it needs these adapters used by the JAXB type. Now that I figured this out, the message makes more sense, but is still very cryptic. XmlJavaTypeAdapter is the annotation interface and the code fails to call the value() method of the annotation as it returns Class<? extends XmlAdapter>, a class type which is not defined on the classpath.
If you look at the code that throws the exception, this is impossible:
public Class More ...getClassValue(Annotation a, String name) {
try {
return (Class)a.annotationType().getMethod(name).invoke(a);
} catch (IllegalAccessException e) {
// impossible
throw new IllegalAccessError(e.getMessage());
} catch (InvocationTargetException e) {
// impossible
throw new InternalError(e.getMessage());
} catch (NoSuchMethodException e) {
throw new NoSuchMethodError(e.getMessage());
}
}
Would be nice if the cause of the exception was not lost.

Java Not Catching Exception

Unfortunately, I don't have control over getUserByUserId(String). The way it behaves is to return a User if a user if found and to throw a OntNoObjectExistsException if no user is found. My problem is that for some reason, the catch doesn't catch OntNoObjectExistsException when it gets thrown.
The type hierarchy for this exception is: OntNoObjectExistsException -> OntException -> Exception -> Throwable.
public boolean isUserIdAvailable(String userId) {
try {
return super.getUserByUserId(userId) == null;
} catch (OntNoObjectExistsException e){
return true;
} catch (Exception ex) {
appLog.error(ex.getMessage());
}
return false;
}
I tried this code to test the waters and the problem persisted. Note, I'm catching Throwable.
public boolean isUserIdAvailable(String userId) {
try {
return super.getUserByUserId(userId) == null;
} catch (Throwable ex) {
appLog.error(ex.getMessage());
}
return false;
}
Here's the stacktrace:
com.opennetwork.exception.OntNoObjectExistsException: User not found
at com.bcbst.dsmart.api.WebUser.getUserByUserId(WebUser.java:411)
at com.bcbst.dsmart.api.WebProspectiveMemberBean.isUserIdAvailable(WebProspectiveMemberBean.java:71)
at com.bcbst.dsmart.api.EJSLocalStatelessWebProspectiveMember_ce00ef7b.isUserIdAvailable(EJSLocalStatelessWebProspectiveMember_ce00ef7b.java:120)
at com.bcbst.prospectivememberweb.actions.UsageagreementAction.execute(UsageagreementAction.java:61)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
Also note, this is java 1.4. Something else I can't control right now.
Let me propose a hypothesis. WebUser.getUserByUserId contains this code:
if (userNotFoundCondition) {
OntNoObjectExistsException e = new OntNoObjectExistsException("User not found");
logger.error("User not found", e);
throw e;
}
This hypothesis is 100% consistent with all the evidence you submitted. In order to move forward with your investigation, you must first disprove this hypothesis.
I agree with the the other answer that it is very bad practice to use exceptions for flow control but to actually answer your question have you tried to catch Throwable instead of Exception?
catch (Throwable t) {
// handle here.
}
You are catching exception in the superclass where you throw new Throwable.
You have no control over getUserByUserId(); however, it seems to be in the same package com.bcbst.dsmart.api, so this answer assumes (so as to move on) it is outside your responsibility within the same project, but you have its source files.
Could there be a mismatch between the sources of the class getUserByUserId() belongs to, and the compiled version that's being used at runtime?
If the throws statements have been modified within that class after they have been compiled, or the exceptions themselves have been changed, this could explain this apparently absurd situation of yours.
See this answer on SO for more on that hypothesis.
=> Recompile everything, and redeploy.

Categories

Resources