Issue
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"
}
}
}
Solution
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:
- Each recursive call should be on a smaller instance of the same problem, that is, a smaller subproblem.
- 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.
Answered By - RobertoAllende
Answer Checked By - Robin (JavaFixing Admin)