0

Can someone explain me why I cannot create empty secondary construcor in my class? I wanna TEST it but I need to create a instance of class to use the methods from, but my class need a parametr to create it. I thought to create a scecondary constructor but when I'm trying it makes a error "There's a cycle in the delegation calls chain". Excatly I wanna use it on this @TEST below but when I'm trying to create instance of Adapter class I must put there also (FragmentManager) inside. Any ideas?

class Adapter(sFM: FragmentManager) : FragmentPagerAdapter(sFM, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    constructor() : this()

    private val pFragmentList = ArrayList<Fragment>()
    private val pFragmentTitle = ArrayList<String>()

    override fun getCount(): Int = pFragmentList.size

    override fun getItem(position: Int): Fragment = pFragmentList[position]

    override fun getPageTitle(position: Int): CharSequence = pFragmentTitle[position]

    fun addFragment(fm: Fragment, title: String) {
        pFragmentList.add(fm)
        pFragmentTitle.add(title)
    }
}

@Test
fun `create instance of class Adapter`() {
        var adapter = Adapter().addFragment()
}

1 Answer 1

3

There is no FragmentPagerAdapter with empty constructor. Basically, what your code is trying to compile, is to do constructor that calls itself. If you want to use base class constructor you need to use super instead of this. But still, you won't find such constructor in base class. You always have to pass some FragmentManager

Sign up to request clarification or add additional context in comments.

2 Comments

So right now how can I call to Adapter class from my @Test function ? I wanna test function "addFragment" for example. How can I do it ?
If you really want to do UnitTests of adapter - just mock FragmentManager, for example with the mockk library. The proper way would be to do it as instrumented tests (on emulator) or using robolectric library

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.