lundi 24 août 2015

Mocking Socket creation

I need to test a class (using Junit) that uses some code that creates Sockets, no way for me to pass the Socket in the constructor or similar. By mocking Socket creation through

 Socket.setSocketImplFactory(myMockSocketFactory);

the tests run like a charm, but since this is a static method call, the SocketImplFactory is set for the whole VM running my tests, causing other tests to fail down the line (the whole is run through a maven build). The Java API just allows changing this factory once!

Is there a way to mock the Socket creation for just this Test Suite and restore the normal one after that? Please consider, no changes to code are allowed, this needs all to be done on the Junit side. Also woulnd't want to have to configure maven to run this tests in a separate VM.

Aucun commentaire:

Enregistrer un commentaire