Skip to content

Commit 7724c3a

Browse files
committed
tests, Maven tests config
1 parent 7402418 commit 7724c3a

3 files changed

Lines changed: 224 additions & 5 deletions

File tree

pom.xml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1213
<spring.version>4.1.6.RELEASE</spring.version>
1314
<spring.security.version>4.0.0.RELEASE</spring.security.version>
1415
</properties>
@@ -144,6 +145,13 @@
144145
<version>4.11</version>
145146
<scope>test</scope>
146147
</dependency>
148+
149+
<dependency>
150+
<groupId>org.mockito</groupId>
151+
<artifactId>mockito-core</artifactId>
152+
<version>1.10.19</version>
153+
<scope>test</scope>
154+
</dependency>
147155
</dependencies>
148156

149157
<build>
@@ -157,14 +165,23 @@
157165
<target>1.8</target>
158166
</configuration>
159167
</plugin>
168+
<!-- unit tests (*Test) -->
160169
<plugin>
161170
<artifactId>maven-surefire-plugin</artifactId>
162171
<version>2.18.1</version>
163-
<configuration>
164-
<includes>
165-
<include>**/*Tests.java</include>
166-
</includes>
167-
</configuration>
172+
</plugin>
173+
<!-- integration tests (*IT) -->
174+
<plugin>
175+
<artifactId>maven-failsafe-plugin</artifactId>
176+
<version>2.18.1</version>
177+
<executions>
178+
<execution>
179+
<goals>
180+
<goal>integration-test</goal>
181+
<goal>verify</goal>
182+
</goals>
183+
</execution>
184+
</executions>
168185
</plugin>
169186
</plugins>
170187
</build>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package alexp.blog;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.security.core.Authentication;
8+
import org.springframework.security.core.context.SecurityContext;
9+
import org.springframework.security.core.context.SecurityContextHolder;
10+
import org.springframework.security.web.FilterChainProxy;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.TestExecutionListeners;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
15+
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
16+
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
17+
import org.springframework.test.context.web.WebAppConfiguration;
18+
import org.springframework.test.web.servlet.MockMvc;
19+
import org.springframework.web.context.WebApplicationContext;
20+
21+
import javax.annotation.Resource;
22+
import javax.transaction.Transactional;
23+
24+
import static org.junit.Assert.assertNotNull;
25+
import static org.junit.Assert.assertNull;
26+
import static org.junit.Assert.assertThat;
27+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
30+
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
31+
32+
@RunWith(SpringJUnit4ClassRunner.class)
33+
@WebAppConfiguration
34+
@ContextConfiguration({ "file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml",
35+
"classpath:/database.xml",
36+
"file:src/main/webapp/WEB-INF/spring-security.xml" })
37+
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
38+
DirtiesContextTestExecutionListener.class,
39+
TransactionalTestExecutionListener.class })
40+
@Transactional
41+
public class AppIT {
42+
43+
@SuppressWarnings("SpringJavaAutowiringInspection")
44+
@Resource
45+
private FilterChainProxy springSecurityFilterChain;
46+
47+
private MockMvc mockMvc;
48+
49+
@SuppressWarnings("SpringJavaAutowiringInspection")
50+
@Autowired
51+
protected WebApplicationContext wac;
52+
53+
@Before
54+
public void setup() {
55+
this.mockMvc = webAppContextSetup(this.wac).addFilter(springSecurityFilterChain).build();
56+
}
57+
58+
@Test
59+
public void simple() throws Exception {
60+
SecurityContext securityContext = SecurityContextHolder.getContext();
61+
62+
Authentication auth = securityContext.getAuthentication();
63+
64+
assertNull(auth);
65+
66+
mockMvc.perform(get("/"))
67+
.andExpect(status().isOk())
68+
.andExpect(view().name("posts"));
69+
}
70+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package alexp.blog.service;
2+
3+
import alexp.blog.model.Post;
4+
import alexp.blog.model.Tag;
5+
import alexp.blog.repository.PostRepository;
6+
import alexp.blog.repository.TagRepository;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.mockito.*;
10+
import org.springframework.data.domain.Page;
11+
import org.springframework.data.domain.PageImpl;
12+
import org.springframework.data.domain.PageRequest;
13+
import org.springframework.data.domain.Sort;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.hamcrest.MatcherAssert.assertThat;
20+
import static org.mockito.Matchers.isNotNull;
21+
import static org.mockito.Matchers.refEq;
22+
import static org.mockito.Mockito.times;
23+
import static org.mockito.Mockito.verify;
24+
import static org.mockito.Mockito.when;
25+
26+
public class PostServiceTest {
27+
28+
@Mock
29+
private PostRepository postRepository;
30+
31+
@Mock
32+
private TagRepository tagRepository;
33+
34+
@InjectMocks
35+
private PostServiceImpl postService;
36+
37+
@Before
38+
public void setUp() throws Exception {
39+
MockitoAnnotations.initMocks(this);
40+
}
41+
42+
@Test
43+
public void shouldGetPostPage() {
44+
final int pageSize = 10;
45+
46+
List<Post> posts = new ArrayList<>();
47+
for (int i = 0; i < 15; i++) {
48+
Post post = new Post();
49+
post.setId((long) i);
50+
posts.add(post);
51+
}
52+
53+
when(postRepository.findAll(Matchers.any(PageRequest.class)))
54+
.thenReturn(new PageImpl<>(new ArrayList<>()));
55+
56+
when(postRepository.findAll(Matchers.eq(new PageRequest(0, pageSize, Sort.Direction.DESC, "dateTime"))))
57+
.thenReturn(new PageImpl<>(posts.subList(0, 10)));
58+
59+
when(postRepository.findAll(new PageRequest(1, pageSize, Sort.Direction.DESC, "dateTime")))
60+
.thenReturn(new PageImpl<>(posts.subList(10, 15)));
61+
62+
Page<Post> page1 = postService.getPostsPage(0, pageSize);
63+
Page<Post> page2 = postService.getPostsPage(1, pageSize);
64+
65+
assertThat(page1.getNumberOfElements(), is(equalTo(10)));
66+
assertThat(page2.getNumberOfElements(), is(equalTo(5)));
67+
68+
verify(postRepository, times(2)).findAll(Matchers.any(PageRequest.class));
69+
}
70+
71+
@Test
72+
public void shouldGetEmptyPostPage() {
73+
when(postRepository.findAll(Matchers.any(PageRequest.class)))
74+
.thenReturn(new PageImpl<>(new ArrayList<>()));
75+
76+
Page<Post> page = postService.getPostsPage(99, 10);
77+
78+
assertThat(page.getNumberOfElements(), is(equalTo(0)));
79+
80+
verify(postRepository, times(1)).findAll(Matchers.any(PageRequest.class));
81+
}
82+
83+
@Test
84+
public void shouldGetPost() {
85+
final long postId = 1L;
86+
87+
Post post = new Post();
88+
post.setId(postId);
89+
90+
when(postRepository.findOne(postId))
91+
.thenReturn(post);
92+
93+
Post retrievedPost = postService.getPost(postId);
94+
95+
assertThat(retrievedPost, is(equalTo(post)));
96+
97+
verify(postRepository, times(1)).findOne(postId);
98+
}
99+
100+
@Test
101+
public void shouldReturnNullWhenPostNotExists() {
102+
final long postId = 1L;
103+
104+
Post retrievedPost = postService.getPost(postId);
105+
106+
assertThat(retrievedPost, is(equalTo(null)));
107+
108+
verify(postRepository, times(1)).findOne(postId);
109+
}
110+
111+
@Test
112+
public void shouldAddNewPostAndSetTags() {
113+
Post post = new Post();
114+
post.setFullPostText("short text " + Post.shortPartSeparator() + " full text");
115+
116+
String tags = "c++, hello world";
117+
118+
postService.saveNewPost(post, tags);
119+
120+
assertThat(post.getShortTextPart(), containsString("short text"));
121+
assertThat(post.getShortTextPart(), not(containsString("full text")));
122+
123+
assertThat(post.getFullPostText(), allOf(containsString("full text"), containsString("full text"), containsString(Post.shortPartSeparator())));
124+
125+
assertThat(post.getTags().size(), is(equalTo(2)));
126+
127+
verify(tagRepository, times(2)).findByNameIgnoreCase(Matchers.anyString());
128+
verify(tagRepository, times(2)).save(Matchers.any(Tag.class));
129+
130+
verify(postRepository, times(1)).save(Matchers.any(Post.class));
131+
}
132+
}

0 commit comments

Comments
 (0)