测试不支持另一个 class 的变量
Test is not supporting the variable of another class
我有一个class shipmentTest
导入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));
}
door 和 window 我从 ProductFixture 导入 class
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);
我已将上述对象设为静态,以便我可以直接访问它们,但在我的测试中 class 它无法识别从 productFicture class
中选取的变量
下面是添加发货方式class
private final List<Product> products = new ArrayList<Product>();
public void add(Product product) {
products.add(product);
}
谁能告诉我如何在测试中访问门对象 class 而无需实例化 productFixture class
非常感谢
您需要 static
import
在 ShipmentTest
class.
更改导入
import com.monotonic.Shipment.project.ProductFixture;
到
import static com.monotonic.Shipment.project.ProductFixture.*;
请注意,从代码可读性和可维护性的角度来看,过多的静态导入并不好。
因此,您可以使用 ProductFixture.door, ProductFixture.floorPanel
和 ProductFixture.window
并在 ShipmentTest
class.[=19= 中定期导入 ProductFixture
而不是静态导入]
你可以这样获取变量:
ProductFixture.door and ProductFixture.window
我有一个class shipmentTest
导入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));
}
door 和 window 我从 ProductFixture 导入 class
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);
我已将上述对象设为静态,以便我可以直接访问它们,但在我的测试中 class 它无法识别从 productFicture class
中选取的变量下面是添加发货方式class
private final List<Product> products = new ArrayList<Product>();
public void add(Product product) {
products.add(product);
}
谁能告诉我如何在测试中访问门对象 class 而无需实例化 productFixture class 非常感谢
您需要 static
import
在 ShipmentTest
class.
更改导入
import com.monotonic.Shipment.project.ProductFixture;
到
import static com.monotonic.Shipment.project.ProductFixture.*;
请注意,从代码可读性和可维护性的角度来看,过多的静态导入并不好。
因此,您可以使用 ProductFixture.door, ProductFixture.floorPanel
和 ProductFixture.window
并在 ShipmentTest
class.[=19= 中定期导入 ProductFixture
而不是静态导入]
你可以这样获取变量:
ProductFixture.door and ProductFixture.window