开发者

jUnit with two methods fails

I have a test class with two methods annotated as @Test.

If i run each methods individually by commenting the other, it succeeds. But if run both together, it fails. What could be the reason?

public class ProductAvailTest {
private static final String PRODUCT_AVAIL_BUILDER = "ProductAvailBuilder";


@Test
public void productAvailResponseDateRequired() throws Exception {

    ResponseBuilderFactory responseBuilderFactory = ResponseBuilderFactory.createResponseBuilderFactory();
    ResponseBuilder responseBuilder = responseBuilderFactory.createResponseBuilder(PRODUCT_AVAIL_BUILDER);
    ProductAvailDateRqdHelper productAvailDateRqdHelper = new ProductAvailDateRqdHelper();
    List<Rsproducts> products = productAvailDateRqdHelper.getLOMProducts();

    // TODO change this to logger
    System.out.println("No. of products in test " + products.size());

    GetProductAvailOutput actualProductAvailOutput = (GetProductAvailOutput) responseBuilder.buildSuccessResponse(
            products, productAvailDateRqdHelper.getProductAvailInput());
    GetProductAvailOutput expectedProductAvailOutput = productAvailDateRqdHelper.getProductAvailOutput();

    // TODO change this to logger
    System.out.println("Size in expected " + expectedProductAvailOutput.getProductBrand().size());
    System.out.println("Size in actual " + actualProductAvailOutput.getProductBrand().size());

    Assert.assertEquals(expectedProductAvailOutput, actualProductAvailOutput);

}


@Test
public void productAvailResponseInvBased() throws Exception {
    ResponseBuilderFactory responseBuilderFactory = ResponseBuilderFactory.createResponseBuilderFactory();
    ResponseBuilder responseBuilder = responseBuilderFactory.createResponseBuilder(PRODUCT_AVAIL_BUILDER);
    ProductAvailInvHelper productAvailInvHelper = new ProductAvailInvHelper();
    List<Rsproducts> products = productAvailInvHelper.getLOMProducts();

    // TODO change this to logger
    System.out.println("No. of products in test " + products.size());

    System.out.println("No of inventory " + products.get(0).getRsproddtls().size());


    GetProductAvailOutput actualProductAvailOutput = (GetProductAvailOutput) responseBuilder.buildSuccessResponse(
            products, productAvailInvHelper.getProductAvailInput());
    GetProductAvailOutput expectedProductAvailOutput = productAvailInvHelper.getProductAvailOutput();

    // TODO change this to logger
    System.out.println("Size in expected " + expectedProductAvailOutput.getProductBrand().size());
    System.out.println("Size in actual " + actualProductAvailOutput.getProductBrand().size());


    Assert.assertEquals(expectedProductAvailOutput, actualProductAvailOutput);
}




@Test
public void productAvailResponseFreeSell() throws Exception {

    ResponseBuilderFactory responseBuilderFactory = ResponseBuilderFactory.createResponseBuilderFactory();
    ResponseBuilder responseBuilder = responseBuilderFactory.createResponseBuilder(PRODUCT_AVAIL_BUILDER);
    ProductAvailFreeSellHelper productAvailFreeSellHelper = new ProductAvailFreeSellHelper();
    List<Rsproducts> products = productAvailFreeSellHelper.getLOMProducts();

    // TODO change this to lo开发者_Go百科gger
    System.out.println("No. of products in test " + products.size());

    GetProductAvailOutput actualProductAvailOutput = (GetProductAvailOutput) responseBuilder.buildSuccessResponse(
            products, productAvailFreeSellHelper.getProductAvailInput());
    GetProductAvailOutput expectedProductAvailOutput = productAvailFreeSellHelper.getProductAvailOutput();

    // TODO change this to logger
    System.out.println("Size in expected " + expectedProductAvailOutput.getProductBrand().size());
    System.out.println("Size in actual " + actualProductAvailOutput.getProductBrand().size());

    Assert.assertEquals(expectedProductAvailOutput, actualProductAvailOutput);

}

}


Maybe you did something like this:

import org.junit.Assert;
import org.junit.Test;


public class StatefulTest {

    private static boolean shouldSucceed = true;

    @Test
    public void test1() {
        System.out.println("shouldSucceed=" + shouldSucceed);
        Assert.assertTrue(shouldSucceed);
        shouldSucceed = false;
    }

    @Test
    public void test2() {
        System.out.println("shouldSucceed=" + shouldSucceed);
        Assert.assertTrue(shouldSucceed);
    }
}

The first test alters some state affecting the second test. In this case a static field, but it might also be file contents or a bean in a re-used Spring context.


I would hazard a guess that the test methods are not setting up their environment independently. So the first test within a given java invocation works, but leaves the environment in a different state that's not suitable for the next test run.

If you're setting up important state within static initialiser blocks, or possibly within the constructor of the test, you may fall foul of this. Important state should be initialised either as local variables within the test method, or via methods annotated with @Before.

You should expand on what "it fails" means - is it the second of the two tests that fails by any chance, while the first to run succeeds?


Is it possible each state changes system's state (DB or some static fields etc) but not clear it in @After method properly?


Probably some data shared between these two methods that is not reinitialized in a method annotated with @Before.


JUnit Tests work very very very different than a "normal" developer would think. It is strongly adviced to read the JUnit documentation.

If you have a JUnit test with two @Test methods, the following is happening: o an object of your TestClass is created o the first test method is called

o another object is created (so every instance field the first method has changed is lost now) o the second test method is called

This is not a "bug" but a design feature ... in other words the inventors wanted it like that. If you are new to such tools like JUnit, then consider TestNG, in the long run it might be more usefull to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜