Search code examples
javaunit-testingtestingjunit

Complex Junit Testing case


I am trying to create a JUnit test for this class.

What is the best way to go about testing the different if-else statements.

The I tried some generic unit test cases.

public class ObjectClaimHistory {

    private List<ObjectCollaborationClaimHistory> objectClaimHistory = new ArrayList<>();

    public void checkClaim(ClaimRequest claimRequest, Set<Integer> outOfDateCommits, int collaborationId, Integer parentCollaborationId, ClaimConflicts conflicts) {
        Set<Integer> conflictingCollaborationClaims = new HashSet<>();
        Set<Integer> conflictingCollaborationBlocks = new HashSet<>();
        for (ObjectCollaborationClaimHistory collaborationClaimHistory: objectClaimHistory) {
            if (!collaborationClaimHistory.checkClaim(claimRequest, outOfDateCommits)) {
                conflictingCollaborationClaims.add(collaborationClaimHistory.getCollaborationId());
            }
            if (!collaborationClaimHistory.checkBlock(claimRequest, outOfDateCommits)) {
                conflictingCollaborationBlocks.add(collaborationClaimHistory.getCollaborationId());
            }
        }
        // After checking all histories create one commit conflict. Choose the closest collaboration.
        if (conflictingCollaborationClaims.contains(collaborationId)) {
            conflicts.addCommitConflict(claimRequest.getObjectId(), claimRequest.getClaim(), collaborationId);
        }
        else if (conflictingCollaborationBlocks.contains(collaborationId)) {
            conflicts.addCommitConflict(claimRequest.getObjectId(), claimRequest.getBlock(), collaborationId);
        }

Solution

  • If I understand your question correctly then ...

    You want to control the behaviour of the collaborationClaimHistory instances such that in some test scenarios it returns true and in others it returns false for these invocations:

    • collaborationClaimHistory.checkClaim(claimRequest, outOfDateCommits)
    • collaborationClaimHistory.checkBlock(claimRequest, outOfDateCommits)

    You want to assert that the commitConflicts added to the given ClaimConflicts are valid.

    Looking at the code I see this: for (ObjectCollaborationClaimHistory collaborationClaimHistory : objectClaimHistory) which makes me suspect that objectClaimHistory is somehow injected into or discovered by ObjectClaimHistory. For your test you must be able to control how that is populated such that it returns instances of CollaborationClaimHistory which you can tweak to meet your test scenarios.

    Here's an example which assumes that objectClaimHistory is injected when creating ObjectClaimHistory:

    @Test
    public void someTest() {
        CollaborationClaimHistory collaborationClaimHistory = Mockito.mock(CollaborationClaimHistory.class);
    
        ObjectClaimHistory sut = new ObjectClaimHistory(Lists.newArrayList(collaborationClaimHistory));
    
        ClaimRequest claimRequest = ...;
        Set<Integer> outOfDateCommits = ...;
        int collaborationId = ...;
        Integer parentCollaborationId = ...;
        ClaimConflicts conflicts = ...;
    
        // set up expectations on the collaborationClaimHistory instance which will be interrogated within the sut
        Mockito.when(collaborationClaimHistory.checkClaim(claimRequest, outOfDateCommits)).thenReturn(true);
    
        sut.checkClaim(claimRequest, outOfDateCommits, collaborationId, parentCollaborationId, conflicts);
    
        // assert that the response (which is implicit in th post invocation state of conflicts) is valid
        assertTrue(conflicts.hasCommitConflict(claimRequest.getObjectId(), claimRequest.getClaim(), collaborationId));
    
        // ... and repeat for other scenarios, multiple CollaborationClaimHistory instances etc
    }