Skip to content
Permalink
Browse files

Added Controller tests

  • Loading branch information...
borabilgin committed Jan 28, 2019
1 parent f8b193d commit ed8a5057233ee264eba6d2eee8cbba91b442cea5
@@ -0,0 +1,52 @@
package com.demo.imagebrowser.controller;

import com.demo.imagebrowser.domain.FeedCategory;
import com.demo.imagebrowser.service.FeedService;
import com.demo.imagebrowser.service.impl.UserDetailsService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@WebMvcTest(FeedCategoryController.class)
public class FeedCategoryControllerTest {

@Autowired
private MockMvc mvc;

@MockBean
private FeedService feedService;

@MockBean
private UserDetailsService userDetailsService;

@Test
public void shouldReturnCategory_whenAddingNewCategory() throws Exception {
String categoryName = "test";
FeedCategory category = new FeedCategory(categoryName);
given(feedService.addCategory(categoryName)).willReturn(category);

mvc.perform(put("/secure/category", categoryName)
.contentType(MediaType.APPLICATION_JSON)
.param("categoryName", categoryName))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value(category.getName()));
}

@Test
public void shouldReturnError_whenAddingNewCategoryWithNoNameProvided() throws Exception {
mvc.perform(put("/secure/category")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is4xxClientError());
}
}
@@ -0,0 +1,53 @@
package com.demo.imagebrowser.controller;

import com.demo.imagebrowser.domain.Feed;
import com.demo.imagebrowser.domain.FeedCategory;
import com.demo.imagebrowser.service.FeedService;
import com.demo.imagebrowser.service.impl.UserDetailsService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Arrays;
import java.util.List;

import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@RunWith(SpringRunner.class)
@WebMvcTest(FeedController.class)
public class FeedControllerTest {

@Autowired
private MockMvc mvc;

@MockBean
private FeedService feedService;

@MockBean
private UserDetailsService userDetailsService;

@Test
public void shouldReturnJSON_whenRequestedAllFeeds() throws Exception {
FeedCategory category = new FeedCategory("test");
List<Feed> feeds = Arrays.asList(new Feed("test", "test", category));

given(feedService.getAll()).willReturn(feeds);

mvc.perform(get("/feed")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].name").value(feeds.get(0).getName()));

}
}

0 comments on commit ed8a505

Please sign in to comment.
You can’t perform that action at this time.