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:
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
Like the others say, you need a test runner.
Since you have a @SpringBootTest
, you need a @RunWith(SpringRunner.class)
.
If you use JUnit5, you use @ExtendWith(SpringExtension.class)
.
Seeing your code, aren't you missing the Test Runner ? It's a class with the Main function that you call the tests you want to run. You can find an example in the TutorialsPoint website: JUnit Executing Tests .
您可以尝试将 @RunWith(MockitoJUnitRunner.class) 注释添加到您的类中
if Gradle:
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.1')
testCompile group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '5.6.1'
// your Mock lib...
}
repositories { jcenter() }
plugins {
id 'java-library'
id 'war'// optional
id 'eclipse' // optional, for Eclipse project
}
test { useJUnitPlatform() }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.