使用maven或JUnit调试java程序


maven和JUnit都可以对java项目进行调试。
本文介绍分别使用这两种方法的调试步骤:

maven

新建maven项目

新版本的Eclipse中自带maven,所以只需要自己创建就好了。
Eclipse -> File -> New -> Other -> maven project
group id:填入组织名
artifact id:项目名

修改pom.xml

在创建maven后经常遇到一些问题,如“maven 不再支持源选项 1.5”或“maven中使用junit提示找不到junit包”,以及maven在原网站下载较慢,对镜像的配置等。
这时候要对pom,xml进行一些设置,具体如下:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>
	<modelVersion>4.0.0</modelVersion>
	<groupId>yourgroupId</groupId>
	<artifactId>yourartifactId</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	 <repositories>
		<repository>
	        <id>central</id>
	        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
	    </repository>
	</repositories>
	
  	<pluginRepositories>
	    <pluginRepository>
	        <id>central</id>
	        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
	    </pluginRepository>
	</pluginRepositories>
	
	<dependencies>
	<dependency>
	   <groupId>junit</groupId>
	   <artifactId>junit</artifactId>
	   <version>4.7</version>
	   <scope>test</scope>
	</dependency>
	</dependencies>
	
</project>

JUnit

导入JUnit

在import JUnit中的包时,有时候eclipse会报错,这时候需要将JUnit导入我们的项目中:
在这里插入图片描述
步骤如上图所示,在Add Libraries中选择JUnit并导入即可。然后就可以使用JUnit了。

代码放置

新建了maven项目后,我们的类代码放在src/main/java下,测试代码放在src/test/java下。

测试

右键项目,选择run as:
若要使用JUnit,则点击JUnit。
若要使用Maven,则点击Maven Test。
使用效果如下:
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/DwenKing/article/details/108808964
今日推荐