When writing tests on jUnit 4, I was faced with the need to simulate the loss of Internet access to check the stability of the application. There are some standard mechanisms that can provide for disabling WiFi when running tests are not kosher.

  • configuring a firewall from the command line? turn on / off - jmu
  • Connect by timeout randomly if you want to automate the imitation - Gorets
  • There is another option to figure out how your router works and remotely send a reboot command to it - jmu

2 answers 2

That's right - your test should not depend on the configuration and be "semi" automatic. Most likely you have a strongly related code. Divide into two methods - normal execution, and emergency (most likely, a method with an Exception spike). Then they can be checked separately. There would be a code, I would explain with an example :)

    Take out a piece of code responsible for the connection in a separate class extending the standard functionality, for example for sockets:

    public TestSocket extends Socket { private Class myClass; public TestSocket(Class myClass) { super(); this.myClass=myClass; } @Override public InpuStream getInputStream() { if(myClass.getName().indexOf("MyTestSuite") > 0) { //если это тестовый класс return new ByteArrayInputStream(new byte[0])); //возвращаем пустой поток return super.getInputStream(); } } } 

    Well, in general, the scheme as an idea - further with respect to your connection, you can develop further.