1

How do we test action classes of Applications written in 2.0.x version?

I read through this link and found out that, struts-junit plugin is only available for struts 2.1.1 or later.

My application is written using struts 2.0.11 and I don't think upgrading the version is easy in my project.

How do we unit test Action classes,given these limitations ?

Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130

1 Answers1

1

Is there any special aspects of your action classes that you would like to test?

As long as your action classes are written as POJOs, you can test them as ordinary Java classes using any testing framework of your choice.

EDIT: Give a try this custom class which extends JUnit's TestCase class. You will need dependency to org.springframework: spring-mock.

jilt3d
  • 3,864
  • 8
  • 34
  • 41
  • It has http request and response objects. It can't be tested as POJOs – Vinoth Kumar C M Oct 11 '12 at 12:51
  • you say it uses"http request and response objects", couldn't you inject these into your action? You would then just mock them using mockito for example. @jilt3d advices to test them as POJO. Don't add any other dependency, keep your tests simple. – Alban Oct 11 '12 at 16:21
  • Also checkout this [question](http://stackoverflow.com/questions/10756591/strut-2-3-1-2-unit-test-how-to-remove-spring-codependency-vs-npe-with-getcontex/11051902#11051902): It is about setting up struts2 ServletContext to avoid some issues we have when junit-testing struts2 actions as POJO – Alban Oct 11 '12 at 16:29
  • @VinothKumar The objects an instance has isn't related to whether or not it's a POJO, and (essentially) anything can be mocked or stubbed. You can also integration test if your classes aren't designed to be testable and the ROI isn't worth it. – Dave Newton Oct 11 '12 at 17:34