我如何才能构建我的java代码(尝试浏览多个论坛的答案,但仍然无法解决)



所以,我刚刚开始使用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文件,后面是数据类";订单";,最后将是运行类";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());
}

}
}

您似乎在使用JUnit测试框架。通常,测试框架依赖于各种机制来发现要运行的测试方法。

基于Netbeans x JUnit的文档:https://netbeans.apache.org//kb/docs/java/junit-intro.html,似乎您需要在测试方法的名称前加上test,以便测试框架运行它

尝试将OrderTests中的public void main(String[] args)方法更改为public void testMain()

一年前,我在使用相同的Netbeans IDE时也遇到过类似的问题。尝试在pom.xml中添加以下依赖项。这实际上添加了JUnit所需的依赖项。

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

相关内容

最新更新