I wrote a test code (Junit & mockit) for the code that calls the AWS API (Java)

Trigger

I had a program to check the stack status of AWS CloudFormation. It is necessary to test all statuses because the processing is different for each status returned.

Task

You can't control the status of the AWS CloudFormation stack here.

How do you test it?

Write test code in Junit, I decided to specify (mock) the status returned by the API.

The part you want to test

Below is the code part to be tested

describeStacks.java


/*Creating a client for executing CloudFormation API*/
AmazonCloudFormationAsync CFclient = AmazonCloudFormationAsyncClientBuilder.standard()
		.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("hope", "huga")))
		.build();

/*DescribeStacks Specify the target stack name*/
DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest();
describeStacksRequest.withStackName("hogestack");

/*API to get the stack status here(describeStacks)(Where you want to mock)*/
DescribeStacksResult describeStacksResult = CFclient.describeStacks(describeStacksRequest);

Mocking

describeStacksTest.java


@RunWith(MockitoJUnitRunner.class)
public class describeStacksTest {

    /*Declare the class to mock*/
    @Mock AmazonCloudFormationAsync AmazonCloudFormationMock;
    /*Specify the class to inject the class to be mocked*/
    @InjectMocks describeStacksClass describeStacks = new describeStacksClass();

	@Before
	public void init() {
        /*Mock initialization*/
        MockitoAnnotations.initMocks(this);
	}
	@Test
public void Controlling stack status() {
        /*Create a class of return values when describeStacks is executed*/
		DescribeStacksResult describeStacksResult = new DescribeStacksResult();
		Stack stacks = new Stack();
		stacks.setStackStatus(StackStatus.CREATE_COMPLETE); //Set StackStatus(This time CREATE_Specify the status of COMPLETE)
		describeStacksResult.withStacks(stacks);

        /*Create a class with the same arguments that the test target executes*/
		DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest();
		describeStacksRequest.withStackName("hogestack");

        /*Specify the run-time arguments and return values of describeStacks for the mocked class*/
		when(AmazonCloudFormationMock.describeStacks(DescribeStacksRequest)).thenReturn(describeStacksResult);

Impressions

Since the AmazonCloudFormationAsync class was public, it was easily mocked. Since it is a class provided as an API, it was easy to mock because it was public. It's true that setters are easy for Resule classes (did you expect AWS to be mocked or stubized?) I think other APIs can be tested in the same way.

Recommended Posts

I wrote a test code (Junit & mockit) for the code that calls the AWS API (Java)
I investigated Randoop, a JUnit test class generator for Java
I wrote a primality test program in Java
[RSpec] I wrote a test for uploading a profile image.
I made a Wrapper that calls KNP from Java
I wrote a test with Spring Boot + JUnit 5 now
I passed the Java test level 2 so I will leave a note
I wrote a Stalin sort that feels like a mess in Java
A story that I finally understood Java for statement as a non-engineer
How to deal with the type that I thought about writing a Java program for 2 years
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
[AWS SDK for Java] Set a retry policy on the S3 client
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
I tried to make a program that searches for the target class from the process that is overloaded with Java
03. I sent a request from Spring Boot to the zip code search API
I made a THETA API client that can be used for plug-in development
Code that deletes all files of the specified prefix in AWS S3 (Java)
I searched for a lightweight framework that answers the sudden "make it quickly"
I made a Diff tool for Java files
I made a primality test program in Java
[Java] How to test for null with JUnit
ChatWork4j for using the ChatWork API in Java
[API] I tried using the zip code search API
I built a Code Pipeline with AWS CDK.
I wrote a prime factorization program in Java
Generate Java client code for Salesforce SOAP API
Set a signed cookie (for CloudFront) with a custom policy using the AWS SDK for Java
Dreaming of easily creating a Web API for the DB of an existing Java system
[CircleCI] I will explain the stupid configuration file (config.yml) that I wrote for the first time.
I made a check tool for the release module
I read the readable code, so make a note
A note for Initializing Fields in the Java tutorial
[Swift] API used for apps that passed the selection
I wrote a sequence diagram of the j.u.c.Flow sample
Credentials referenced by the AWS SDK for Java by default
I want to write a loop that references an index with Java 8's Stream API
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
I made a reply function for the Rails Tutorial extension (Part 4): A function that makes the user unique
I made a program in Java that solves the traveling salesman problem with a genetic algorithm