我如何执行Junit测试?

问题描述 投票:0回答:2

我使用jhipster控制器的生成器通过以下命令生成了该测试的一部分:jhipster generate-controller NAME-CONTROLLER但是我不知道如何尝试编织测试,我拥有的代码是:

     * Test class for the NotificationResource REST controller.
 *
 * @see NotificationResource
 */
@SpringBootTest(classes = MyApp.class)
public class NotificationResourceIT {

    private MockMvc restMockMvc;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        NotificationResource notificationResource = new NotificationResource();
        restMockMvc = MockMvcBuilders
            .standaloneSetup(notificationResource)
            .build();
    }

    public static String asJsonString(final Object obj) {
        try {
            return new ObjectMapper().writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Test postNotification
     */
    @Test
    public void testPostNotification() throws Exception {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
        LocalDate date = LocalDate.parse("02.11.2019",formatter);
        restMockMvc.perform(post("/api/notification/post-notification")
                .contentType(MediaType.APPLICATION_JSON)
                .content(asJsonString(date))
                )
            .andExpect(status().isOk());
    }
}

我正在使用IDE Eclipse,然后选择文件并在“以Junit方式运行”中执行clic,这会给我下一条消息:

enter image description here

并且在控制台中显示下一个:

    java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createUnfilteredTest(JUnit5TestLoader.java:75)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:66)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:53)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.PreconditionViolationException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 7 more

我需要帮助才能尝试此测试,非常感谢您的帮助

java testing junit jhipster
2个回答
0
投票

看到您的代码,您是否错过了Test Runner?这是带有Main函数的类,您可以调用要运行的测试。您可以在TutorialsPoint网站上找到一个示例:JUnit Executing Tests


0
投票

您可以尝试在类中添加@RunWith(MockitoJUnitRunner.class)批注

© www.soinside.com 2019 - 2024. All rights reserved.