Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you want to test features implemented via Spring AOP, you need to use <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#testcontext-framework" rel="noreferrer">Spring TestContext</a> framework to run tests against application context.</p> <p>Then you create a base test with minimal security configuration:</p> <p><code>abstract-security-test.xml</code>:</p> <pre><code>&lt;security:authentication-manager alias="authenticationManager"&gt; &lt;security:authentication-provider user-service-ref = "userService" /&gt; &lt;/security:authentication-manager&gt; &lt;security:global-method-security pre-post-annotations="enabled" /&gt; &lt;bean id = "userService" class = "..." /&gt; </code></pre> <p><code>AbstractSecurityTest.java</code>:</p> <pre><code>@ContextConfiguration("abstract-security-test.xml") abstract public class AbstractSecurityTest { @Autowired private AuthenticationManager am; @After public void clear() { SecurityContextHolder.clearContext(); } protected void login(String name, String password) { Authentication auth = new UsernamePasswordAuthenticationToken(name, password); SecurityContextHolder.getContext().setAuthentication(am.authenticate(auth)); } } </code></pre> <p>Now you can use it in your tests:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(...) public class CreatePostControllerSecurityTest extends AbstractSecurityTest { ... @Test @ExpectedException(AuthenticationCredentialsNotFoundException.class) public void testNoAuth() { controller.modifyContent(...); } @Test @ExpectedException(AccessDeniedException.class) public void testAccessDenied() { login("userWithoutAccessRight", "..."); controller.modifyContent(...); } @Test public void testAuthOK() { login("userWithAccessRight", "..."); controller.modifyContent(...); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload