mercredi 15 mai 2019

How to check test cases if no verification possible

I'm learning about unit testing and I came across the problem to create a final check on wether a test case is correct or not. Usually I try to create a verification, like through an assertEquals(). But what is recommended to do when it's not possible to test it like this?

I have a class like this:

public class Landlord {

    private Map<String, ChannelHandlerContext> currentOccupier;
    private static Landlord instance;

    public Landlord() {
        currentOccupier = new HashMap<>();
    }

    public static Landlord getInstance {
        //return instance
    }

    public void add(Occupier occupier){
        currentOccupier.put("test", occupier.getChannelHandlerContext());    
    }
}

And now I try to test the method like this:

public class LandlordTest {

    private Landlord landlord;
    @Mock
    private Occupier occupier;
    @Mock
    private ChannelHandlerContext channelHandlerContext;


    @BeforeEach
    void setUp() {
        occupier = mock(Occupier.class);
        channelHandlerContext = mock(ChannelHandlerContext.class);

        landlord = Landlord.getInstance();

        when(occupier.getChannelHandlerContext()).thenReturn(channelHandlerContext);
    }

    public void add(Occupier occupier){
        addedOccupier.put(occupier.getChannelHandlerContext()); 
        //adding succeded
    }

}

Maybe in this short example it wouldn't be needed to test it, but is there a way to verify that the add method was successful? Normally in these kind of cases, I'd try something like: assertEquals(currentOccupier.size(), 1), but here I can't access the hashMap of the instance to do it like this. Is there another way to verify the correct behaviour of adding it?

Aucun commentaire:

Enregistrer un commentaire