JUnit testing with Acegi.

I tried to make my unit tests run after disabling it for long time for some reasons ;)
But whene I attempted to make it run it gives me Access Denied off course this after configuring the required depenedencies.
Oooh I forget to say that this project has Acegi now for authintication and authorization and this was the problem of making unit tests fails.

So what I will write here is what I had done to make my unit test rework again with Acegi.

The secured method interceptor needs:

1. An authenticated user to use it in test. 2. Authorities that this user has to able to use it.

1. An authentication user to use it in test.

One way is to create user and give him all the authorities to be able to run the tests on all the methods.

Acegi has TestingAuthenticationToken for unit testing purposes, so we can create the authenticated user that we can use it. as follows:

// Grant all roles to Ali.
TestingAuthenticationToken testToken = new TestingAuthenticationToken(
"Ali", "Ali", new GrantedAuthority[] {
new GrantedAuthorityImpl("ROLE_ONE"),
new GrantedAuthorityImpl("ROLE_TWO"),
new GrantedAuthorityImpl("ROLE_THREE"),
new GrantedAuthorityImpl("ROLE_FOUR")});

2. Authorities that this user should has to be able to call the secured methods.

You will need to have authentication provider.
So we will use the ProviderManager to leave the configuration file as it is and dynamically changes the provider in the setup of the unit test at run time as follows"

ProviderManager providerManager = (ProviderManager) _appContext.getBean("authenticationManager");
List list = new ArrayList();
list.add(new TestingAuthenticationProvider());

3. An extra [the last] step:

// Create and store the Acegi SecurityContext into the SecurityContextHolder.
SecurityContext securityContext = new SecurityContextImpl();

Nice post!

I really appreciate the effort of writing this blog as it is very helpful. I would also come up with such a world via help with assignment.

It is nice to learn the

It is nice to learn the steps on how to do it. Most especially when we talked of different stages. - Dennis Wong YOR Health

Powered by Drupal - Design by artinet