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"
}
}
}
CodePudding user response:
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