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

@RequestMapping("/college")

@RestController

public class college {

@PersistenceContext

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 -

@SpringBootTest

public class MockTestCollegeController {

@Mock

private EntityManager em;

@Mock

private student stu;

@InjectMocks

private college col;

@Autowired

private MockMvc mockMvc;

@Before

public void setup() {

MockitoAnnotations.initMocks(this);

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

}

@Test

public void dets() throws Exception

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

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

//System.out.println(result.getResponse().getContentAsString());

//String expected...

Read More »

By: StackOverFlow - 6 days ago

Related Posts