Alpha - parent with beta as child
public class Alpha {
Beta beta;
public Alpha(int argument) {}
void start() {
beta = createBeta();
}
Beta createBeta() {
return new Beta(this);
}
}
Beta - child of alpha, has charlie
public class Beta {
Alpha alpha;
Charlie charlie;
public Beta(Alpha alpha) {
this.alpha = alpha;
this.charlie = createCharlie();
}
Charlie createCharlie() {
return new Charlie().shuffle();
}
}
Charlie - has a list, which is usually shuffled in production
public class Charlie {
List<Integer> list = new ArrayList<Integer>();
public Charlie() {
for (int i = 0; i < 6; i++) {
list.add(i);
}
}
public Charlie shuffle() {
Collections.shuffle(list);
return this;
}
@Override
public String toString() {
return list.toString();
}
}
AlphaTest [need help with this test] - want to try different variations of shuffling to see how alpha/beta would react.
public class AlphaTest {
Charlie special = new Charlie();
@Test
public void testSpecialCharlie() {
Alpha alpha = Mockito.spy(new Alpha(0));
Beta beta = Mockito.spy(alpha.createBeta());
Mockito.when(alpha.createBeta()).thenReturn(beta);
Mockito.when(beta.createCharlie()).thenReturn(special);
alpha.start();
// FAILURE: expected:<[0, 1, 2, 3, 4, 5]> but was:<[0, 4, 1, 5, 3, 2]>
assertEquals(special.list, alpha.beta.charlie.list);
}
}
Goal is to test Alpha/Beta with different combinations of Charlie. Not sure what's the best way to do that? This is exact code copied. Open to changing the setup also to facilitate testing. Tried different variations, nothing really working. Would really appreciate help with this.
I am not sure, I tried a bunch of ways (mocking out createCharlie()
function, mocking out Charlie
class, mocking out shuffle()
, moving createCharlie()
to parent Alpha
class, nothing really works properly or maybe I was missing something. Any help would be really appreciated. Thanks!
Wonder why I can't just do this:
Charlie charlie = Mockito.mock(Charlie.class);
Mockito.when(charlie.shuffle()).thenReturn(special);