Apache Struts Unit Testing with JUnit Tutorial |Video upload date:  · Duration: PT7M29S  · Language: EN

Learn how to unit test Apache Struts actions with JUnit and mocks for fast reliable tests and better controller coverage.

So you have Struts actions that need testing and time is short. This guide walks through testing Struts2 actions with JUnit and Mockito in a way that actually saves time and sanity. No full container boot ups and no flaky tests that fail only on Fridays when the coffee runs out.

Set up project dependencies and fast test runner

Add JUnit and a mocking library such as Mockito to your build. If your project uses Maven include the Struts test support classes or the lightweight helpers your team prefers. Configure a test profile that runs only the fast unit tests by default so your build stays speedy and your CI stays happy.

Write a focused test for a single Action class

Keep tests narrow. Instantiate the Action under test as a plain object, populate the fields or setters it expects, and call the method that performs the action logic. Resist the urge to start the whole app server. Full container startup is slow and brittle and that kind of drama belongs in runtime logs not unit tests.

Mock HTTP parameters and service collaborators

Use Mockito to create fake request parameters session attributes and service dependencies. Options include mocking the servlet request and session or using Struts test utilities to provide a light ActionContext. The goal is to isolate controller logic from external systems so you test behavior not integration plumbing.

  • Mock form parameters and session values to simulate user input.
  • Mock service layer collaborators and stub their responses to cover success and error cases.
  • Use ArgumentCaptor when you need to inspect what was sent to a service.

Assert results and internal state

Verify the action result string that drives navigation and any changes to model properties or validation messages. Assert that validation rules fire when they should and that error messages are added when they should not be ignored. When interaction matters use Mockito verify calls to assert how the service layer was used.

Run tests in CI and keep them deterministic

Make tests hermetic. Do not let network calls or real database access sneak in. Use in memory stubs or dedicated test doubles. Split slow integration tests from fast unit tests with a Maven profile or tagging strategy and run the slow suite less often. Fast reliable tests on every commit keep refactoring from feeling like Russian roulette.

Quick checklist for Struts2 action testing

  • Target one action per test to keep failures easy to diagnose
  • Mock HTTP inputs and session state so tests stay focused
  • Mock the service layer and assert interactions with Mockito
  • Avoid full container startup unless the test is marked integration
  • Keep CI profiles so unit tests run fast and integration runs on demand

Follow these rules and your Struts action tests will be quick to write and kinder to CI. You will sleep better and your teammates will stop blaming the framework for everything.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.