I am new in JUnittesting so i have a question. Can anyone please tell me why we use ReflectionTestUtils.setField() in our Junit testing with example.

5

4 Answers

Like mentioned in the comment, the java docs explains the usage very well. But I want to give you also a simple example.

Lets say you have an Entity class with private or protected field access and no provided setter method.

@Entity public class MyEntity { @Id private Long id; public Long getId(Long id){ this.id = id; } } 

In you test class you are not able to set an id of your entity because of the missing setter method.

Using ReflectionTestUtils.setField you are able to do that for testing purpose:

ReflectionTestUtils.setField(myEntity, "id", 1); 

The Parameters are described:

public static void setField(Object targetObject, String name, Object value) Set the field with the given name on the provided targetObject to the supplied value. This method delegates to setField(Object, String, Object, Class), supplying null for the type argument. Parameters: targetObject - the target object on which to set the field; never null name - the name of the field to set; never null value - the value to set 

But give it a try and read the docs.

2

One more Use Case:

We externalised many properties Such as: URL's , endpoint and many other properties in application properties like below:

kf.get.profile.endpoint=/profile kf.get.clients.endpoint=clients 

and then use it in application like below:

 @Value("${kf.get.clients.endpoint}") private String getClientEndpoint 

and whenever when we write unit test , we get NullPointerException because Spring cannot inject @value similarly as @Autowired does. ( at least at the moment , I dont know alternative. ) so to avoid that we can use ReflectionTestUtils to inject externalised properties. like below:

ReflectionTestUtils.setField(targetObject,"getClientEndpoint","lorem"); 

it's very useful when we want to write unit test, such as:

class A{ int getValue(); } class B{ A a; int caculate(){ ... int v = a.getValue(); .... } } class ServiceTest{ @Test public void caculateTest(){ B serviceB = new B(); A serviceA = Mockito.mock(A.class); Mockito.when(serviceA.getValue()).thenReturn(5); ReflectionTestUtils.setField(serviceB, "a", serviceA); } } 

Thank you for above discussions, the below part can also contribute in writing the unit tests by reading the properties from application-test.properties.

import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.util.ReflectionTestUtils; import java.util.List; import static org.junit.jupiter.api.Assertions.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestPropertySource("classpath:application-test.properties") public class FileRetrivalServiceTest { @Value ("${fs.local.quarantine.directory}") private String localQuarantineDirectory; @Value ("${fs.local.quarantine.wrong.directory}") private String localQuarantineWrongDirectory; @Value ("${fs.local.quarantine.tmp.directory}") private String localQuarantineTmpDirectory; @Value ("${fs.local.keys.file}") private String localKeyFile; private FileRetrivalService fileRetrivalService; @Before public void setUp() throws Exception { fileRetrivalService = new FileRetrivalServiceImpl(); ReflectionTestUtils.setField(fileRetrivalService, "keyFile", localKeyFile); } @Test public void shouldRetrieveListOfFilesInQuarantineDirectory() { // given ReflectionTestUtils.setField(fileRetrivalService, "quarantineDirectory", localQuarantineDirectory); // when List<IcrFileModel> quarantineFiles = fileRetrivalService.retrieveListOfFilesInQuarantineDirectory(); // then assertNotNull(quarantineFiles); assertEquals(quarantineFiles.size(), 4); } } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy