Skip to content

Commit daa0876

Browse files
committed
部分重构
1 parent 99c0c75 commit daa0876

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

chapter14/Spring Demo/rx-react/src/test/java/com/dockerx/rxreact/repository/GitHbubReposTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void getRepos() {
6565
@Test
6666
public void getCommitsInWeek() {
6767
String aWeekAgo = ZonedDateTime.now(ZoneOffset.UTC).minusWeeks(1).minusDays(1)
68-
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'00:00:00'Z'"));;
68+
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'00:00:00'Z'"));
6969
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
7070
mockServer.expect(requestTo(String.format(GitHbubRepos.COMMITS, user, repo, aWeekAgo)))
7171
.andRespond(withSuccess( /*此处请自行添加结果测试,或者找自己的仓库设置*/

chapter14/Spring Demo/rx-react/src/test/java/com/dockerx/rxreact/services/GitHubServiceImplTest.java

+38-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.dockerx.rxreact.domain.*;
44
import com.dockerx.rxreact.repository.GitHbubRepos;
5+
import io.reactivex.Flowable;
56
import io.reactivex.subscribers.TestSubscriber;
67
import org.junit.Before;
78
import org.junit.Test;
@@ -33,11 +34,13 @@ public class GitHubServiceImplTest {
3334
@Before
3435
public void setup() {
3536
initMocks(this);
36-
gitHubService=new GitHubServiceImpl(gitHbubRepos);
37-
when(gitHbubRepos.getRepos(anyString())).thenAnswer(m->getReposForTest());
37+
gitHubService = new GitHubServiceImpl(gitHbubRepos);
38+
when(gitHbubRepos.getRepos(anyString())).thenAnswer(m -> getReposForTest());
3839

3940
when(gitHbubRepos.getCommitsInWeek(anyString(), anyString())).thenAnswer(m -> getCommitsInWeekForTest());
40-
when(gitHbubRepos.getSingleCommit(anyString(), anyString(), anyString())).thenAnswer(m -> getSingleCommitForTest());
41+
when(gitHbubRepos.getSingleCommit(anyString(),
42+
anyString(),
43+
anyString())).thenAnswer(m -> getSingleCommitForTest());
4144
when(gitHbubRepos.getSingleCommitByUrl(anyString())).thenAnswer(m -> getSingleCommitForTest());
4245
}
4346

@@ -52,6 +55,7 @@ private List<Repository> getReposForTest() {
5255
new Repository("repo3", LocalDateTime.now().minusDays(3))
5356
);
5457
}
58+
5559
@Test
5660
public void getRepos() {
5761
TestSubscriber<String> testSubscriber = new TestSubscriber<>();
@@ -63,7 +67,7 @@ public void getRepos() {
6367
testSubscriber.assertNoErrors();
6468
assertThat(testSubscriber.values())
6569
.as("getRepos returns all user code repository ")
66-
.containsExactly("repo1 ","repo2 ", "repo3 ");
70+
.containsExactly("repo1 ", "repo2 ", "repo3 ");
6771
}
6872

6973
@Test
@@ -94,7 +98,6 @@ public void getCommitsInWeek() {
9498
}
9599

96100

97-
98101
@Test
99102
public void getCommittedFilesByUser() {
100103
TestSubscriber<CommittedFile> testSubscriber = new TestSubscriber<>();
@@ -117,13 +120,23 @@ private List<Commit> getCommitsInWeekForTest() {
117120
return Arrays.asList(
118121
Commit.builder()
119122
.sha("sha1")
120-
.committer(new Committer("test-user","https://github.com/muyinchen","https://api.github.com/users/muyinchen/orgs"))
121-
.author(new Author("url2","https://github.com/muyinchen",true,"https://api.github.com/users/muyinchen/orgs"))
123+
.committer(new Committer("test-user",
124+
"https://github.com/muyinchen",
125+
"https://api.github.com/users/muyinchen/orgs"))
126+
.author(new Author("url2",
127+
"https://github.com/muyinchen",
128+
true,
129+
"https://api.github.com/users/muyinchen/orgs"))
122130
.build(),
123131
Commit.builder()
124132
.sha("sha2")
125-
.committer(new Committer("no-test-user","https://github.com/muyinchen/no-test-user","https://api.github.com/users/muyinchen/orgs/no-test-user"))
126-
.author(new Author("url2","https://github.com/muyinchen/no-test-user",true,"https://api.github.com/users/muyinchen/orgs/no-test-user"))
133+
.committer(new Committer("no-test-user",
134+
"https://github.com/muyinchen/no-test-user",
135+
"https://api.github.com/users/muyinchen/orgs/no-test-user"))
136+
.author(new Author("url2",
137+
"https://github.com/muyinchen/no-test-user",
138+
true,
139+
"https://api.github.com/users/muyinchen/orgs/no-test-user"))
127140
.build()
128141
);
129142
}
@@ -140,4 +153,20 @@ private SingleCommit getSingleCommitForTest() {
140153
)
141154
);
142155
}
156+
157+
@Test
158+
public void testTestSubscriber() {
159+
160+
TestSubscriber<String> testSubscriber = new TestSubscriber<>();
161+
//In order to emit "1", "2", "3"
162+
Flowable.just("1", "2", "3").subscribe(testSubscriber);
163+
//Assert whether values are equal
164+
testSubscriber.assertValues("1", "2", "3");
165+
//Assert value does not exist
166+
testSubscriber.assertNever("4");
167+
//Is the number of asserted values equal?
168+
testSubscriber.assertValueCount(3);
169+
//Assertion terminated
170+
testSubscriber.assertTerminated();
171+
}
143172
}

0 commit comments

Comments
 (0)