测试不支持另一个类的变量

时间:2018-05-21 06:21:11

标签: java junit static-variables

我有一个班次测试

import com.monotonic.Shipment.project.ProductFixture;

public class ShipmentTest  {

    private Shipment shipment = new Shipment();

    @Test
    public void shouldAddItems() throws Exception {
        shipment.add(door); // it is not recognizing door and window objs
        shipment.add(window);

        assertThat(shipment, contains(door, window));
    }
我从ProductFixture类

导入的门窗
public static Product door = new Product("Wooden Door", 35);
    public static Product floorPanel = new Product("Floor Panel", 25);
    public static Product window = new Product("Glass Window", 10);

我已将上述对象设为静态,以便我可以直接访问它们,但在我的测试类中,它无法识别从productFicture类中选择的变量

以下是货件类的添加方法

private final List<Product> products = new ArrayList<Product>();

    public void add(Product product) {
        products.add(product);
    }

有人可以告诉我如何在不实例化productFixture类的情况下访问测试类中的门对象 非常感谢你

2 个答案:

答案 0 :(得分:1)

static课程中需要import ShipmentTest

更改您的导入

import com.monotonic.Shipment.project.ProductFixture;

import static com.monotonic.Shipment.project.ProductFixture.*;

请注意,代码可读性和可维护性视图中过多的静态导入

因此,您可以在ProductFixture.door, ProductFixture.floorPanel类中定期导入ProductFixture.window,而不是静态导入,而只需使用ProductFixtureShipmentTest

答案 1 :(得分:1)

你可以得到这样的变量:

ProductFixture.door and ProductFixture.window