I have multiple packages under a folder for which tests were written. These tests pass when run as a Class or from the residing package. But when I run it from the folder as a whole, the tests are failing with mockito exception. For example, EmailHandlerTest class passes individually and also when run as a package from "email" package. But, when I run the tests as a whole from bff folder, many of the classes are throwing error with respect to mockito.
Error:
java.lang.NullPointerException: Cannot invoke "[Ljava.lang.Class;.clone()" because "
<local2>.parameterTypes" is null
at java.base/java.lang.reflect.Method.getParameterTypes(Method.java:314)
at org.mockito.internal.creation.DelegatingMethod.<init>(DelegatingMethod.java:20)
at org.mockito.internal.invocation.DefaultInvocationFactory.createMockitoMethod(DefaultInvocationFactory.java:81)
at org.mockito.internal.invocation.DefaultInvocationFactory.createInvocation(DefaultInvocationFactory.java:60)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:83)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:56)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptAbstract(MockMethodInterceptor.java:161)
at com.<package>.bff.repository.EmailTemplateRepository$MockitoMock$520687419.findByEmailTemplateId(Unknown Source)
at <package>.bff.email.EmailHandlerTest.sendProjectCompletionMail_Should_Send_Email_Successfully(EmailHandlerTest.java:926)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
Code of a single method in EmailHandlerTest class. There are many other similar methods in this class which are failing with the same error:
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class EmailHandlerTest {
@Mock private EmailTemplateRepository emailTemplateRepository;
<<Few Other Mocks here>>
@InjectMocks EmailHandler emailHandler;
@TempDir
Path reportDir;
private static TestUtils testUtils = new TestUtils();
List<EmailTemplate> emailTemplates;
void setup() throws IOException {
ReflectionTestUtils.setField(emailHandler, "apiEndpoint", "testEndpoint");
ReflectionTestUtils.setField(emailHandler, "emailIsProdEnv", false);
ReflectionTestUtils.setField(emailHandler, "fromAddress", "[email protected]");
ReflectionTestUtils.setField(emailHandler, "sharedMailBoxEmail", "[email protected]");
ClassLoader classLoader = getClass().getClassLoader();
ObjectMapper mapper = new ObjectMapper();
InputStream inputStream= classLoader.getResourceAsStream("emailTemplates.json");
emailTemplates = mapper.readValue(inputStream, new TypeReference<List<EmailTemplate>>(){});
}
@Test
void sendProjectCompletionMail_Should_Send_Email_Successfully() throws IOException, ParseException {
// Given
setup();
ReflectionTestUtils.setField(emailHandler, "sendProjectCompletionMailToggle", true);
ClassLoader classLoader = getClass().getClassLoader();
File report = new File(classLoader.getResource("Report.xlsx").getFile());
Project project = Instancio.of(testUtils.getProjectModel()).create();
project.setProductType(Constants.PRODUCT_TYPE_1);
ProjectDomain projectDomain = Instancio.of(ProjectDomain.class).create();
User assignee = Instancio.of(User.class).create();
when(userRepository.findByUsername(any())).thenReturn(assignee);
EmailTemplate emailTemplate = emailTemplates.stream().filter(e -> StringUtils.equalsIgnoreCase(e.getEmailTemplateId(), "TEMPLATE_ID_1")).findFirst().get();
when(emailTemplateRepository.findByEmailTemplateId("TEMPLATE_ID_1")).thenReturn(emailTemplate);
doNothing().when(sesEmailService).sendMail(any(), any(), any(), any(), any(), any(), any());
Email email = Instancio.of(Email.class).create();
when(emailRepository.save(any())).thenReturn(email);
// When
emailHandler.sendProjectCompletionMail(report, project, projectDomain);
// Then
verify(sesEmailService).sendMail(any(), any(), any(), any(), any(), any(), any());
}