Shweta Gulati
Shweta Gulati

Reputation: 606

What happens internally when I "Run Spring as JUnit Test"?

Even when my server is not running, I can still load spring bean and property files when I run as JUnit Test.

How is this happening?

Is an instance of JVM created which compiles my test class?

Also I have not defined any classpath.

What is the default classpath? How is it able to find my spring beans?

If I want to give location for properties file with respect to which folder should its relative path be given?

Upvotes: 1

Views: 498

Answers (2)

Satvinder Hullait
Satvinder Hullait

Reputation: 109

Your Spring beans are not dependent on your server, they are initialized when your application is executed, for instance via a main method or unit tests being run.

The way in which Spring works is that you will have an application context which defines your beans, likely to be either defined as a:

Java Class - if defined in Java the class will have an @Configuration annotation and your beans will be marked with an @Bean annotation

Xml file - Which will have entries into the file which might look similar to

<bean id="myBean" class="com.my.package.MyBean">
     <constructor-arg ref="anotherBean"/>
</bean>

note: beans can also be defined through annotations as well.

Good practice dictates that ideally you should have 2 configs, one application for your service beans etc. and one infra-structure config which defines your data sources which is likely to differ depending on your environment. The ability to change your infra-structure set up dependent on environment is a key reason behind why you would be able to run tests locally as well on the server. You might find that you have profiles defined for your infra-structure config, meaning that you can cater your tests to run on different environments e.g. you don't have a SQL server on your local machine so you will use an in memory database instead.

When the application is executed, in this scenario via unit tests, it will access your spring bean definitions and start instantiating your beans (you might notice a slight delay at the start of your tests whilst this happening) once this is done, your application is then good to start running your tests.

At the top of your test class you might have some annotations: @RunWith(SpringJUnit4ClassRunner.class) - which makes the test class run with Spring support

if using java config @ContextConfiguration(classes=myConfig.class)

if using xml config @ContextConfiguration(“classpath:com/my/app/resources/my-test-config.xml”

Without seeing any code examples it is difficult to understand your question fully, but in short the @ContextConfiguration annotation on your class will tell your class where it is picking your beans up from, but you might also have profiles which define what your data sources are.

The default classpath will be whatever your project is set up to use

more information can be found at: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/

Upvotes: 1

SkorpEN
SkorpEN

Reputation: 2709

It depends, in eclipse it runs their jUnit plugin. All springs magic is due to specific runner, not jUnit itself. All spring specifics are defined in context file, but could be overriden. So if You define context file that one will be used, if no, then some default will be used.

Upvotes: 0

Related Questions