2

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?

2
  • 1
    I was able to run your tests in Intellij without any change. Here is a link to similar question - stackoverflow.com/questions/47710837/… Commented Jul 12, 2019 at 5:53
  • 1
    Thanks for your suggestion. The linked helped me a lot. @ContextConfiguration was missing. Commented Jul 12, 2019 at 6:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.