gpt4 book ai didi

java - 有两种方法的 jUnit 失败

转载 作者:行者123 更新时间:2023-11-30 05:02:43 24 4
gpt4 key购买 nike

我有一个测试类,其中有两个注释为 @Test 的方法。

如果我通过注释另一个方法来单独运行每个方法,它就会成功。但如果两者一起运行,就会失败。可能是什么原因?

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 logger
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);

}

}

最佳答案

也许你做了这样的事情:

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);
}
}

第一个测试改变了影响第二个测试的某些状态。在本例中是一个静态字段,但它也可能是文件内容或重用的 Spring 上下文中的 bean。

关于java - 有两种方法的 jUnit 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6123465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com