I am following the below article to test my contoller on its own without having to start a spring application completely. Please can you let me know where I am going wrong, is the article wrong?
https://www.javadevjournal.com/spring-boot/testing-in-spring-boot/
The error I get is as below
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
SearchController.java
@RestController
@RequestMapping(value = "/top5")
public class SearchController {
@Autowired
private UserService userService;
@RequestMapping(value = "/{user}", method = RequestMethod.GET)
public List<Repo> getInfo(@PathVariable(name = "user") String user) {
System.out.println("Querying for [" + user + "]");
return userService.getResults(user);
}
}
UserService.java
@Service
public class UserService {
private static final String search_url = "https://api.github.com/users/%s/repos?page=1&per_page=5";
@Autowired
RestTemplate rt;
public List<Repo> getResults(String user) {
System.out.println("Invoking: " + String.format(search_url, user));
Repo[] a = rt.getForEntity(String.format(search_url, user), Repo[].class).getBody();
return Arrays.asList(a);
}
}
SearchControllerTest.java
@RunWith(SpringRunner.class)
@WebMvcTest(value = SearchController.class)
public class SearchControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;
private static List<Repo> repoList;
@BeforeClass
public static void setupTestData() {
repoList = new ArrayList<Repo>();
// Populate with test data
repoList.add(new Repo("1", "Repo1", "Repository 1", "http://myurl.com/1", "Description 1"));
repoList.add(new Repo("2", "Repo2", "Repository 2", "http://myurl.com/2", "Description 2"));
repoList.add(new Repo("3", "Repo3", "Repository 3", "http://myurl.com/3", "Description 3"));
repoList.add(new Repo("4", "Repo4", "Repository 4", "http://myurl.com/4", "Description 4"));
repoList.add(new Repo("5", "Repo5", "Repository 5", "http://myurl.com/5", "Description 5"));
}
@Test
public void testGetInfo() throws Exception {
String apiUrl = "/top5/tester";
// Setup "Mockito" to mock userService call
Mockito.when(userService.getResults(Mockito.anyString())).thenReturn(repoList);
// Build a GET Request and send it to the test server
RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON);
MvcResult r = mockMvc.perform(rb).andReturn(); // throws Exception
// Validate response
String tr = r.getResponse().getContentAsString();
// System.out.println(tr);
String er = "[{\"id\":\"1\",\"name\":\"Repo1\",\"full_name\":\"Repository 1\",\"html_url\":\"http://myurl.com/1\",\"description\":\"Description 1\"},{\"id\":\"2\",\"name\":\"Repo2\",\"full_name\":\"Repository 2\",\"html_url\":\"http://myurl.com/2\",\"description\":\"Description 2\"},{\"id\":\"3\",\"name\":\"Repo3\",\"full_name\":\"Repository 3\",\"html_url\":\"http://myurl.com/3\",\"description\":\"Description 3\"},{\"id\":\"4\",\"name\":\"Repo4\",\"full_name\":\"Repository 4\",\"html_url\":\"http://myurl.com/4\",\"description\":\"Description 4\"},{\"id\":\"5\",\"name\":\"Repo5\",\"full_name\":\"Repository 5\",\"html_url\":\"http://myurl.com/5\",\"description\":\"Description 5\"}]";
JSONAssert.assertEquals(er, tr, true);
// Or we can use JUnit's assertEquals() method
// assertEquals("REST API Returned incorrect response.", er, tr);
}
}
Repo.class
public class Repo {
private String id;
private String name;
private String description;
private String fullName;
private String htmlUrl;
public Repo(String id, String name, String description, String fullName, String htmlUrl) {
this.id = id;
this.name = name;
this.description = description;
this.fullName = fullName;
this.htmlUrl = htmlUrl;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getFullName() {
return fullName;
}
public String getHtmlUrl() {
return htmlUrl;
}
}