Monthly Archive: August, 2016

Java JUnit4测试代码记录

首先使用Maven引入许要的jar包

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

JUnit4注解解释

  1. @Test : 测试方法,测试程序会运行的方法,后边可以跟参数代表不同的测试,如(expected=XXException.class) 异常测试,(timeout=xxx)超时测试
  2. @Ignore : 被忽略的测试方法
  3. @Before: 每一个测试方法之前运行
  4. @After : 每一个测试方法之后运行
  5. @BeforeClass: 所有测试开始之前运行
  6. @AfterClass: 所有测试结束之后运行

fail方法是指测试失败
assertEquals测试2个参数是否相等,具体参考相应API

Maven 安装及更换阿里云仓库

一、MAVEN下载
Maven 官方下载网址:http://maven.apache.org/download.cgi

二、下载下来之后,解压,找个路径放进去, 把bin的位置设在环境变量里,新建环境变量MAVEN_HOME
注意:MAVEN运行时,系统变量必须有 %JAVA_HOME%

variable

三、在PATH里加入maven的bin的路径

variable

四、配置完毕后,在Windows命令提示符下,输入mvn -v测试一下

maven version

五、配置Eclipse的Maven(点击Window -> Preference -> Maven -> Installation -> Add进行设置)

Eclipse Maven Rutime

六、配置Maven

Eclipse Maven User Setting

七、配置镜像仓库
setting.xml 文件内,找到mirrors节点,添加以下内容(mirror节点)

<mirrors>
	<mirror>
		<id>nexus-aliyun</id>
		<mirrorOf>central</mirrorOf>
		<name>Nexus aliyun</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public</url>
	</mirror>
 
	<mirror>
		<id>sonatype</id>
		<mirrorOf>central</mirrorOf>
		<name>Sonatype Snapshots</name>
		<url>https://oss.sonatype.org/content/groups/public/</url>
	</mirror>
 
	<mirror>
		<id>repo2</id>
		<mirrorOf>central</mirrorOf>
		<name>Human Readable Name for this Mirror.</name>
		<url>http://repo2.maven.org/maven2/</url>
	</mirror>
 
	<mirror>
		<id>net-cn</id>
		<mirrorOf>central</mirrorOf>
		<name>Human Readable Name for this Mirror.</name>
		<url>http://maven.net.cn/content/groups/public/</url>
	</mirror>
 
	<mirror>
		<id>ui</id>
		<mirrorOf>central</mirrorOf>
		<name>Human Readable Name for this Mirror.</name>
	 <url>http://uk.maven.org/maven2/</url>
	</mirror>
 
	<mirror>
		<id>ibiblio</id>
		<mirrorOf>central</mirrorOf>
		<name>Human Readable Name for this Mirror.</name>
	 <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
	</mirror>
 
	<mirror>
		<id>jboss-public-repository-group</id>
		<mirrorOf>central</mirrorOf>
		<name>JBoss Public Repository Group</name>
	 <url>http://repository.jboss.org/nexus/content/groups/public</url>
	</mirror>
</mirrors>

直接在pom.xml中配置仓库

<repositories>
	<repository>
		<id>sonatype-nexus-snapshots</id>
		<name>Sonatype Nexus Snapshots</name>
		<url>http://repo1.maven.org/maven2/</url>
		<releases>
			<enabled>true</enabled>
		</releases>
		<snapshots>
			<enabled>true</enabled>
		</snapshots>
	</repository>
	<repository>
		<id>sonatype</id>
		<name>Sonatype Snapshots</name>
		<url>https://oss.sonatype.org/content/groups/public</url>
		<releases>
			<enabled>true</enabled>
		</releases>
		<snapshots>
			<enabled>true</enabled>
		</snapshots>
	</repository>
</repositories>

log4j/Log4j2 配置

Event LevelLoggerConfig Level
 --TRACEDEBUGINFOWARNERRORFATALOFF
ALLYESYESYESYESYESYESNO
TRACEYESNONONONONONO
DEBUGYESYESNONONONONO
INFOYESYESYESNONONONO
WARNYESYESYESYESNONONO
ERRORYESYESYESYESYESNONO
FATALYESYESYESYESYESYESNO
OFFNONONONONONONO
package main;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
public class Main  {
    public static void main(String args[]){
        Logger Log = LogManager.getLogger(Main.class.getName());
        Log.info("this is the info");
        Log.warn("this is the warn info");
        Log.error("this is the error info");
        Log.fatal("this is the fatal info");
        Log.trace("enter Main.test()");
        //new Main().test();
        Log.trace("exit Main.test()");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
 
<configuration debug="off" status="INFO"> <!-- 这个status是控制系统信息的输出级别 -->
	<Properties>
		<Property name="path">D:/logs</Property>
	</Properties>
 
	<Appenders>
		<Console name="Console" target="SYSTEM_OUT">	<!-- 将日志信息从控制台输出 -->
			<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" />
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36}.%M()/%L - %msg%xEx%n" />
		</Console>
 
		<File name="debug" fileName="${path}/debug.log" append="true">	<!-- 将日志信息写入日志文件 -->
			<Filters>
				<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
				<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
				<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" />
			</Filters>
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36}.%M()/%L - %msg%xEx%n" />
		</File>
 
		<RollingFile name="RollingFile" fileName="${path}/log.log" filePattern="${path}/log-%d{yyyy-MM-dd}_%i.log">
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36}.%M()/%L - %msg%xEx%n" />
			<SizeBasedTriggeringPolicy size="50MB" />
		</RollingFile>
	</Appenders>
 
	<Loggers>
		<Root level="trace">
			<AppenderRef ref="Console" />   <!-- 仅有上述的Appenders配置还不够,这里还不能少,少了就不会在控制台输出 -->
			<AppenderRef ref="RollingFile" />   
			<AppenderRef ref="debug" />  <!-- 仅有上述的Appenders配置还不够,这里还不能少,少了就不会写入文件,但会创建文件 -->
		</Root>
	</Loggers>
 
</configuration>
log4j.rootLogger=debug,stdout,error

# set the output lever
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.Encoding=UTF-8
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout.ConversionPattern=[%p] - [%d{yyyy-MM-dd HH:mm:ss}] %F %m%n

# %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n

# set the Spring lever
log4j.category.org.springframework.beans.factory = DEBUG

### put the log to the file
log4j.appender.debug=org.apache.log4j.RollingFileAppender
log4j.appender.debug.File=../logs/debug.log
log4j.appender.debug.MaxFileSize = 10MB
log4j.appender.debug.Threshold = DEBUG
log4j.appender.debug.Encoding=UTF-8
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=[%p] - [%d{yyyy-MM-dd HH:mm:ss}] %F %m%n

### put the log to the file
log4j.appender.error=org.apache.log4j.RollingFileAppender
log4j.appender.error.File=../logs/error.log
log4j.appender.error.MaxFileSize = 10MB
log4j.appender.error.Threshold = ERROR
log4j.appender.error.Encoding=UTF-8
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=[%p] - [%d{yyyy-MM-dd HH:mm:ss}] %F %m%n

### output the sql
log4j.logger.com.pay.dao=DEBUG

### druid sql ###
log4j.logger.druid.sql=warn,stdout
log4j.logger.druid.sql.DataSource=warn,stdout
log4j.logger.druid.sql.Connection=warn,stdout
log4j.logger.druid.sql.Statement=warn,stdout
log4j.logger.druid.sql.ResultSet=warn,stdout

Archives