Skip to content

Commit

Permalink
Add dedicated exception, which is thrown, while field injection fails…
Browse files Browse the repository at this point in the history
… in tests
  • Loading branch information
rchomczyk committed Oct 15, 2023
1 parent 4d5b9f9 commit 8fe6de4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2023 cory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package moe.rafal.cory.integration;

public class FieldInjectionException extends IllegalStateException {

public FieldInjectionException(String message, Throwable throwable) {
super(message, throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.lang.reflect.Field;
import java.util.function.Predicate;
import moe.rafal.cory.integration.FieldInjectionException;
import np.com.madanpokharel.embed.nats.EmbeddedNatsConfig;
import np.com.madanpokharel.embed.nats.EmbeddedNatsServer;
import org.junit.jupiter.api.extension.AfterAllCallback;
Expand Down Expand Up @@ -82,8 +83,12 @@ private void injectFields(Class<?> testClass, Object testInstance, Predicate<Fie
try {
field.setAccessible(true);
field.set(testInstance, underlyingServer);
} catch (Exception ex) {
throw new RuntimeException(ex);
} catch (Exception exception) {
throw new FieldInjectionException(
format("Could not inject %s into %s test class.",
underlyingServer.getClass().getName(),
testClass.getSimpleName()),
exception);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.function.Predicate;
import moe.rafal.cory.integration.FieldInjectionException;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand Down Expand Up @@ -82,8 +83,12 @@ private void injectFields(Class<?> testClass, Object testInstance, Predicate<Fie
try {
field.setAccessible(true);
field.set(testInstance, underlyingServer);
} catch (Exception ex) {
throw new RuntimeException(ex);
} catch (Exception exception) {
throw new FieldInjectionException(
format("Could not inject %s into %s test class.",
underlyingServer.getClass().getName(),
testClass.getSimpleName()),
exception);
}
});
}
Expand Down

0 comments on commit 8fe6de4

Please sign in to comment.