0

I am working in an Apache sling based environment. Much of my code-base can be unit tested "normally" at compile using Junit tests.

There is a subset of functionality that requires the context of a HTTP request being passed in through the Framework to work properly, and the effort for Mock all the Framework provided objects that i rely on is far too difficult.

My current approach during my test build cycle is: 1) Junit test everything I can using standard JUnit tests 2) Compile and deploy the code to my local instance 3) Make HTTP requests to specific "test" resources on my local instance that run a "test" and return some value 4) Check the return value against the expected value (repeat 3-4)

I am trying to figure out how to most effectively define my tests outside the platform (from where I am issuing the HTTP requests) and execute them from the context of my local instance.

Thoughts on the best way to structure and execute a test suite give these requirements?

empire29
  • 3,729
  • 6
  • 45
  • 71

1 Answers1

0

What your trying to do I believe is run what are normally called "Integration Tests" or "Acceptance Tests". Although the line is blurry what makes an integration test/acceptance test is that they generally involve multiple components where as a unit test should only focus on one component and mock everything.

Mybest recommendation is you create a separate test suite that will start your sling server (on a predefined port) and then run a bunch of test that will make RESTful calls to your server.

I recommend you use either Spring Rest Template or Jersey's Rest Client with JUnit. Your build script will have to start the server. Many people use Cargo (unfortuanately the cargo site is down right now) to do that.

Slowly you'll make a library and convenience fixtures/methods/objects so that it gets easier.

Community
  • 1
  • 1
Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • right, i am blurring the lines a little. The problem is that every method in my class depends on the framework, so i cannot unit test my class *at all* right now. What I'd like to do is be able to make requests http://local:9000/test/myclass?exec=obj.getProperty("foo") -- and in the servlet responsible for myclass would declare the obj, and then execute the param value of "exec" against it. (I understand this is dangerous in the wrong env). I may end up making a JSP for each method on the class that merely prints out the return val. – empire29 May 21 '12 at 14:17
  • Also, Im happy making RESTful calls to my server (dev or integration dev), I was looking for a nice concise way to executes tests against each method, without being overly verbose and creating a extra-tight coupling between my test suite and the env running the test. – empire29 May 21 '12 at 14:20