How To mock a public variable using mockmvc in junit testcases

41 Views Asked by At

How do we mock a public variable used in the controller which was collecting its static value from the application.properties and using in the controller and We need to make the testcases who will not allowed to load the application.properties context file so how do we mock that variable so is their any other way in which we can create the new variable.properties and load that for the test cases and also for the controller so how can we do it also?

Is it a good approach to ignore the main context file which has many of the secure configuration.

1

There are 1 best solutions below

2
On

What you need are spring profiles.

You can create a separate application.properties file that will be used just for testing, for the test profile to be more exact. It should be called application-test.yml and contain the same properties as the original one, but of course with values that will be used for testing.

That file will then be used by tests that have defined this annotation @ActiveProfiles(value = "test") in their test class.

Please use this great guide to quickly and easily set up properties (profiles) which can be used for testing in spring.