Junit 4 学习


Junit 4 学习

  • @Before 和 @After 可以有多个
  • @Before 和 @After 可以修饰同一个方法
  • @BeforeClass –> @Before –> @Test –> @After –> @AfterClass
  • @Before –> @Test –> @After

Java 泛型

定义

Info<T>
Info<T super String>
Info<T extends Number>

引用

Info<?>
Info<? extends Number>
Info<? super String> //只能接收String或Object类型的泛型

Java 变参方法

public static void test(String str, Integer... test) {  
    System.out.println(str + test[0]);
}

易变业务使用脚本语言来编写

Java 开源库推荐

plexus

<dependency>
  <groupId>org.codehaus.plexus</groupId>
  <artifactId>plexus-utils</artifactId>
  <version>3.0.9</version>
</dependency>

com.google.guava

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>16.0.1</version>
</dependency>

相同的工具只选择一种就好

Java 编码规范

  • 包名全小写
  • 类名首字母全大写
  • 常量全部大写并用下划线分隔
  • 变量采用驼峰命名法

Maven

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=my-app
mvn archetype:create -DgroupId=com.yuwenyi.mvnwork -DartifactId=app1

/src/main/java
/src/test/java

cd /pom dir

mvn clean compile
mvn clean test
mvn clean package
mvn clean install

mvn [plugin]:[goal]
mvn dependency:copy-dependencies
mvn dependency:copy-dependencies -DoutputDirectory=${project.build.directory}/lib
mvn dependency:tree
mvn dependency:list
mvn dependency:sources  # tells Maven to resolve all dependencies and their source attachments, and displays the version.
mvn dependency:build-classpath
mvn dependency:unpack-dependencies -DoutputDirectory=src/main/java
mvn eclipse:eclipse

mvn surefire-report:report

mvn source:jar
mvn source:test-jar
mvn ant:ant
mvn ant:clean

# cool
mvn clean dependency:unpack-dependencies -DoutputDirectory=${project.build.directory}/classes package

pom: 
    <scope>compile</scope>
    <scope>test</scope>

<build>
<plugins>
  <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
          <archive>
              <manifest>
                  <addClasspath>true</addClasspath>
                  <classpathPrefix>lib/</classpathPrefix>
                  <mainClass>com.yuwenyi.mvnwork.App</mainClass>
              </manifest>
          </archive>
      </configuration>
  </plugin>
</plugins>
</build>
panda /
Published under (CC) BY-NC-SA in categories junit  tagged with java  junit