Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to write JUnit test cases for spring application  using annotations

Posted on 2013-11-20
13
Medium Priority
?
794 Views
Last Modified: 2014-01-05
hi,
i have to write JUnit testcases for spring appln through annotations,can any one suggest advice,useful links to me.
0
Comment
Question by:srikotesh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
13 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 39664483
The Spring documentation on testing is pretty good, although (to me at least) seems to not be covered in the best order. Therefore, I suggest to check out the docs here...

http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/testing.html

...but probably start reading from section "10.3.5 Spring TestContext Framework".


In a nutshell though, the annotations that will be of the biggest use to you are...
@RunWith(SpringJUnit4ClassRunner.class)
// This is a JUnit annotation enables Spring functionality



@ContextConfiguration({"/app-config.xml", "/test-config.xml"})
// If you use XML based Spring configuration

       or

@ContextConfiguration(classes={AppConfig.class, TestConfig.class})
// If you use JavaCofig based Spring configuration

Open in new window

Once you have this setup, you can use normal Spring @Autowired annotations to inject references from your Spring configuration into your test class, and then use the normal JUnit annotations to run your tests.


If you can provide more information about your unique situation/requirements we can probably help a little more too.
0
 
LVL 2

Author Comment

by:srikotesh
ID: 39682746
I've requested that this question be deleted for the following reason:

i know those steps
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39676882
You need to provide a proper explanation for deleting the question. What you have done is not sufficient. If you don't do that then the delete request will be stopped and you will need to close the question in a different way.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 36

Expert Comment

by:mccarl
ID: 39682747
I'm objecting to the deletion of this question, as I believe my comment (http:Q_28299158.html#a39664483) sufficiently answers the (very briefly worded) question. Also, the question author has made no attempt to follow up on my comment or ask additional questions. And finally, the (also very briefly worded) explanation for deletion is inadequate. Attempts to resolve this with the question author have been ignored.
0
 
LVL 2

Author Comment

by:srikotesh
ID: 39682820
sorry for that attempt actually i postponed  my practice. i need to test spring application using junit testcasese.

i created one testcase and i imported those two steps
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:data-servlet.xml"})
after  that i called one method i inserted hard code values finally i called service mehod

while running the junit test  i am getting error.

Failed to load ApplicationContext

parsing XML document from class path resource [data-servlet.xml]; nested exception is java.io.FileNotFoundException: class path resource [data-servlet.xml] cannot be opened because it does not exist

class path resource [data-servlet.xml] cannot be opened because it does not exist

any problem if i import the statement like this @ContextConfiguration(locations = {"classpath:data-servlet.xml"})
can u give a proper suggestion for me.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39684634
any problem if i import the statement like this
No, it appears that you are correctly using the ContextConfiguration annotation. The problem might be exactly where Spring is looking for the file.

Can you tell me the exact directory that your "data-servlet.xml" is located in? Also can you tell me the package name that this TestCase class has?
0
 
LVL 2

Author Comment

by:srikotesh
ID: 39691920
directory location for data-servlet.xml is:
WebContent/WEB-INF/data-servlet.xml

pkg name for Test class:
com.hcltech.poc.ets.test.ManagerPageControllerTest

pkg name for Service class:
com.hcltech.poc.ets.service.TaskServiceProvider
0
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39694254
Ah ok, so the problem is that your Webcontent/WEB-INF directories AREN'T on the classpath and so accessing the spring xml by the "classpath:data-servlet.xml" URL isn't possible. You may be able to get it to load by using "file:WebContent/WEB-INF/data-servlet.xml" as the URL to @ContextConfiguration


However, I think this may be an indication that you can/should organise your project in a slightly different way. What is the nature of your Junit test that you are trying to run? Do the truly need access to all the beans in your data-servlet.xml?

Typically, the above is not the case. You normally setup Junit to just test small units of your code, such as a Service object, and you mock out any DAO objects and other dependencies that they have. And then you Spring configuration works something like this... Your data-servlet.xml contains only the configuration specific to the "Web" part of your app, such as setting up controllers, views, configuring mappings, etc. And then any configuration of Service objects is moved out to a separate .xml file that IS on your classpath (such as src/main/resources if you are using maven, etc). At runtime the web app needs to access those Service objects, so your data-servlet.xml will contain a line such as <import resource="classpath:service-beans.xml" /> to bring those in. And then your unit tests can also load your Service objects by using the @ContextConfiguration annotation with a "classpath:" URL in a similar way to what you have been doing. This way, it means that you only load up JUST what you need for the unit tests and nothing more.
0
 
LVL 2

Author Comment

by:srikotesh
ID: 39756244
Thanks for your suggestions i have a habit like if i didn't get the solution for my question i will delete my question from the portal i think mccrl you saw only my answered questions previously i deleted many questions because of i didn't get exact input from experts.

I dont know A is the default rating just now i saw the ratings.
0
 
LVL 2

Author Closing Comment

by:srikotesh
ID: 39756248
i got the proper input from you.Thanks for your suggestion.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39758334
@srikotesh,

Thanks for accepting, and explaining why your reasons for grading the way your did.

And to follow from what SouthMod posted, I didn't bring this up just for my own benefit, or to enforce the policy for sake of enforcing it, but to help you to hopefully get a high level of help from the other experts here. :)

Cheers
mccarl
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question