tuliomagalhaes / SweetFactory

SweetFactory is a library to help you instantiate classes in a modular project.
Apache License 2.0
10 stars 3 forks source link

How to test? #4

Closed pedrofsn closed 4 years ago

pedrofsn commented 4 years ago

I had a router-library-module used with an object class who extends Routable class. Routable class has an property className to handle a string with entire packagename and activity name (like "abc.abc.abac.XActivity") and a lazy property called activity with Class.forName(className) declaration. Of course, in the end, a function to handle "open acivity".

So the problem here is manutenability and your sweet library solve that. Thank you!

When I used the old strategy, I tested if activity can be created based on classname. So I simple testing using Assert.assertNotNull(activity).

My questio is, how can I test now?

P.S.: I'm not good guy with tests, I started to study this right now.

pedrofsn commented 4 years ago

I'm trying

lateinit var context: Context

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this);
        context = mock()
    }

    @Test
    fun `RouterDepositsMenu was found`() {
        val factory = SweetFactory.newInstanceOf(FeatureDepositFactory::class)
        val intent = factory?.newIntent(context)
        val intentClass= intent?.component?.className
        val rightClass = DepositsMenuActivity::class.java.name
        Assert.assertEquals(rightClass, intentClass)
    }

I'm getting

Method getComponent in android.content.Intent not mocked.

But getComponent can't be mocked, it have to be generated right?

tuliomagalhaes commented 4 years ago

As you are using the Android Library in Unit Tests, it is not possible to test it because Android gives you a simple stub classes to be used only for Mock purpose. You have two choices: use Instrumented test or Robolectric test.

matheusribeirolima commented 4 years ago

Hi @pedrofsn , in this case, when you want to test intent calls, an approach is to use instrumented test. Here is a guide with Espresso: https://developer.android.com/training/testing/espresso/intents

pedrofsn commented 4 years ago

Thanks @matheusribeirolima!