【问题标题】:sling test case SERVER sidesling 测试用例 SERVER 端
【发布时间】:2023-03-09 22:17:01
【问题描述】:

我正在为服务器端编写 sling 单元测试用例,我的测试包是通过使用以下代码从客户端命中 junit servlet 来运行的。我的测试需要一个正在运行的FTP服务器,我想通过使用@before并通过@after或任何其他可能的最佳方式清理转储,如何使用调用的可运行测试类来做到这一点一个junit servlet。

@RunWith(SlingRemoteTestRunner.class)
    public class FTPImporterTest extends SlingTestBase implements SlingRemoteTestParameters, SlingTestsCountChecker {
        /**
         *
         */
        public static final String TEST_SELECTOR = "com.my.proj.FTPImporterTesting.FTPImporterServerTest";
        public static final int TESTS_AT_THIS_PATH = 3;
        /**
         *
         */

        public int getExpectedNumberOfTests() { 
            return TESTS_AT_THIS_PATH;
        }

        public String getJunitServletUrl() {
            return getServerBaseUrl() + "/system/sling/junit";
        }

        public String getTestClassesSelector() {
            return TEST_SELECTOR;
        }

        public String getTestMethodSelector() {
            return null;
        }

        public void checkNumberOfTests(int i) {
            Assert.assertEquals(TESTS_AT_THIS_PATH, i);
        }

    }

【问题讨论】:

    标签: unit-testing testing junit integration-testing sling


    【解决方案1】:

    一种可能的方法是使用Mockito framework

    这里是一个使用Mocktio in AEM的例子

    【讨论】:

      【解决方案2】:

      我建议对Sling Teleporter Junit Rule 使用较新的方法。它允许编写在客户端运行的测试,但内部 JUnit 语句在客户端被替换并在 sling 中传送,它使用 junit 核心框架运行。

      这是文档中的示例:

      public class BasicTeleporterTest {
      
      @Rule
      public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "Launchpad");
      
      @Test
      public void testConfigAdmin() throws IOException {
          final String pid = "TEST_" + getClass().getName() + UUID.randomUUID();
      
          final ConfigurationAdmin ca = teleporter.getService(ConfigurationAdmin.class);
          assertNotNull("Teleporter should provide a ConfigurationAdmin", ca);
      
          final Configuration cfg = ca.getConfiguration(pid);
          assertNotNull("Expecting to get a Configuration", cfg);
          assertEquals("Expecting the correct pid", pid, cfg.getPid());
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-05
        相关资源
        最近更新 更多