0

I have tried to write test case for some model class that getting Stackoverflow error and i found it is problem on recursive call but i'm not sure is it case for that? Can any one help me to write test case recursive call. Can anyone help me on below sample code to how can i fix it?

Note: I'm getting StackOverflow error on getComponentsData() on assign data function.

class ComponentTest {

lateinit var classUnderTest: ComponentTest

@Before
fun setUp() {
    classUnderTest = ComponentTest()
}

@Test
fun `test to convert Component with one set Component`() {
    val component = getComponentsData()
    val result = classUnderTest.convert(component)
    testComponent(result)
}

private fun testComponent(result: ContentComponentBO?) {
    Assert.assertNotNull(result)
    Assert.assertEquals("taggingGroup",
                        result?.taggingGroup)
    Assert.assertEquals("componentType",
                        result?.componentType)
    testCollection(result?.collection?.first())
}

private fun getComponentsData(): Component {
    return Component().apply {
        taggingGroup = "taggingGroup"
        componentType = "componentType"
        collection = listOf(getCollectionData())
        name = "name"
        headerText = "headerText"
        pageHeader = "pageHeader"
        iroaButton = getCollectionData()
        isOpenInOverlay = false
        overlayHeader = "overlayHeader"
    }
}

private fun getCollectionData(): Collection {
    return Collection().apply {
        defaultGroup = false
        groupName = "groupName"
        collection = listOf(getCollectionData())
        isOpenInOverlay = true
        image2ShoppingCategory = "image2ShoppingCategory"
        components = listOf(getComponentsData())
        categoryName = "categoryName"
    }
}

}

1 Answer 1

1

Let's try to describe part of the call stack:

`test to convert Component with one set Component` calls  getComponentsData()

getComponentsData() calls getCollectionData() to set iroaButton

Here is the interesting part, you've two potential issues. I say potential because I've partial access to the code but it seems you've one issue when:

getCollectionData() calls getCollectionData() to set collection
then getCollectionData() calls getCollectionData() to set collection
then getCollectionData() calls getCollectionData() to set collection
then getCollectionData() calls getCollectionData() to set collection
then getCollectionData() calls getCollectionData() to set collection
💥

And then when

getCollectionData() calls getComponentsData() to set components  
getComponentsData() calls getCollectionData() to set iroaButton
then getCollectionData() calls getComponentsData() to set components  
then getComponentsData() calls getCollectionData() to set iroaButton
then getCollectionData() calls getComponentsData() to set components  
then getComponentsData() calls getCollectionData() to set iroaButton
💥

Your code has issues with recursion.

Recursion means "defining a problem in terms of itself".

In other words, we can distill the idea of recursion into two simple rules:

  1. Each recursive call should be on a smaller instance of the same problem, that is, a smaller subproblem.
  2. The recursive calls must eventually reach a base case, which is solved without further recursion.

The problem with your code is that there's no base case. The method getCollectionData() calls getCollectionData() and get stuck until the stacks gets overflowed because there's nothing that will make your code stop.

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

2 Comments

Thanks for your description Do we have any way to test recursive call/ recursive function?
Sure, one of the classical examples of teaching Test Driven Development is to implement a recursive factorial method. Example. The problem of your code is not you're testing something recursive, the problem is that the recursion is wrong because it never stops. As it is, your code will crash in your app as well.

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.