I have written a method using Java streams which simply iterates over list of objects and returns true/false is certain condition is satisfied
Java Method:
boolean method(SampleObj sampleObj) {
List testList = invokeSomeMethod();
int result = testList
.parallelStream()
.filter(listObj -> (listObj.getAttr() = 1))
.count(listObj -> listObj.isAttr4());
return (result > 10);
}
I have written a Mock test case for the same as well. When I execute the test case, the test succeeds, however I get project custom error stating that all threads created were not shutdown.
I even tried using stream with try-with-resources with that noo did not help.
Mock Test:
@Test
public void testSomeMethod() {
SampleObj sampleObj1 = new SampleObj(10, 20, 30, true);
SampleObj sampleObj2 = new SampleObj(10, 20, 30, true);
SampleObj sampleObj3 = new SampleObj(10, 20, 30, false);
SampleObj sampleObjTest = new SampleObj(10, 20, 30, true);
List<SampleObj> testList = new ArrayList<SampleObj>();
testList.add(sampleObj1);
testList.add(sampleObj2);
testList.add(sampleObj3);
when(mockedAttribute.invokeSomeMethod()).thenReturn(nodeList);
ClassToBeTested classTest = createGenericMockRules();
Assert.assertTrue(classTest.method(sampleObjTest));
}
P.S. I have debugged to confirm that when invokeSomeMethod() is called, my mocked testList is returned.
As far as I know, Java streams internally closes the threads it creates. Am I implementing this incorrectly?