microsoft / kiota-java

Java libraries for Kiota-generated API clients.
https://aka.ms/kiota/docs
MIT License
22 stars 23 forks source link

Refactor to Eliminate Repetitive Mock Object Creation in RequestInformationTest #1374

Closed gzhao9 closed 3 weeks ago

gzhao9 commented 3 weeks ago

Hi there!

While working with theRequestInformationTest class. I've noticed that there are 2 mock variables repeatedly created across various tests. To simplify the code, I propose a small refactor to eliminate these redundancies, which could reduce the code by 20 lines.

  1. Mock RequestAdapter Creation: Repeated 5 times.
  2. Mock SerializationWriterFactory Creation: Repeated 5 times.

For instance, the mock creation logic is as follows:

final SerializationWriterFactory factoryMock = mock(SerializationWriterFactory.class);
when(factoryMock.getSerializationWriter(anyString())).thenReturn(writerMock);
final RequestAdapter requestAdapterMock = mock(RequestAdapter.class);
when(requestAdapterMock.getSerializationWriterFactory()).thenReturn(factoryMock);

To eliminate redundancy, We can create two methods that can be called repeatedly, createMockSerializationWriterFactory and createMockRequestAdapter:

public final SerializationWriterFactory createMockSerializationWriterFactory(SerializationWriter writer){
    final SerializationWriterFactory factoryMock = mock(SerializationWriterFactory.class);
    when(factoryMock.getSerializationWriter(anyString())).thenReturn(writer);
    return  factoryMock;
}
public final RequestAdapter createMockRequestAdapter(SerializationWriterFactory factory){
    final RequestAdapter requestAdapterMock = mock(RequestAdapter.class);
    when(requestAdapterMock.getSerializationWriterFactory()).thenReturn(factory);
    return  requestAdapterMock;
}

Using these methods, the refactored code becomes much cleaner:

final SerializationWriterFactory factoryMock = createMockSerializationWriterFactory(writer);
final RequestAdapter requestAdapterMock = createMockRequestAdapter(factoryMock );

And for further improvement, we can overload createMockRequestAdapter for a more streamlined approach:

public final RequestAdapter createMockRequestAdapter(SerializationWriter writer){
    return  createMockRequestAdapter(createMockSerializationWriterFactory(writer));
}

With this refactoring, mock creation and configuration can be done in a single line:

final RequestAdapter requestAdapterMock = createMockRequestAdapter(writerMock);

I’ve created a draft PR in my forked project where you can see the detailed changes here. The refactor reduced the test cases by 20 lines of code, and I believe these changes will improve code readability.

baywet commented 3 weeks ago

Hi @gzhao9 Thanks for bringing this up and starting a PR! Please submit a pull request over here whenever things are ready.

gzhao9 commented 3 weeks ago

Hi @gzhao9 Thanks for bringing this up and starting a PR! Please submit a pull request over here whenever things are ready.

Hello, I have submitted the PR #1375