I am using junit 4 to create tests for controller. Below is the TestController test class.
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(EmployeeController.class)
public class TestEmployeeController {
@Autowired
private MockMvc mvc;
@MockBean
private EmployeeService service;
@BeforeClass
public static void beforeTest() {
System.out.println("Starting test engine");
}
@AfterClass
public static void AfterTest() {
System.out.println("Terminating test engine");
}
@Test
public void testfindEmployeeById() {
System.out.println("here");
Employee emp = new Employee();
emp.setId(1);
given(service.findById(emp.getId())).willReturn(emp);
try {
mvc.perform( MockMvcRequestBuilders
.get("/employee/findById/{id}", emp.getId())
.accept(MediaType.APPLICATION_JSON))
.andReturn();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The issue ocurrs when i try to run the test from eclipse, i get the below error. i also renamed the method with a prefix test but still its not working.
I guess the issue might be because of webmvc annotation but not sure. In Eclipse console logger provides the below info.
DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.SpringHiber.testController.TestEmployeeController]
I also tried annotating @Profile in controller and @ActiveProfiles in testController method. But it didn't help.
Detail Info about the exception i am getting is below.
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testfindEmployeeById], {ExactMatcher:fDisplayName=testfindEmployeeById(com.SpringHiber.testController.TestEmployeeController)], {LeadingIdentifierMatcher:fClassName=com.SpringHiber.testController.TestEmployeeController,fLeadingIdentifier=testfindEmployeeById]] from org.junit.internal.requests.ClassRequest@2aceadd4
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:80)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:71)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:46)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Below i am posting the rest controller class.
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired EmployeeService eService;
@GetMapping(path= {"/findById/{id}"},produces= {MediaType.APPLICATION_JSON_VALUE})
public Employee findById(@PathVariable int id) {
return eService.findById(id);
}
}
Please let me know what am i missing in the above code?