dimanche 8 décembre 2019

How can I execute a Junit test?

I generated part of this test with the generator of controllers of jhipster, with the command: jhipster generate-controller NAME-CONTROLLER but I don't know how can I try a jnit test, the code that I have is:

     * 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());
    }
}

I'am using IDE Eclipse, and I select the file and do clic in Run as Junit, and this give me the next message:

enter image description here

and in console appear the next:

    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

I need help for try this test, I will apreciate much your help

Aucun commentaire:

Enregistrer un commentaire