Created
March 1, 2019 13:47
-
-
Save aykuttasil/7c85a90aed4c0cefe92cc66889f40401 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(JUnit4::class) | |
class BlogRepositoryUTest { | |
lateinit var blogRepository : BlogRepository | |
lateinit var mockServer : MockWebServer | |
lateinit var blogService : BlogService | |
@Before @Throws fun setUp() { | |
// Initialize mock webserver | |
mockServer = MockWebServer() | |
// Start the local server | |
mockServer.start() | |
// Get an okhttp client | |
val okHttpClient = OkHttpClient.Builder() | |
.build() | |
// Get an instance of Retrofit | |
val retrofit = Retrofit.Builder() | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | |
.baseUrl("https://api.test.com") | |
.addConverterFactory(GsonConverterFactory.create()) | |
.client(okHttpClient) | |
.build() | |
// Get an instance of blogService | |
blogService = retrofit.create(BlogService::class.java) | |
// Initialized repository | |
blogRepository = BlogRepository(blogService) | |
} | |
@After @Throws fun tearDown() { | |
// We're done with tests, shut it down | |
mockServer.shutdown() | |
} | |
@Test fun testBlogsReturnsListOfBlogs() { | |
val testObserver = TestObserver<List<Blog>>() | |
val path = "/blogs" | |
// Mock a response with status 200 and sample JSON output | |
val mockReponse = MockReponse() | |
.setResponseCode(200) | |
.setBody(getJson("json/blog/blogs.json")) | |
// Enqueue request | |
mockServer.enqueue(mockResponse) | |
// Call the API | |
blogRepository.blogs().subscribe(testObserver) | |
testObserver.awaitTerminalEvent(2, TimeUnit.SECONDS) | |
// No errors | |
testObserver.assertNoErrors() | |
// One list emitted | |
testObserver.assertValueCount(1) | |
// Get the request that was just made | |
val request = mockServer.takeRequest() | |
// Make sure we made the request to the required path | |
assertEquals(path, request.path) | |
} | |
// Network ağ durumu testi | |
@Test fun testBlogsReturnsError() { | |
val testObserver = TestObserver<List<Blog>>() | |
val path = "/blogs" | |
// Mock a response with status 200 and sample JSON output | |
val mockReponse = MockReponse() | |
.setResponseCode(500) // Simulate a 500 HTTP Code | |
// Enqueue request | |
mockServer.enqueue(mockResponse) | |
// Call the API | |
blogRepository.blogs().subscribe(testObserver) | |
testObserver.awaitTerminalEvent(2, TimeUnit.SECONDS) | |
// No values | |
testObserver.assertNoValues() | |
// One error recorded | |
assertEquals(1, testObserver.errorCount()) | |
// Get the request that was just made | |
val request = mockServer.takeRequest() | |
// Make sure we made the request to the required path | |
assertEquals(path, request.path) | |
} | |
// SocketTimeoutException | |
@Test fun testBlogsReturnsError1() { | |
val testObserver = TestObserver<List<Blog>>() | |
val path = "/blogs" | |
// Mock a response with status 200 and sample JSON output | |
val mockReponse = MockReponse() | |
.setResponseCode(200) | |
.throttleBody(1024, 1, TimeUnit.SECONDS) // Simulate SocketTimeout | |
.setBody(getJson("json/blog/blogs.json")) | |
// Enqueue request | |
mockServer.enqueue(mockResponse) | |
// Call the API | |
blogRepository.blogs().subscribe(testObserver) | |
testObserver.awaitTerminalEvent(2, TimeUnit.SECONDS) | |
// No values | |
testObserver.assertNoValues() | |
// One error recorded | |
assertEquals(1, testObserver.errorCount()) | |
// Get the request that was just made | |
val request = mockServer.takeRequest() | |
// Make sure we made the request to the required path | |
assertEquals(path, request.path) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment