Skip to content

Commit 89a8e0f

Browse files
authored
Merge pull request #11 from DimaSanKiev/testing-service
Add FlashCardServiceImplTest
2 parents f704040 + 08c1516 commit 89a8e0f

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

.idea/modules/flashy_test.iml

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,10 @@ dependencies {
2727
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
2828
compile 'com.h2database:h2'
2929
compile 'org.eclipse.mylyn.github:org.eclipse.egit.github.core:2.1.5'
30+
31+
testCompile 'org.springframework.boot:spring-boot-starter-test'
32+
testCompile 'org.springframework.security:spring-security-test'
33+
testCompile 'com.github.springtestdbunit:spring-test-dbunit:1.3.0'
34+
testCompile 'org.dbunit:dbunit:2.5.2'
3035
testCompile group: 'junit', name: 'junit', version: '4.11'
3136
}

src/main/java/com/teamtreehouse/flashy/domain/FlashCard.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class FlashCard {
1414
private String term;
1515
private String definition;
1616

17-
protected FlashCard() {
17+
public FlashCard() {
1818
id = null;
1919
}
2020

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.teamtreehouse.flashy.services;
2+
3+
import com.teamtreehouse.flashy.domain.FlashCard;
4+
import com.teamtreehouse.flashy.repositories.FlashCardRepository;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.mockito.InjectMocks;
8+
import org.mockito.Mock;
9+
import org.mockito.runners.MockitoJUnitRunner;
10+
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
import static org.hamcrest.Matchers.instanceOf;
15+
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertThat;
17+
import static org.mockito.Mockito.verify;
18+
import static org.mockito.Mockito.when;
19+
20+
@RunWith(MockitoJUnitRunner.class)
21+
public class FlashCardServiceImplTest {
22+
23+
@Mock
24+
private FlashCardRepository repository;
25+
26+
@InjectMocks
27+
private FlashCardService service = new FlashCardServiceImpl();
28+
29+
@Test
30+
public void getRandomFlashCards_ShouldReturnTwo() throws Exception {
31+
List<FlashCard> flashCards = Arrays.asList(
32+
new FlashCard(),
33+
new FlashCard()
34+
);
35+
36+
when(repository.findAll()).thenReturn(flashCards);
37+
38+
assertEquals("getRandomFlashCards should return two favorites", 2, service.getRandomFlashCards(2).size());
39+
verify(repository).findAll();
40+
}
41+
42+
@Test
43+
public void findById_ShouldReturnOne() throws Exception {
44+
when(repository.findOne(1L)).thenReturn(new FlashCard());
45+
assertThat(service.getFlashCardById(1L), instanceOf(FlashCard.class));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)