You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by lh...@apache.org on 2010/12/07 20:06:41 UTC

svn commit: r1043165 - in /shiro/trunk/core/src/test/java/org/apache/shiro/test: ExampleShiroIntegrationTest.java ExampleShiroTest.java ExampleShiroUnitTest.java

Author: lhazlewood
Date: Tue Dec  7 19:06:41 2010
New Revision: 1043165

URL: http://svn.apache.org/viewvc?rev=1043165&view=rev
Log:
Updated example test classes to reflect updated documentation.

Added:
    shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroIntegrationTest.java
      - copied, changed from r1040232, shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroTest.java
    shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroUnitTest.java
Removed:
    shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroTest.java

Copied: shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroIntegrationTest.java (from r1040232, shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroTest.java)
URL: http://svn.apache.org/viewvc/shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroIntegrationTest.java?p2=shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroIntegrationTest.java&p1=shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroTest.java&r1=1040232&r2=1043165&rev=1043165&view=diff
==============================================================================
--- shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroTest.java (original)
+++ shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroIntegrationTest.java Tue Dec  7 19:06:41 2010
@@ -13,7 +13,7 @@ import org.junit.Test;
  *
  * @since 1.1.1
  */
-public class ExampleShiroTest extends AbstractShiroTest {
+public class ExampleShiroIntegrationTest extends AbstractShiroTest {
 
     @BeforeClass
     public static void beforeClass() {
@@ -32,7 +32,9 @@ public class ExampleShiroTest extends Ab
         //2. Bind the subject to the current thread:
         setSubject(subjectUnderTest);
 
-        //perform test logic here.
+        //perform test logic here.  Any call to
+        //SecurityUtils.getSubject() directly (or nested in the
+        //call stack) will work properly.
     }
 
     @After

Added: shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroUnitTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroUnitTest.java?rev=1043165&view=auto
==============================================================================
--- shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroUnitTest.java (added)
+++ shiro/trunk/core/src/test/java/org/apache/shiro/test/ExampleShiroUnitTest.java Tue Dec  7 19:06:41 2010
@@ -0,0 +1,45 @@
+package org.apache.shiro.test;
+
+import org.apache.shiro.subject.Subject;
+import org.junit.After;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+
+/**
+ * Simple example test class showing how one may perform unit tests for code that requires Shiro APIs.
+ */
+public class ExampleShiroUnitTest extends AbstractShiroTest {
+
+    @Test
+    public void testSimple() {
+
+        //1.  Create a mock Subject instance for the test to run
+        //    (for example, as an authenticated Subject):
+        Subject subjectUnderTest = createNiceMock(Subject.class);
+        expect(subjectUnderTest.isAuthenticated()).andReturn(true);
+
+        //2. Bind the subject to the current thread:
+        setSubject(subjectUnderTest);
+
+        //perform test logic here.  Any call to
+        //SecurityUtils.getSubject() directly (or nested in the
+        //call stack) will work properly.
+    }
+
+    /**
+     * Method is optional - use it with the @After annotation if you want
+     * to guarantee that the Subject is removed after every test method.
+     * You'll just need to set up a new Subject instance for every test
+     * that requires the Subject to be present.
+     * tests
+     * that require the Subject to be present.
+     */
+    @After
+    public void tearDownSubject() {
+        //3. Unbind the subject from the current thread:
+        clearSubject();
+    }
+
+}