Mocking external service returns null - java

I got pretty simple Dictionary class that makes a call to external API.
public class Dictionary {
protected ExternalService service = new ExternalService();
public String getValue(String key, String dictionaryName) {
ExternalServiceInput input = new ExternalServiceInput();
ExternalServiceOutput output = new ExternalServiceOutput();
input.setKey(key);
input.setDictionaryName(dictionaryName);
try {
output = service.invoke(input);
} catch (Exception e) {
return null;
}
return output.getValue();
}
}
It works fine, but I wanted to write Unit Tests for this, so I decided I need to mock service.invoke().
#Mock
private ExternalService service;
#InjectMocks
private Dictionary dictionary;
#InjectMocks
private ExternalServiceOutput output;
#InjectMocks
private ExternalServiceInput input;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
input.setKey("testKey");
input.setDictionaryName("testDictionary");
output.setValue("testValue");
}
#Test
public void shouldReturnValue() throws Exception {
when(service.invoke(input)).thenReturn(output);
assertEquals(output.getValue(), dictionary.getValue(input.getKey(), input.getDictionaryName()));
}
I'v tried with Input and Output as regular field or initialize it in setUp method, everything ends up with NullPointerException at Dictionary class at
return output.getValue();
Can someone point me what I did wrong?

You should override equals and hashCode in your ExternalServiceInput class or change your mock to accept any object of ExternalServiceInput
when(service.invoke(Mockito.any(ExternalServiceInput.class))).thenReturn(output);

Related

Mockito -- Response from service returns null in test - Java

I'm working on testing a class. This class calls on a service (let's say its called client, and we want to call client.put())
put() should return a response, but at least in the test, the response is null
I don't know if I just didn't setup the mock correctly and wanted to sanity check with you guys here
public class ATest {
#Mock
private ServiceProto.PutItemsResponse res;
...(private variables)...
#Before
public void setUp() throws Exception {
client = mock(Client.class);
clientFactory = mock(ClientFactory.class);
when(clientFactory.get(any())).thenReturn(client);
...(initializing private vars for constructor as mock variables, example below...)
captionConverter = mock(CaptionToCTItemConverter.class);
when(privateVar.convert(any(obj.class))).thenReturn(Item.newBuilder().build());
classAToTest = spy(new ClassAToTest(private variables);
}
#Test
public void putItem() {
long id = 4710582L;
AObject aObject = testUtils.getObject();
doReturn(res).when(client).putItems(any(ServiceProto.PutItemsRequest.class));
System.out.println("result is "+ res);
try {
classAToTest.putMethod(aObject);
}
catch (NullPointerException e) {
}
verify(creativeToolsClient, Mockito.times(1)).putItems(any(IngestionServiceProto.PutItemsRequest.class));
}
}
And this is the method being tested
public void putMethod(AObject aObject) {
final String id = Long.toString(aObject.getId());
ServiceProto.PutItemsResponse putItemsResponse = null;
Exception putItemsFailure = null;
putItemsResponse =
client.putItems(ServiceProto.PutItemsRequest.newBuilder()
.putItems(
id,
ServiceProto.PutItemsRequest.Item.newBuilder()).build())
.build());
if (putItemsResponse == null) {
logger.warning("PutItems request has failed: "+
(putItemsFailure == null ? "null" : putItemsFailure.getMessage()));
}
}
and when I run it it gives the warning
The putItems method works for other people. Did I set up mock incorrectly?
res variable is not initialized. To mock objects with #Mock annotation use
#ExtendWith(MockitoExtension.class)
public class ATest {
...

How to mock the forEach elements which is iterate over the minioclient collection?

I am using minioClient.listObjects to fetch all the objects from s3. It returns the iterable Result<Item>. Here I was iterating over these results.
public class S3class
{ //this class is part of spring boot application. So here no main class.
public void s3method(){
MinioClient minioClient = MinioClient.builder().endpoint("")
.build();
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder()
.bucket("bucket_name")
.recursive(true)
.startAfter("something")
.build()
);
for (Result<Item> result : results) {
Item item;
try {
System.out.println("inside try");
item = result.get(); ///it returns null. How to mock this
System.out.println("if it reach here, no problem");
}
catch (Exception e) {
System.out.println("problem is here");
throw new Error();
}
Tags tags = minioClient.getObjectTags(
GetObjectTagsArgs.builder()
.bucket(bucket)
.object(item.objectName()) //beforei was tried like result.get().objectName(). but it fails. Here always i want to return "dummy" string. but it gives **null**.
.build()
);
}
}
}
Testing method
#RunWith(MockitoJUnitRunner.class)
public class Testclass{
#InjectMocks
private S3class s3class;
#Mock
private MinioClient minioClient;
#Mock
private Result<Item> result;
#Mock
private Iterable<Result<Item>> results;
#Mock
private Iterator<Result<Item>> iterator;
#Mock
private Consumer<Result<Item>> consumer;
public void testmethod(){
Item item = new Item("dummy") {};
result = new Result<Item>(item);
// Mockito.when(result.get()).thenReturn(item);
Mockito.when(minioClient.listObjects(
Mockito.any(ListObjectsArgs.class)))
.thenReturn(results);
Mockito.doCallRealMethod().when(results).forEach(consumer);
Mockito.when(iterator.hasNext()).thenReturn(true,false);
Mockito.when(iterator.next()).thenReturn(result);
Mockito.when(results.iterator()).thenReturn(iterator);
s3class.s3method();
}
}
I mocked the minioclient. But I can't mock this result.get().objectName(). I used when and thenReturn but no use. Here I want to return some string whenever result.get().objectName() called.
I tried this whole day in many ways. But I couldn't. Can anyone help me with this?

JMockit verify if private method is called

I am testig a public method and I want to verify if a private method, that have mocked params, is called.
All the answers I have found are using invoke method, but this was removed since JMockit v1.36
public class ClassToTest{
public void methodToTest(){
DependencyClass abc = new DependencyClass();
if(privateMethod1()){
privateMethod2(abc);
}
}
private boolean privateMethod1(){ return true; }
private void privateMethod2(DependencyClass abc){ abc.doStuff(); }
}
public class testClassToTest{
#Mocked
DependencyClass abc;
#Tested
ClassToTest testedClass;
#BeforeEach
public void setUp() {
testedClass = new ClassToTest();
}
#Test
public void testMethod(){
new MockUp<ClassToTest>() {
#Mock
private boolean privateMethod1() {
return true;
}
};
testedClass.methodToTest();
new FullVerificationsInOrder() {{
abc = new DependencyClass();
//Check here if privateMethod2(abc) gets called once
}};
}
You can use Powermock to mock and verify private methods.
Please check https://github.com/powermock/powermock/wiki/MockPrivate
You have two ways:
To level up your method's scope from private to package-private and after it, your method will become visible in the test.
Refactoring your code and encapsulate the private method to Predicate and after it, you can test your primary method and Predicate separately.
You can't test the private method by Junit.

How do I test Function's code when it's passed as method parameter?

Is it possible to test code that is written in lambda function that is passed inside the method process?
#AllArgsConstructor
public class JsonController {
private final JsonElementProcessingService jsonElementProcessingService;
private final JsonObjectProcessingService jsonObjectProcessingService;
private final JsonArrayProcessingService jsonArrayProcessingService;
public void process(String rawJson) {
jsonElementProcessingService.process(json -> {
JsonElement element = new JsonParser().parse(json);
if (element.isJsonArray()) {
return jsonArrayProcessingService.process(element.getAsJsonArray());
} else {
return jsonObjectProcessingService.process(element.getAsJsonObject());
}
}, rawJson);
}
}
Since the lambda is lazy the function is not invoked (Function::apply) when I call JsonController::process so is there any way to check that jsonArrayProcessingService::process is called?
#RunWith(JMockit.class)
public class JsonControllerTest {
#Injectable
private JsonElementProcessingService jsonElementProcessingService;
#Injectable
private JsonObjectProcessingService jsonObjectProcessingService;
#Injectable
private JsonArrayProcessingService jsonArrayProcessingService;
#Tested
private JsonController jsonController;
#Test
public void test() {
jsonController.process("[{\"key\":1}]");
// how check here that jsonArrayProcessingService was invoked?
}
}
Just make it testable (and readable) by converting it to a method:
public void process(String rawJson) {
jsonElementProcessingService.process(this::parse, rawJson);
}
Object parse(String json) {
JsonElement element = new JsonParser().parse(json);
if (element.isJsonArray()) {
return jsonArrayProcessingService.process(element.getAsJsonArray());
} else {
return jsonObjectProcessingService.process(element.getAsJsonObject());
}
}
The relevant guiding principles I personally follow are:
anytime my lambdas require curly brackets, convert them to a method
organise code so that it can be unit tested
You may need to change the return type of the parse method to match whatever your processing services (which you didn’t show) return.
Given its relatively-basic redirection logic, don't you just want to confirm which of the #Injectables got called:
#Test
public void test() {
jsonController.process("[{\"key\":1}]");
new Verifications() {{
jsonArrayProcessingService.process(withInstanceOf(JsonArray.class));
}};
}

Mockito wanted but not invoked utility class

I am making a unit test for class in my app, it just a simple class and I thought i did everything right but the test failed saying
Wanted but not invoked:
mContextWeakReference.get();
-> at rahmat.com.app.utility.backwardcompatibility.StringResourceUtilTest.getString(StringResourceUtilTest.java:40)
Actually, there were zero interactions with this mock.
this is the class to be tested
public class StringResourceUtil {
private static StringResourceUtil sInstance;
private WeakReference<Context> mContextWeakReference;
public static StringResourceUtil getInstance() {
return sInstance;
}
#Inject
public StringResourceUtil(Context context) {
mContextWeakReference = new WeakReference<>(context);
sInstance = this; //NOSONAR
}
public String getString(int resId) {
return mContextWeakReference.get().getString(resId);
}}
this is unit test I made
public class StringResourceUtilTest {
private StringResourceUtil mResourceUtil;
#Mock
private Context mContext;
#Mock
private WeakReference<Context> mContextWeakReference;
#Before
public void setUp(){
MockitoAnnotations.initMocks(this);
mResourceUtil = new StringResourceUtil(mContext);
}
#Test
public void getString() {
int resId = 123;
mResourceUtil.getString(resId);
verify(mContextWeakReference).get().getString(eq(resId));
}}
any help would be much appreciated, thanks
Your StringUtil class is always creating a new object of mContextWeakReference object and even if you are making it, it won't inject automatically ( for that you use injectMock but no use here, since new object creation always happens internally).
public class StringResourceUtilTest {
private StringResourceUtil mResourceUtil;
#Mock
private Context mContext;
#Before
public void setUp(){
MockitoAnnotations.initMocks(this);
mResourceUtil = new StringResourceUtil(mContext);
// setup mock return type
// mock objects are not real,so need to moeck the behavior of method as well
when(mContext.getString(R.string.a123)).thenReturn("123");
}
#Test
public void getString() {
int resId = R.string.a123;
// check the return type
assertEquals("123",mResourceUtil.getString(resId));
}
}
Note: To verify the internal working, read
What is the difference between mocking and spying when using Mockito?
Because you creating mContextWeakReference = new WeakReference<>(context); in constructor, it will never been a mock in StringResourceUtil.
You can set prepared mock mContextWeakReference by using
org.springframework.test.util.ReflectionTestUtils.setField(mResourceUtil , "mContextWeakReference", mContextWeakReference);
Otherwise you should modifying StringResourceUtil class to be a testable

Categories

Resources