suneel meena
suneel meena

Reputation: 11

Junit test cases failing for static method with java.lang.NoSuchFieldError: PASCAL_CASE_TO_CAMEL_CASE error

creating the sqs object to publish message to sqs queue.

public final class ClientFactory {

    private SqsClientFactory() {
        throw new ConstructorCallForbiddenException();
    }
    
  
    public static AmazonSQS buildSqs(String awsRegion) {
        String regionName = Optional.ofNullable(awsRegion).filter(StringUtils::isNotBlank).orElseGet(() -> {
            var region = Regions.getCurrentRegion();
            if (region != null) {
                return region.getName();
            }
            throw SqsClientException.regionNotAvailable();
        });

        return AmazonSQSClientBuilder.standard().withRegion(regionName).build();
    }
}

This is our main class there we are sending the message to amazon sqs and method first line we are trying to set sqs mock object from UT.

@Component
@Slf4j
public class Publisher {

    public void sendMessageToSqs(String eventPayload) {
        AmazonSQS sqs = SqsClientFactory.buildSqsClient(awsRegion);
        try {
            Optional<String> eventAsJsonOpt = formatErrorEventAsJson(eventPayload);
            eventAsJsonOpt.ifPresent(eventJson -> {
                var smr = sqs.sendMessage(new SendMessageRequest()
                        .withQueueUrl(queueUrl));
                log.info("successfully published to SQS");
            });
        } catch (Exception e) {
            log.error("Message failed to post into SQS queue : {} ", e);
        }
    }
    }

there when we tried to set static method return to mock Amazon sqs then returning the error. even we tried with static approach as well then it saying return type should be MockitoStatic.Verification not Amazon sqs

@ExtendWith(MockitoExtension.class)
public class SqsPublisherTest { 

    @Mock
    private AmazonSQS sqs;

    @InjectMocks
    private Publisher publisher;

    @Test
    public void testSQSClientBuilder() {
        try (MockedStatic<AmazonSQSClientBuilder> mockedBuilder = mockStatic(AmazonSQSClientBuilder.class)) {
            AmazonSQSClientBuilder mockBuilder = mock(AmazonSQSClientBuilder.class);
            mockedBuilder.when(AmazonSQSClientBuilder::standard).thenReturn(mockBuilder);

            when(mockBuilder.withRegion("us-east-1")).thenReturn(mockBuilder);
            when(mockBuilder.build()).thenReturn(sqs);

            when(SqsClientFactory.buildSqsClient("us-east-1")).thenReturn(sqs);

            publisher.sendMessageToSqs(Mockito.anyString());
        }   
}

maven pom dependencies for junit test cases

pom.xml

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>5.14.1</version>
            <scope>test</scope>
        </dependency>
        
            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

Upvotes: 0

Views: 116

Answers (0)

Related Questions