Move api androidTest/ tests to test/

It's useless to make an instrumented test if Context is only used to retrieve a resource file. It can be done with classLoader
This commit is contained in:
Shinokuni 2020-11-09 22:00:55 +01:00
parent 749145b305
commit a8e3a689d5
42 changed files with 125 additions and 134 deletions

View File

@ -48,7 +48,7 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.3.0' androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0' androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:4.9.0' testImplementation 'com.squareup.okhttp3:mockwebserver:4.9.0'
androidTestImplementation "org.koin:koin-test:2.1.6" androidTestImplementation "org.koin:koin-test:2.1.6"
testImplementation "org.koin:koin-test:2.1.6" testImplementation "org.koin:koin-test:2.1.6"

View File

@ -1,14 +1,9 @@
package com.readrops.api.utils package com.readrops.api.utils
import android.net.Uri
import com.readrops.api.services.Credentials import com.readrops.api.services.Credentials
import com.readrops.api.services.freshrss.FreshRSSService
import com.readrops.api.services.nextcloudnews.NextNewsService
import com.readrops.db.entities.account.Account
import com.readrops.db.entities.account.AccountType
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.Response import okhttp3.Response
import java.lang.IllegalArgumentException import java.net.URI
class AuthInterceptor(var credentials: Credentials? = null) : Interceptor { class AuthInterceptor(var credentials: Credentials? = null) : Interceptor {
@ -18,11 +13,11 @@ class AuthInterceptor(var credentials: Credentials? = null) : Interceptor {
if (credentials != null) { if (credentials != null) {
if (credentials!!.url != null) { if (credentials!!.url != null) {
val uri = Uri.parse(credentials!!.url) val uri = URI.create(credentials!!.url)
urlBuilder urlBuilder
.scheme(uri.scheme!!) .scheme(uri.scheme!!)
.host(uri.host!!) .host(uri.host!!)
.encodedPath(uri.encodedPath + chain.request().url.encodedPath) .encodedPath(uri.path + chain.request().url.encodedPath)
} }
if (credentials!!.authorization != null) { if (credentials!!.authorization != null) {

View File

@ -0,0 +1,9 @@
package com.readrops.api
import java.io.InputStream
object TestUtils {
fun loadResource(path: String): InputStream =
javaClass.classLoader?.getResourceAsStream(path)!!
}

View File

@ -1,10 +1,7 @@
package com.readrops.api.localfeed package com.readrops.api.localfeed
import android.accounts.NetworkErrorException import android.accounts.NetworkErrorException
import android.content.Context import com.readrops.api.TestUtils
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.icapps.niddler.interceptor.okhttp.NiddlerOkHttpInterceptor
import com.readrops.api.apiModule import com.readrops.api.apiModule
import com.readrops.api.utils.ApiUtils import com.readrops.api.utils.ApiUtils
import com.readrops.api.utils.AuthInterceptor import com.readrops.api.utils.AuthInterceptor
@ -21,8 +18,6 @@ import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module import org.koin.dsl.module
import org.koin.test.KoinTest import org.koin.test.KoinTest
import org.koin.test.KoinTestRule import org.koin.test.KoinTestRule
@ -31,10 +26,8 @@ import java.net.HttpURLConnection
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@RunWith(AndroidJUnit4::class)
class LocalRSSDataSourceTest : KoinTest { class LocalRSSDataSourceTest : KoinTest {
private val context by inject<Context>()
private lateinit var url: HttpUrl private lateinit var url: HttpUrl
private val mockServer: MockWebServer = MockWebServer() private val mockServer: MockWebServer = MockWebServer()
@ -42,7 +35,6 @@ class LocalRSSDataSourceTest : KoinTest {
@get:Rule @get:Rule
val koinTestRule = KoinTestRule.create { val koinTestRule = KoinTestRule.create {
androidContext(InstrumentationRegistry.getInstrumentation().context)
modules(apiModule, module { modules(apiModule, module {
single(override = true) { single(override = true) {
OkHttpClient.Builder() OkHttpClient.Builder()
@ -67,7 +59,7 @@ class LocalRSSDataSourceTest : KoinTest {
@Test @Test
fun successfulQueryTest() { fun successfulQueryTest() {
val stream = context.resources.assets.open("localfeed/rss_feed.xml") val stream = TestUtils.loadResource("localfeed/rss_feed.xml")
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK) mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.addHeader(ApiUtils.CONTENT_TYPE_HEADER, "application/xml; charset=UTF-8") .addHeader(ApiUtils.CONTENT_TYPE_HEADER, "application/xml; charset=UTF-8")
@ -91,7 +83,7 @@ class LocalRSSDataSourceTest : KoinTest {
@Test @Test
fun headersTest() { fun headersTest() {
val stream = context.resources.assets.open("localfeed/rss_feed.xml") val stream = TestUtils.loadResource("localfeed/rss_feed.xml")
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK) mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.addHeader("Content-Type", "application/rss+xml; charset=UTF-8") .addHeader("Content-Type", "application/rss+xml; charset=UTF-8")
@ -108,7 +100,7 @@ class LocalRSSDataSourceTest : KoinTest {
@Test @Test
fun jsonFeedTest() { fun jsonFeedTest() {
val stream = context.resources.assets.open("localfeed/json/json_feed.json") val stream = TestUtils.loadResource("localfeed/json/json_feed.json")
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK) mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.addHeader(ApiUtils.CONTENT_TYPE_HEADER, "application/feed+json") .addHeader(ApiUtils.CONTENT_TYPE_HEADER, "application/feed+json")
@ -122,7 +114,7 @@ class LocalRSSDataSourceTest : KoinTest {
@Test @Test
fun specialCasesAtomTest() { fun specialCasesAtomTest() {
val stream = context.resources.assets.open("localfeed/atom/atom_feed_no_url_siteurl.xml") val stream = TestUtils.loadResource("localfeed/atom/atom_feed_no_url_siteurl.xml")
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK) mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.addHeader(ApiUtils.CONTENT_TYPE_HEADER, "application/atom+xml") .addHeader(ApiUtils.CONTENT_TYPE_HEADER, "application/atom+xml")
@ -136,7 +128,7 @@ class LocalRSSDataSourceTest : KoinTest {
@Test @Test
fun specialCasesRSS1Test() { fun specialCasesRSS1Test() {
val stream = context.resources.assets.open("localfeed/rss1/rss1_feed_no_url_siteurl.xml") val stream = TestUtils.loadResource("localfeed/rss1/rss1_feed_no_url_siteurl.xml")
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK) mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.addHeader(ApiUtils.CONTENT_TYPE_HEADER, "application/rdf+xml") .addHeader(ApiUtils.CONTENT_TYPE_HEADER, "application/rdf+xml")

View File

@ -7,18 +7,23 @@ import com.readrops.api.localfeed.rss1.RSS1ItemsAdapter
import com.readrops.api.localfeed.rss2.RSS2FeedAdapter import com.readrops.api.localfeed.rss2.RSS2FeedAdapter
import com.readrops.api.localfeed.rss2.RSS2ItemsAdapter import com.readrops.api.localfeed.rss2.RSS2ItemsAdapter
import junit.framework.TestCase.assertTrue import junit.framework.TestCase.assertTrue
import org.junit.Assert import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.rules.ExpectedException
class XmlAdapterTest { class XmlAdapterTest {
@get:Rule
val expectedException: ExpectedException = ExpectedException.none()
@Test @Test
fun xmlFeedAdapterFactoryTest() { fun xmlFeedAdapterFactoryTest() {
assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.RSS_1) is RSS1FeedAdapter) assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.RSS_1) is RSS1FeedAdapter)
assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.RSS_2) is RSS2FeedAdapter) assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.RSS_2) is RSS2FeedAdapter)
assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.ATOM) is ATOMFeedAdapter) assertTrue(XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.ATOM) is ATOMFeedAdapter)
Assert.assertThrows(IllegalArgumentException::class.java) { XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.UNKNOWN) } expectedException.expect(IllegalArgumentException::class.java)
XmlAdapter.xmlFeedAdapterFactory(LocalRSSHelper.RSSType.UNKNOWN)
} }
@Test @Test
@ -27,6 +32,7 @@ class XmlAdapterTest {
assertTrue(XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.RSS_2) is RSS2ItemsAdapter) assertTrue(XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.RSS_2) is RSS2ItemsAdapter)
assertTrue(XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.ATOM) is ATOMItemsAdapter) assertTrue(XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.ATOM) is ATOMItemsAdapter)
Assert.assertThrows(IllegalArgumentException::class.java) { XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.UNKNOWN) } expectedException.expect(IllegalArgumentException::class.java)
XmlAdapter.xmlItemsAdapterFactory(LocalRSSHelper.RSSType.UNKNOWN)
} }
} }

View File

@ -1,22 +1,16 @@
package com.readrops.api.localfeed.atom package com.readrops.api.localfeed.atom
import android.content.Context import com.readrops.api.TestUtils
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import junit.framework.TestCase.assertEquals import junit.framework.TestCase.assertEquals
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ATOMFeedAdapterTest { class ATOMFeedAdapterTest {
private val context: Context = InstrumentationRegistry.getInstrumentation().context
private val adapter = ATOMFeedAdapter() private val adapter = ATOMFeedAdapter()
@Test @Test
fun normalCasesTest() { fun normalCasesTest() {
val stream = context.assets.open("localfeed/atom/atom_feed.xml") val stream = TestUtils.loadResource("localfeed/atom/atom_feed.xml")
val feed = adapter.fromXml(stream) val feed = adapter.fromXml(stream)

View File

@ -1,25 +1,24 @@
package com.readrops.api.localfeed.atom package com.readrops.api.localfeed.atom
import android.content.Context import com.readrops.api.TestUtils
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.readrops.api.utils.DateUtils import com.readrops.api.utils.DateUtils
import com.readrops.api.utils.exceptions.ParseException import com.readrops.api.utils.exceptions.ParseException
import junit.framework.TestCase.* import junit.framework.TestCase.assertEquals
import org.junit.Assert import junit.framework.TestCase.assertNotNull
import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.rules.ExpectedException
@RunWith(AndroidJUnit4::class)
class ATOMItemsAdapterTest { class ATOMItemsAdapterTest {
private val context: Context = InstrumentationRegistry.getInstrumentation().context
private val adapter = ATOMItemsAdapter() private val adapter = ATOMItemsAdapter()
@get:Rule
val expectedException: ExpectedException = ExpectedException.none()
@Test @Test
fun normalCasesTest() { fun normalCasesTest() {
val stream = context.resources.assets.open("localfeed/atom/atom_items.xml") val stream = TestUtils.loadResource("localfeed/atom/atom_items.xml")
val items = adapter.fromXml(stream) val items = adapter.fromXml(stream)
val item = items[0] val item = items[0]
@ -36,7 +35,7 @@ class ATOMItemsAdapterTest {
@Test @Test
fun noDateTest() { fun noDateTest() {
val stream = context.resources.assets.open("localfeed/atom/atom_items_no_date.xml") val stream = TestUtils.loadResource("localfeed/atom/atom_items_no_date.xml")
val item = adapter.fromXml(stream).first() val item = adapter.fromXml(stream).first()
assertNotNull(item.pubDate) assertNotNull(item.pubDate)
@ -44,18 +43,22 @@ class ATOMItemsAdapterTest {
@Test @Test
fun noTitleTest() { fun noTitleTest() {
val stream = context.resources.assets.open("localfeed/atom/atom_items_no_title.xml") val stream = TestUtils.loadResource("localfeed/atom/atom_items_no_title.xml")
val exception = Assert.assertThrows(ParseException::class.java) { adapter.fromXml(stream) } expectedException.expect(ParseException::class.java)
assertTrue(exception.message!!.contains("Item title is required")) expectedException.expectMessage("Item title is required")
adapter.fromXml(stream)
} }
@Test @Test
fun noLinkTest() { fun noLinkTest() {
val stream = context.resources.assets.open("localfeed/atom/atom_items_no_link.xml") val stream = TestUtils.loadResource("localfeed/atom/atom_items_no_link.xml")
val exception = Assert.assertThrows(ParseException::class.java) { adapter.fromXml(stream) } expectedException.expect(ParseException::class.java)
assertTrue(exception.message!!.contains("Item link is required")) expectedException.expectMessage("Item link is required")
adapter.fromXml(stream)
} }
} }

View File

@ -1,28 +1,22 @@
package com.readrops.api.localfeed.json package com.readrops.api.localfeed.json
import android.content.Context import com.readrops.api.TestUtils
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.readrops.db.entities.Feed import com.readrops.db.entities.Feed
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
import junit.framework.TestCase.assertEquals import junit.framework.TestCase.assertEquals
import okio.Buffer import okio.Buffer
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class JSONFeedAdapterTest { class JSONFeedAdapterTest {
private val context: Context = InstrumentationRegistry.getInstrumentation().context
private val adapter = Moshi.Builder() private val adapter = Moshi.Builder()
.add(JSONFeedAdapter()) .add(JSONFeedAdapter())
.build() .build()
.adapter<Feed>(Feed::class.java) .adapter(Feed::class.java)
@Test @Test
fun normalCasesTest() { fun normalCasesTest() {
val stream = context.assets.open("localfeed/json/json_feed.json") val stream = TestUtils.loadResource("localfeed/json/json_feed.json")
val feed = adapter.fromJson(Buffer().readFrom(stream))!! val feed = adapter.fromJson(Buffer().readFrom(stream))!!

View File

@ -1,33 +1,32 @@
package com.readrops.api.localfeed.json package com.readrops.api.localfeed.json
import android.content.Context import com.readrops.api.TestUtils
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.readrops.api.utils.DateUtils import com.readrops.api.utils.DateUtils
import com.readrops.api.utils.exceptions.ParseException import com.readrops.api.utils.exceptions.ParseException
import com.readrops.db.entities.Item import com.readrops.db.entities.Item
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
import com.squareup.moshi.Types import com.squareup.moshi.Types
import junit.framework.TestCase.* import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNotNull
import okio.Buffer import okio.Buffer
import org.junit.Assert import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.rules.ExpectedException
@RunWith(AndroidJUnit4::class)
class JSONItemsAdapterTest { class JSONItemsAdapterTest {
private val context: Context = InstrumentationRegistry.getInstrumentation().context
private val adapter = Moshi.Builder() private val adapter = Moshi.Builder()
.add(Types.newParameterizedType(List::class.java, Item::class.java), JSONItemsAdapter()) .add(Types.newParameterizedType(List::class.java, Item::class.java), JSONItemsAdapter())
.build() .build()
.adapter<List<Item>>(Types.newParameterizedType(List::class.java, Item::class.java)) .adapter<List<Item>>(Types.newParameterizedType(List::class.java, Item::class.java))
@get:Rule
val expectedException: ExpectedException = ExpectedException.none()
@Test @Test
fun normalCasesTest() { fun normalCasesTest() {
val stream = context.resources.assets.open("localfeed/json/json_feed.json") val stream = TestUtils.loadResource("localfeed/json/json_feed.json")
val items = adapter.fromJson(Buffer().readFrom(stream))!! val items = adapter.fromJson(Buffer().readFrom(stream))!!
val item = items.first() val item = items.first()
@ -43,7 +42,7 @@ class JSONItemsAdapterTest {
@Test @Test
fun otherCasesTest() { fun otherCasesTest() {
val stream = context.resources.assets.open("localfeed/json/json_items_other_cases.json") val stream = TestUtils.loadResource("localfeed/json/json_items_other_cases.json")
val item = adapter.fromJson(Buffer().readFrom(stream))!!.first() val item = adapter.fromJson(Buffer().readFrom(stream))!!.first()
@ -55,7 +54,7 @@ class JSONItemsAdapterTest {
@Test @Test
fun nullDateTest() { fun nullDateTest() {
val stream = context.resources.assets.open("localfeed/json/json_items_no_date.json") val stream = TestUtils.loadResource("localfeed/json/json_items_no_date.json")
val item = adapter.fromJson(Buffer().readFrom(stream))!!.first() val item = adapter.fromJson(Buffer().readFrom(stream))!!.first()
assertNotNull(item.pubDate) assertNotNull(item.pubDate)
@ -63,18 +62,22 @@ class JSONItemsAdapterTest {
@Test @Test
fun nullTitleTest() { fun nullTitleTest() {
val stream = context.resources.assets.open("localfeed/json/json_items_no_title.json") val stream = TestUtils.loadResource("localfeed/json/json_items_no_title.json")
val exception = Assert.assertThrows(ParseException::class.java) { adapter.fromJson(Buffer().readFrom(stream)) } expectedException.expect(ParseException::class.java)
assertTrue(exception.message!!.contains("Item title is required")) expectedException.expectMessage("Item title is required")
adapter.fromJson(Buffer().readFrom(stream))
} }
@Test @Test
fun nullLinkTest() { fun nullLinkTest() {
val stream = context.resources.assets.open("localfeed/json/json_items_no_link.json") val stream = TestUtils.loadResource("localfeed/json/json_items_no_link.json")
val exception = Assert.assertThrows(ParseException::class.java) { adapter.fromJson(Buffer().readFrom(stream)) } expectedException.expect(ParseException::class.java)
assertTrue(exception.message!!.contains("Item link is required")) expectedException.expectMessage("Item link is required")
adapter.fromJson(Buffer().readFrom(stream))
} }
} }

View File

@ -1,22 +1,16 @@
package com.readrops.api.localfeed.rss1 package com.readrops.api.localfeed.rss1
import android.content.Context import com.readrops.api.TestUtils
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import junit.framework.Assert.assertEquals import junit.framework.Assert.assertEquals
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class RSS1FeedAdapterTest { class RSS1FeedAdapterTest {
private val context: Context = InstrumentationRegistry.getInstrumentation().context
private val adapter = RSS1FeedAdapter() private val adapter = RSS1FeedAdapter()
@Test @Test
fun normalCaseTest() { fun normalCaseTest() {
val stream = context.resources.assets.open("localfeed/rss1/rss1_feed.xml") val stream = TestUtils.loadResource("localfeed/rss1/rss1_feed.xml")
val feed = adapter.fromXml(stream) val feed = adapter.fromXml(stream)

View File

@ -1,25 +1,24 @@
package com.readrops.api.localfeed.rss1 package com.readrops.api.localfeed.rss1
import android.content.Context import com.readrops.api.TestUtils
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.readrops.api.utils.DateUtils import com.readrops.api.utils.DateUtils
import com.readrops.api.utils.exceptions.ParseException import com.readrops.api.utils.exceptions.ParseException
import junit.framework.TestCase.* import junit.framework.TestCase.assertEquals
import org.junit.Assert import junit.framework.TestCase.assertNotNull
import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.rules.ExpectedException
@RunWith(AndroidJUnit4::class)
class RSS1ItemsAdapterTest { class RSS1ItemsAdapterTest {
private val context: Context = InstrumentationRegistry.getInstrumentation().context
private val adapter = RSS1ItemsAdapter() private val adapter = RSS1ItemsAdapter()
@get:Rule
val expectedException: ExpectedException = ExpectedException.none()
@Test @Test
fun normalCasesTest() { fun normalCasesTest() {
val stream = context.resources.assets.open("localfeed/rss1/rss1_feed.xml") val stream = TestUtils.loadResource("localfeed/rss1/rss1_feed.xml")
val items = adapter.fromXml(stream) val items = adapter.fromXml(stream)
val item = items.first() val item = items.first()
@ -38,7 +37,7 @@ class RSS1ItemsAdapterTest {
@Test @Test
fun specialCasesTest() { fun specialCasesTest() {
val stream = context.resources.assets.open("localfeed/rss1/rss1_items_special_cases.xml") val stream = TestUtils.loadResource("localfeed/rss1/rss1_items_special_cases.xml")
val item = adapter.fromXml(stream).first() val item = adapter.fromXml(stream).first()
@ -49,7 +48,7 @@ class RSS1ItemsAdapterTest {
@Test @Test
fun nullDateTest() { fun nullDateTest() {
val stream = context.resources.assets.open("localfeed/rss1/rss1_items_no_date.xml") val stream = TestUtils.loadResource("localfeed/rss1/rss1_items_no_date.xml")
val item = adapter.fromXml(stream).first() val item = adapter.fromXml(stream).first()
assertNotNull(item.pubDate) assertNotNull(item.pubDate)
@ -57,17 +56,21 @@ class RSS1ItemsAdapterTest {
@Test @Test
fun nullTitleTest() { fun nullTitleTest() {
val stream = context.resources.assets.open("localfeed/rss1/rss1_items_no_title.xml") val stream = TestUtils.loadResource("localfeed/rss1/rss1_items_no_title.xml")
val exception = Assert.assertThrows(ParseException::class.java) { adapter.fromXml(stream) } expectedException.expect(ParseException::class.java)
assertTrue(exception.message!!.contains("Item title is required")) expectedException.expectMessage("Item title is required")
adapter.fromXml(stream)
} }
@Test @Test
fun nullLinkTest() { fun nullLinkTest() {
val stream = context.resources.assets.open("localfeed/rss1/rss1_items_no_link.xml") val stream = TestUtils.loadResource("localfeed/rss1/rss1_items_no_link.xml")
val exception = Assert.assertThrows(ParseException::class.java) { adapter.fromXml(stream) } expectedException.expect(ParseException::class.java)
assertTrue(exception.message!!.contains("RSS1 link or about element is required")) expectedException.expectMessage("RSS1 link or about element is required")
adapter.fromXml(stream)
} }
} }

View File

@ -1,23 +1,18 @@
package com.readrops.api.localfeed.rss2 package com.readrops.api.localfeed.rss2
import android.content.Context import com.readrops.api.TestUtils
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.readrops.api.utils.exceptions.ParseException import com.readrops.api.utils.exceptions.ParseException
import junit.framework.TestCase.assertEquals import junit.framework.TestCase.assertEquals
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class RSS2FeedAdapterTest { class RSS2FeedAdapterTest {
private val context: Context = InstrumentationRegistry.getInstrumentation().context
private val adapter = RSS2FeedAdapter() private val adapter = RSS2FeedAdapter()
@Test @Test
fun normalCasesTest() { fun normalCasesTest() {
val stream = context.resources.assets.open("localfeed/rss2/rss_full_feed.xml") val stream = TestUtils.loadResource("localfeed/rss2/rss_full_feed.xml")
val feed = adapter.fromXml(stream) val feed = adapter.fromXml(stream)
@ -30,7 +25,7 @@ class RSS2FeedAdapterTest {
@Test(expected = ParseException::class) @Test(expected = ParseException::class)
fun nullTitleTest() { fun nullTitleTest() {
val stream = context.resources.assets.open("localfeed/rss2/rss_feed_special_cases.xml") val stream = TestUtils.loadResource("localfeed/rss2/rss_feed_special_cases.xml")
adapter.fromXml(stream) adapter.fromXml(stream)
} }
} }

View File

@ -1,25 +1,24 @@
package com.readrops.api.localfeed.rss2 package com.readrops.api.localfeed.rss2
import android.content.Context import com.readrops.api.TestUtils
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.readrops.api.utils.DateUtils import com.readrops.api.utils.DateUtils
import com.readrops.api.utils.exceptions.ParseException import com.readrops.api.utils.exceptions.ParseException
import junit.framework.TestCase.* import junit.framework.TestCase.assertEquals
import org.junit.Assert import junit.framework.TestCase.assertNotNull
import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.rules.ExpectedException
@RunWith(AndroidJUnit4::class)
class RSS2ItemsAdapterTest { class RSS2ItemsAdapterTest {
private val context: Context = InstrumentationRegistry.getInstrumentation().context
private val adapter = RSS2ItemsAdapter() private val adapter = RSS2ItemsAdapter()
@get:Rule
val expectedException: ExpectedException = ExpectedException.none()
@Test @Test
fun normalCasesTest() { fun normalCasesTest() {
val stream = context.resources.assets.open("localfeed/rss_feed.xml") val stream = TestUtils.loadResource("localfeed/rss_feed.xml")
val items = adapter.fromXml(stream) val items = adapter.fromXml(stream)
val item = items.first() val item = items.first()
@ -35,7 +34,7 @@ class RSS2ItemsAdapterTest {
@Test @Test
fun otherNamespacesTest() { fun otherNamespacesTest() {
val stream = context.resources.assets.open("localfeed/rss2/rss_items_other_namespaces.xml") val stream = TestUtils.loadResource("localfeed/rss2/rss_items_other_namespaces.xml")
val item = adapter.fromXml(stream).first() val item = adapter.fromXml(stream).first()
assertEquals(item.guid, "guid") assertEquals(item.guid, "guid")
@ -46,7 +45,7 @@ class RSS2ItemsAdapterTest {
@Test @Test
fun noDateTest() { fun noDateTest() {
val stream = context.resources.assets.open("localfeed/rss2/rss_items_no_date.xml") val stream = TestUtils.loadResource("localfeed/rss2/rss_items_no_date.xml")
val item = adapter.fromXml(stream).first() val item = adapter.fromXml(stream).first()
assertNotNull(item.pubDate) assertNotNull(item.pubDate)
@ -54,23 +53,27 @@ class RSS2ItemsAdapterTest {
@Test @Test
fun noTitleTest() { fun noTitleTest() {
val stream = context.resources.assets.open("localfeed/rss2/rss_items_no_title.xml") val stream = TestUtils.loadResource("localfeed/rss2/rss_items_no_title.xml")
val exception = Assert.assertThrows(ParseException::class.java) { adapter.fromXml(stream) } expectedException.expect(ParseException::class.java)
assertTrue(exception.message!!.contains("Item title is required")) expectedException.expectMessage("Item title is required")
adapter.fromXml(stream)
} }
@Test @Test
fun noLinkTest() { fun noLinkTest() {
val stream = context.resources.assets.open("localfeed/rss2/rss_items_no_link.xml") val stream = TestUtils.loadResource("localfeed/rss2/rss_items_no_link.xml")
val exception = Assert.assertThrows(ParseException::class.java) { adapter.fromXml(stream) } expectedException.expect(ParseException::class.java)
assertTrue(exception.message!!.contains("Item link is required")) expectedException.expectMessage("Item link is required")
adapter.fromXml(stream)
} }
@Test @Test
fun enclosureTest() { fun enclosureTest() {
val stream = context.resources.assets.open("localfeed/rss2/rss_items_enclosure.xml") val stream = TestUtils.loadResource("localfeed/rss2/rss_items_enclosure.xml")
val item = adapter.fromXml(stream).first() val item = adapter.fromXml(stream).first()
assertEquals(item.imageLink, "https://image1.jpg") assertEquals(item.imageLink, "https://image1.jpg")
@ -78,7 +81,7 @@ class RSS2ItemsAdapterTest {
@Test @Test
fun mediaContentTest() { fun mediaContentTest() {
val stream = context.resources.assets.open("localfeed/rss2/rss_items_media_content.xml") val stream = TestUtils.loadResource("localfeed/rss2/rss_items_media_content.xml")
val items = adapter.fromXml(stream) val items = adapter.fromXml(stream)
assertEquals(items.first().imageLink, "https://image1.jpg") assertEquals(items.first().imageLink, "https://image1.jpg")
@ -87,7 +90,7 @@ class RSS2ItemsAdapterTest {
@Test @Test
fun mediaGroupTest() { fun mediaGroupTest() {
val stream = context.resources.assets.open("localfeed/rss2/rss_items_media_group.xml") val stream = TestUtils.loadResource("localfeed/rss2/rss_items_media_group.xml")
val item = adapter.fromXml(stream).first() val item = adapter.fromXml(stream).first()
assertEquals(item.imageLink, "https://image1.jpg") assertEquals(item.imageLink, "https://image1.jpg")

View File

@ -67,7 +67,7 @@ class JsonReaderExtensionsTest {
reader.beginObject() reader.beginObject()
reader.nextName() reader.nextName()
assertEquals(reader.nextNullableString(), "value") assertEquals(reader.nextNonEmptyString(), "value")
reader.endObject() reader.endObject()
} }