【发布时间】:2021-03-14 03:05:54
【问题描述】:
所以,我刚刚开始使用 Java,我的第二个任务是将涉及两个 Java 类(一个数据类和一个要运行)的程序放在一起。但是,在我将代码放在一起并通过抽查错误后,我右键单击代码以尝试运行它,但该选项显示为灰色。顺便说一句,我的班级使用 Netbeans IDE,我正在尝试使用 Maven 构建代码。在无法运行代码后,我尝试对其进行测试并收到以下错误:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.2:test (default-cli) on project JavaProgramming-app: No tests were executed
运行“调试测试文件”选项产生了相同的结果。我尝试浏览引用了类似问题的论坛,并找到了编辑 pom.xml 文件的通用解决方案,但我复制的大部分代码已经存在于文件中,或者根本没有帮助。下面首先是 pom.xml 文件,然后是数据类“Order”,最后是运行类“OrderTest”。提前感谢您的帮助。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mycompany</groupId>
<artifactId>JavaProgramming-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>JavaProgramming-app</artifactId>
<packaging>nbm-application</packaging>
<name>JavaProgramming-app</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<all.clusters>${project.build.directory}/${brandingToken}</all.clusters>
</properties>
<dependencies>
<dependency>
<groupId>org.netbeans.cluster</groupId>
<artifactId>platform</artifactId>
<version>${netbeans.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>JavaProgramming-branding</artifactId>
<version>${project.version}</version>
</dependency>
<!-- NbModuleSuite functional in RELEASE70 or later: -->
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-nbjunit</artifactId>
<version>${netbeans.version}</version>
<scope>test</scope> <!-- beyond platform cluster, this often needs to be dropped down to compile/runtime, some other modules in IDE clusters depend on it -->
</dependency>
<!-- To use Jelly Tools in your functional tests, add or replace with:
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-jellytools-platform</artifactId>
<version>${netbeans.version}</version>
<scope>test</scope>
</dependency>
-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.netbeans.utilities</groupId>
<artifactId>nbm-maven-plugin</artifactId>
</plugin>
<!-- Permits NbModuleSuite to be run in integration-test phase: -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<systemPropertyVariables>
<all.clusters>${all.clusters}</all.clusters>
<branding.token>${brandingToken}</branding.token>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>deployment</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.netbeans.utilities</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<executions>
<execution>
<id>extra</id>
<goals>
<goal>autoupdate</goal>
<goal>webstart-app</goal>
<goal>build-installers</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
package orderAssignment;
public class Order {
private String customerName, itemID;
private double itemPrice, shippingFee, subtotal, totalOrderPrice;
private int quantity;
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerName() {
return customerName;
}
public void setItemID(String itemID) {
this.itemID = itemID;
}
public String getItemID() {
return itemID;
}
public void setItemPrice (double itemPrice) {
if (itemPrice > 0.0) {
this.itemPrice = itemPrice;
}
}
public double getItemPrice() {
return itemPrice;
}
public void setQuantity(int quantity) {
if (quantity > 0.0) {
this.quantity = quantity;
}
}
public int getQuantity() {
return quantity;
}
public void subtotal (double itemPrice, int quantity) {
subtotal = itemPrice * quantity;
}
public double getSubtotal() {
return subtotal;
}
public void shippingFee(double subtotal) {
if (subtotal > 0.0) {
shippingFee = subtotal * 0.1;
}
}
public double getShippingFee() {
return shippingFee;
}
public void totalOrderPrice(double subtotal, double shippingFee) {
totalOrderPrice = subtotal + shippingFee;
}
public double getTotalOrderPrice() {
return totalOrderPrice;
}
}
package orderAssignment;
import java.util.Scanner;
public class OrderTest {
private String customerName, itemID;
private double itemPrice, shippingFee, subtotal;
private int quantity, selection1, selection2;
Scanner input = new Scanner(System.in);
public void main(String[] args) {
Order order1 = new Order();
Order order2 = new Order();
System.out.println("Enter the customer name for the first order: ");
customerName = input.nextLine();
order1.setCustomerName(customerName);
System.out.println("Enter the item ID for the first order: ");
itemID = input.nextLine();
order1.setItemID(itemID);
System.out.println("Enter the customer name for the second order: ");
customerName = input.nextLine();
order2.setCustomerName(customerName);
System.out.println("Enter the item ID for the second order: ");
itemID = input.nextLine();
order2.setItemID(itemID);
System.out.println("Enter the price of the item for the first customer's order: ");
itemPrice = input.nextDouble();
order1.setItemPrice(itemPrice);
System.out.println("Enter the quantity of items ordered by the first customer: ");
quantity = input.nextInt();
order1.setQuantity(quantity);
order1.subtotal(itemPrice, quantity);
order1.shippingFee(subtotal);
System.out.println("Enter the price of the item for the second customer's order: ");
itemPrice = input.nextDouble();
order2.setItemPrice(itemPrice);
System.out.println("Enter the quantity of items ordered by the second customer: ");
quantity = input.nextInt();
order2.setQuantity(quantity);
order2.subtotal(itemPrice, quantity);
order2.shippingFee(subtotal);
order1.totalOrderPrice(subtotal, shippingFee);
System.out.printf("Here's the data you entered for the first order:%nCustomer Name: %s%nItem ID: %s%n"
+ "Item Price: $%.2f%nQuantity: %d%nSubtotal: $%.2f%nShipping Fee: $%.2f%nTotal: $%.2f%n"
+ "Would you like to make any changes to the item price or quantity? Enter 0 for no or 1 for yes.", order1.getCustomerName(), order1.getItemID(), order1.getItemPrice(), order1.getQuantity(),order1.getSubtotal(), order1.getShippingFee(), order1.getTotalOrderPrice());
selection1 = input.nextInt();
if (selection1 == 0) {
System.out.println("%nNo changes made.");
}
else if (selection1 == 1) {
System.out.println("Enter the correct item price for the first customer's order: ");
itemPrice = input.nextDouble();
order1.setItemPrice(itemPrice);
System.out.println("Enter the correct quantity order for the first order: ");
quantity = input.nextInt();
order1.setQuantity(quantity);
order1.subtotal(itemPrice, quantity);
order1.shippingFee(subtotal);
order1.totalOrderPrice(subtotal, shippingFee);
System.out.printf("%nHere's the updated data you entered for the first order:%nCustomer Name: %s%nItem ID: %s%n"
+ "Item Price: $%.2f%nQuantity: %d%nSubtotal: $%.2f%nShipping Fee: $%.2f%nTotal: $%.2f",
order1.getCustomerName(), order1.getItemID(), order1.getItemPrice(), order1.getQuantity(),order1.getSubtotal(), order1.getShippingFee(), order1.getTotalOrderPrice());
}
else {
System.out.println("Invalid input, please try again");
}
order2.totalOrderPrice(subtotal, shippingFee);
System.out.printf("Here's the data you entered for the second order:%nCustomer Name: %s%nItem ID: %s%n"
+ "Item Price: $%.2f%nQuantity: %d%nSubtotal: $%.2f%nShipping Fee: $%.2f%nTotal: $%.2f%n"
+ "Would you like to make any changes to the item price or quantity? Enter 0 for no or 1 for yes.", order2.getCustomerName(), order2.getItemID(), order2.getItemPrice(), order2.getQuantity(),order2.getSubtotal(), order2.getShippingFee(), order2.getTotalOrderPrice());
selection2 = input.nextInt();
if (selection2 == 0) {
System.out.println("%nNo changes made.");
}
else if (selection2 == 1) {
System.out.println("Enter the correct item price for the second customer's order: ");
itemPrice = input.nextDouble();
order2.setItemPrice(itemPrice);
System.out.println("Enter the correct quantity order for the second order: ");
quantity = input.nextInt();
order2.setQuantity(quantity);
order2.subtotal(itemPrice, quantity);
order2.shippingFee(subtotal);
order2.totalOrderPrice(subtotal, shippingFee);
System.out.printf("%nHere's the updated data you entered for the second order:%nCustomer Name: %s%nItem ID: %s%n"
+ "Item Price: $%.2f%nQuantity: %d%nSubtotal: $%.2f%nShipping Fee: $%.2f%nTotal: $%.2f",
order2.getCustomerName(), order2.getItemID(), order2.getItemPrice(), order2.getQuantity(),order2.getSubtotal(), order2.getShippingFee(), order2.getTotalOrderPrice());
}
else {
System.out.println("Invalid input, please try again");
System.out.printf("Here's all of the data that you have entered:%n%nOrder 1:%nCustomer Name: %s%nItem ID: %s%n"
+ "Item Price: $%.2f%nQuantity: %d%nSubtotal: $%.2f%nShipping Fee: $%.2f%nTotal: $%.2f%n%n"
+ "Order 2:%nCustomer Name: %s%nItem ID: %s%nItem Price: $%.2f%nQuantity: %d%n"
+ "Subtotal: $%.2f%nShipping Fee: $%.2f%nTotal: $%.2f%n",
order1.getCustomerName(), order1.getItemID(), order1.getItemPrice(), order1.getQuantity(), order1.getSubtotal(), order1.getShippingFee(), order1.getTotalOrderPrice(), order2.getCustomerName(), order2.getItemID(), order2.getItemPrice(), order2.getQuantity(), order2.getSubtotal(), order2.getShippingFee(), order2.getTotalOrderPrice());
}
}
}
【问题讨论】:
-
Maven 需要一个特定的文件结构。您确定测试文件在预期的文件夹结构中,即在测试文件夹中吗?所有 IDE 都支持这些,您可以将项目添加为 maven 项目。但是简单的命令行也应该可以工作
-
我刚才也试着写了一个简单的代码,看看我运行它会发生什么。代码是:
package demos; public class test { public static void main(String args[]) { System.out.println("hello world"); } }但是,我收到以下错误:al org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project JavaProgramming-app: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]我只需要重新安装 Netbeans/JDK 吗?