1. Atlassian Tutorials
  2. Untitled project
  3. Confluence unittesting plugin

Commits

Vincent Choy [Atlassian] x  committed 22c54a8

Added second tests with refactored setup test fixtures

  • Participants
  • Parent commits 8550719
  • Branches master

Comments (0)

Files changed (1)

File src/test/java/com/atlassian/tutorial/confluence/plugin/unittesting/ExampleMacroTest.java

View file
  • Ignore whitespace
 import com.atlassian.confluence.pages.Page;
 import com.atlassian.confluence.pages.PageManager;
 import com.atlassian.confluence.spaces.SpaceManager;
+import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
+import com.atlassian.user.impl.DefaultUser;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.MockitoAnnotations.Mock;
 
 import junit.framework.TestCase;
 
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 /**
  * Testing {@link com.atlassian.tutorial.confluence.plugin.unittesting.ExampleMacro}
     @Mock
     private ConversionContext conversionContext;
 
+    private ExampleMacro exampleMacro;
+
+    @Before
+    public void setUp() throws Exception
+    {
+        super.setUp();
+        exampleMacro = new ExampleMacro(pageManager, spaceManager);
+    }
+
     @Test
     public void testOutputIncludesRecentPages() throws Exception
     {
         // set up stub method to return our test page
         when(pageManager.getRecentlyAddedPages(55, "DS")).thenReturn(Arrays.asList(page));
 
-        // create the macro
-        ExampleMacro exampleMacro = new ExampleMacro(pageManager, spaceManager);
-
         // verify that the output contains the page title
         String output = exampleMacro.execute(new HashMap(), "", conversionContext);
         assertTrue("Output should contain page title but was: " + output,
                 output.contains(page.getTitle()));
     }
+
+    @Test
+    public void testOutputIncludesCurrentUser() throws Exception
+    {
+        // create test user
+        DefaultUser user = new DefaultUser("test");
+        user.setFullName("Test User");
+
+        // set current user to test user
+        AuthenticatedUserThreadLocal.setUser(user);
+        try
+        {
+            // verify that the output contains the current user
+            String output = exampleMacro.execute(new HashMap(), "", conversionContext);
+            assertTrue("Output should contain current user but was: " + output,
+                    output.contains(user.getFullName()));
+        }
+        finally
+        {
+            // reset current user
+            AuthenticatedUserThreadLocal.setUser(null);
+        }
+    }
 }