Home test - srujanabala/springboot-couchbase GitHub Wiki

Welcome to the springboot-couchbase wiki!

package org.axp.springbootcouchbase.mvc.service;

import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.ArrayList; import java.util.List;

import org.axp.springbootcouchbase.mvc.model.Student; import org.axp.springbootcouchbase.mvc.repository.UserRepository; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult;

@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc(secure = false) @ActiveProfiles("playground") public class ViewControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private UserRepository userRepository;

@InjectMocks
private ViewController viewController;

List<Student> students = new ArrayList<>();

@Before
public void setup() {
	MockitoAnnotations.initMocks(this);
	Student student = new Student("101", "Sapthika", 2);
	students.add(student);
}

@Test
public void testFetchRecords() throws Exception {
	when(userRepository.findAll()).thenReturn(students);
	//userRepository.findAll().forEach(System.out::println);
	MvcResult result = this.mockMvc
							.perform(get("/get"))
							.andExpect(status().isOk())
							.andDo(print())
							.andReturn();
	System.out.println(result.getResponse());

}

@Test
public void testFetchByName() throws Exception {
	when(userRepository.findByName(Mockito.anyString())).thenReturn(students);
	String name ="Abhishek";
	MvcResult result = this.mockMvc
							.perform(get("/get/"+name))
							.andExpect(status().isOk())
							.andDo(print())
							.andReturn();
	System.out.println(result.getResponse());

}

@Test
public void testFetchByQuery() throws Exception {
	when(userRepository.findByTheQuery(Mockito.anyString())).thenReturn(students);
	String name ="Abhishek";
	MvcResult result = this.mockMvc
							.perform(get("/get/"+name))
							.andExpect(status().isOk())
							.andDo(print())
							.andReturn();
	System.out.println(result.getResponse());

}

}

⚠️ **GitHub.com Fallback** ⚠️