lundi 22 juin 2015

Unit test passing locally but failing on build server

I am testing a method getServerConnection for making an HTTP connection, specifically that the method returns something. The method should return the connection if it is successful and null if there is no response from the server or if an exception is thrown anywhere in the method.

Here is my test method:

`@Test
    public void testGetServerConnection(){
        try {
            URL url = new URL("http://ift.tt/MoCLMW")

            HttpURLConnection response;
            response = testClass.getServerConnection(url);
            if (response == null){
                Assert.assertNull(response);
            } else {
                Assert.assertNotNull(response);
            }
        } catch(Exception e){
            log.error("Unexpected exception in testGetServerConnection");
            //Assert.fail("Unexpected exception: getServerConnection should return Connection or null");
        }
    }`

The test passes and the build is successful locally, but when I synchronize to an SVN repository (beginning a CruiseControl build) my test is failing, causing the build to do so also. This is because of the Assert.fail().

The test is failing correctly, because if an exception is thrown then the test will not pass. However I don't understand why this isn't the case locally. Is there any particular reason that a test will fail in a build when the tests pass locally?

I have looked for similar questions, the most similar being this: Unit tests failing only on the build server. It is also cause by an exception, but it hasn't been answered. I have checked my dependencies locally match that on the build server.

p.s. apologies for the state of the example code - unit tests are not in my wheelhouse!

Aucun commentaire:

Enregistrer un commentaire