views:

133

answers:

2

Hi all,

I am writing a junit test in order to (surprisingly) test a part of my app as a standalone.

Thing is I execute the following line in a constructor being invoked in the unit test:

WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();

How would I go about to set up a web context when running a simple unit test?

Thanks

+1  A: 

You usually don't make use of a WebApplicationContext in unit testing. Unit testing is about testing classes in isolation. You shouldn't have to start the whole application to make the test. If you need it, you can mock an HttpServletRequest object with a mocking framework, like EasyMock.

kgiannakakis
+3  A: 
  • In unit tests, test with mocks.
  • In integration tests you may actually want to start an embedded servlet container. Here's a approach.
lexicore