How to write unit tests for enitymanager?

So I'm learning Unit Testcases for SpringMVC and also pretty new to Spring-Boot in general.

I wanna write a unit test case for this mvc controller method that I have



public class college {


private EntityManager em;

@RequestMapping(value = "/request", method = RequestMethod.GET)

public List display() {

//List val = temp.findAll();

Query query = em.createNativeQuery("SELECT * FROM STUDENT");

List val = (List)query.getResultList();

return val;


I tried writing something like this -


public class MockTestCollegeController {


private EntityManager em;


private student stu;


private college col;


private MockMvc mockMvc;


public void setup() {


this.mockMvc = MockMvcBuilders.standaloneSetup(col).build();



public void dets() throws Exception

{ RequestBuilder req =MockMvcRequestBuilders.get("/college/request").accept(MediaType.APPLICATION_JSON);

MockHttpServletResponse result = mockMvc.perform(req).andReturn().getResponse();


//String expected...

Read More »

By: StackOverFlow - 6 days ago

Related Posts