React JS router not working on Nginx docker container
Problem Statement I developed a simple ReactJS application where I have used…
March 17, 2017
While writing JUnit test cases, we encounter cases like we want to initialize a class, and that class instantiate a new class object which is an external entity class like FTP class or AWS service class. We do not want to initializethat external class object. In that cases, we would want to mock those objects.
We can mock any object in JUnit test code. But, what about the objects that are instantiated using new inside that class.
class AmazonS3ServiceImpl {
private AmazonS3Client amazonS3Client;
public AmazonS3ServiceImpl() {
this.amazonS3Client = new AmazonS3Client();
}
}
Here, we would want to inject our mock object for AmazonS3Client, but how?
You need to annotate your JUnit test class with “@PrepareForTest” and mention both the classes you want to mock. See below:
@RunWith(PowerMockRunner.class)
@PrepareForTest({AmazonS3Client.class, AmazonS3ServiceImpl.class})
public class S3Test {
@Before
public void setup() throws Exception {
AmazonS3Client amazonS3Client = PowerMockito.mock(AmazonS3Client.class);
PowerMockito.whenNew(AmazonS3Client.class).withParameterTypes(AWSCredentials.class).
withArguments(Mockito.any()).thenReturn(amazonS3Client); //code for mocking Impl class
}
}
So, we have informed Mocking system that whenever you are trying to instantiate a new object of AmazonS3Client class with AWSCredentials type parameter, return our mock object.
Problem Statement I developed a simple ReactJS application where I have used…
Introduction In Azure, assuming you are having a storage container. And, you…
Pre-requisite Assuming you have a mongodb database, and you want to take backup…
Introduction I had to create many repositories in an Github organization. I…
Introduction To give some context, I have two python files. (Both in same folder…
Introduction In this post we will see following: How to schedule a job on cron…
Introduction There are some cases, where I need another git repository while…
Introduction In this post, we will see how to fetch multiple credentials and…
Introduction I have an automation script, that I want to run on different…
Introduction I had to write a CICD system for one of our project. I had to…
Introduction Java log4j has many ways to initialize and append the desired…