Git Product home page Git Product logo

coffeshopapplication's Introduction

CoffeShopApplication

android kotlin project + architecture components + testing

Description

sample MVVM project where i used stateflow instead of livedata and unit tested it with striKt. used hilt DI help me to test ui components such as fragments, activity, recycler view and several views with espresso and mockitto. used retrofit as REST API client and gson to get images from unsplash api and show them by glide. store data by room and unit tested.

Installing

  • CLONE the repo
  • create gradle.properties file in the project folder and add those lines:

API_KEY = "[get api key(https://unsplash.com/developers)]"

android.useAndroidX=true

android.enableJetifier=true

CAN you show me some code? OK :

@Test
fun insertShoppingItem() = runBlockingTest {


    val shoppingItem = ShoppingItem("name", 1, 1f, "url", id = 1)
    dao.insertShoppingItem(shoppingItem)


    val allShoppingItems = dao.observeAllShoppingItems().first()


   /// assertThat(allShoppingItems).contains(shoppingItem)


    expect {
        that(allShoppingItems){
            contains(shoppingItem)
        }
    }


}

@Module @InstallIn(ApplicationComponent::class) object TestAppModule {

@Provides
@Named("test_db")
fun provideInMemoryDb(@ApplicationContext context: Context) =
    Room.inMemoryDatabaseBuilder(context, ShoppingItemDatabase::class.java)
        .allowMainThreadQueries()
        .build()

}

from ImagePickFragmentTest.class :

@Test
fun clickImage_popBackStackAndSetImageUrl() = runBlockingTest{
    val navController = mock(NavController::class.java)
    val imageUrl = "TEST"
    val testViewModel = ShoppingViewModel(FakeShoppingRepositoryAndroidTest())
    launchFragmentInHiltContainer<ImagePickFragment>(fragmentFactory = fragmentFactory) {
        Navigation.setViewNavController(requireView(), navController)
        imageAdapter.images = listOf(imageUrl)
        viewModel = testViewModel
    }


    onView(withId(R.id.rvImages)).perform(
        RecyclerViewActions.actionOnItemAtPosition<ImageAdapter.ImageViewHolder>(
            0,
            click()
        )
    )


    verify(navController).popBackStack()
  //  assertThat(testViewModel.curImageUrl.getOrAwaitValue()).isEqualTo(imageUrl)


    expectThat(testViewModel.curImageUrl.first()){
        isEqualTo(imageUrl)
    }
}

Author

@bilaltaghda in twitter

@bilaltaghda in linkedin

@bilaltaghda5 in facebook

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.