How to solve - Apache Ftp Client library is printing password on console
The problem comes while using FTPS. When developer uses login method of this…
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.
The problem comes while using FTPS. When developer uses login method of this…
Introduction This post is about hosting ElasticSearch cluster on dockerised…
While dealing with ELastic Search documents, you faced this issue while updating…
Introduction Lets take a look at how forms are being handled in ReactJS. We will…
Introduction There were few files that I need to take backup from a machine that…
Introduction This post is about hosting MongoDB replica set cluster with…
In this post, we will see some of the frequently used concepts/vocabulary in…
System design interview is pretty common these days, specially if you are having…
Introduction You are given an array of integers with size N, and a number K…
Graph Topological Sorting This is a well known problem in graph world…
Problem Statement Given a Binary tree, print out nodes in level order traversal…
Problem Statement Given an array nums of n integers and an integer target, are…