You are on page 1of 24

1

Getting Started

Java

Java

Java

JUnit

TDD

TDD

TDD

31

1.1 StudentTest Student


StudentTest Student
Student StudentTest

Agile Java

1.1

Web

Java

Java

32

StudentStudent ID

story, story
story

1 StudentTest.java

public class StudentTest extends junit.framework.TestCase {


}

1
Java IDEdefault package StudentTest
package

Agile Java

StudentTest.java
StudentTest.java StudentTest {}
StudentTest
public JUnit
public public JUnit

extends junit.framework.TestCase StudentTest


junit.framework.TestCase StudentTest junit.framework.TestCase
StudentTest extends
JUnit StudentTest
1.2 UML StudentTest junit.framework.TestCase
StudentTest junit.framework.TestCase Student

1.2 StudentTest junit.framework.TestCase


StudentTest Java
StudentTest junit.framework.TestCase JAR
Java JAR JUnit

JAR Java
JUnit JAR classpath
Classpath
classpath Java
classpath Windows Unix
Java classpath JAR
class class
classpath Java Java
Java
Agile Java

33

classpath classpath

34

classpath
javac -classpath c:\junit3.8.1\junit.jar StudentTest.java

JUnit.jar 2 JUnit
JUnit.jar
StudentTest.java
classpath, Java
StudentTest.java:1: package junit.framework does not exist
public class StudentTest extends junit.framework.TestCase {
^
1 error

IDE classpath Eclipse


Java Tab

JUnit
StudentTest, JUnit JUnit GUI
JUnit JUnit junit.awtui.TestRunner
AWT 3 StudentTest.class
35

java -cp .;c:\junit3.8.1\junit.jar junit.awtui.TestRunner StudentTest

classpath-cp Java JUnit


Java Java
classpath .
Java4 StudentTest.class
JAR Java class
StudentTest
junit.awtui.TestRunner junit.awtui.TestRunner
TestRunner 1.3

StudentTest /usr/src/studentJUnit.jar /usr/src/JUnit.3.8.1../JUnit/3.8.1/JUnit.jar


3
SwingAWT Java AWT JUnit AWT
junit.swingui.TestRunner junit.awtui.TestRunner Swing
junit.textui.TestRunner text
4
Java does thisJava does this
Java does this Java

Agile Java

1.3 JUnit

36

JUnit
StudentTest Run
Run
5
JUnit
Erros and Failures
JUnit StudentTest
TDD JUnit

StudentTest
public class StudentTest extends junit.framework.TestCase {
public void testCreate() {
}
}

StudentTest
public void testCreate() {
}

Java

Agile Java

testCreateJUnit public
Java

testCreate JUnit JUnit


:
37

testCreate

testCreate Java JUnit

public

void

test

JUnit TestRunner 1.4

1.4 JUnit

38

StudentTestJUnit
Runs:1
testCreate JUnit
Agile Java

Student

Student
testCreate
public class StudentTest extends junit.framework.TestCase {
public void testCreate() {
new Student("Jane Doe");
}
}

testCreate Java Java


testCreate
testCreate Java Student
new Student("Jane Doe");

new
Student

Jane DoeJane Doe


Java java.lang.String Java
Student

Java new Java Student


Java Student

Student
StudentTest 6StudentTest
Student
StudentTest.java:3: cannot find symbol
symbol : class Student
location: class StudentTest
new Student("Jane Doe");
^
1 error

^ Java Student

Java IDE

Agile Java

39

Java

Windows
@echo off
javac -cp c:\junit3.8.1\junit.jar *.java
if not errorlevel 1 java -cp .;c:\junit3.8.1\junit.jar junitawtui.TestRunner
StudentTest

JUnit Unix

40

#!/bin/sh
javac -classpath "/junit3.8.1/junit.jar" *.java
if [ $? -eq 0 ]; then
java -cp ".:/junit3.8.1/junit.jar" junit.awtui.TestRunnerStudentTest
fi

Unix makemake
Ant Ant

Student.java
class Student {
}

javac
javac -cp c:\junit3.8.1\junit.jar *.java

new String Student


Jane Doe
StudentTest.java:3: cannot find symbol
symbol : constructor Student(java.lang.String)
location: class Student
new Student("Jane Doe");
^
1 error

Student

Agile Java

Student
41

new Student("Jane Doe");

Student String
Student.java
class Student {
Student(String name) {
}
}

JUnit
javac -classpath c:\junit3.8.1\junit.jar *.java
java -cp .;c:\junit3.8.1\junit.jar junit.awtui.TestRunner StudentTest

Student

JUnit testCreate Java Student


JUnit testCreate
testCreate Student
testCreate
Student Java Student
new =
StudentTest
public class StudentTest extends junit.framework.TestCase {
public void testCreate() {
Student student = new Student("Jane Doe");
}
}

:
Java Student
Student
student Student Student
test

Agile Java

42

10

someStudent janeDoe student

1.57 student Student

Java Java
C C++

43

1.5

Java
Java

Java
Java

Student
public class StudentTest extends junit.framework.TestCase {
public void testCreate() {
Student student = new Student("Jane Doe");
String studentName = student.getName();
}
}

testCreate Java
7

0x

Agile Java

11

testCreate

student Student
Student Student
getName

student.getName();

student.
getName()
getName()
String studentName
Student String getName()
getName()
Student getName
StudentTest.java:4: cannot find symbol
symbol : method getName()
location: class Student
String studentName = student.getName();
^
1 error

Student getName
class Student {
Student(String name) {
}
String getName() {
}
}

void getName
String Student.java
Student.java:5: missing return statement
}
^
1 error

getName String return Srting


String getName
class Student {
Student(String name) {
}
String getName() {

Agile Java

44

12

return "";
}
45

return String
JUnit

testCreate Student
Student
Student
public class StudentTest extends junit.framework.TestCase {
public void testCreate() {
Student student = new Student("Jane Doe");
String studentName = student.getName();
assertEquals("Jane Doe", studentName);
}
}

Jane Doe

assertEquals Java

Junit.framework.TestCase assertEquals StudentTest

public class StudentTest extends junit.framework.TestCase {

StudentTest junit.framework.TestCase
assertEquals StudentTest Java StudentTest assertEquals
Java junit.framework.TestCase
Java assertEquals
assertEquals StudentTest
StudentTest

AssertEquals Jane Doe studentName


assertEquals JUnit
testCreate studentName Jane Doe
Agile Java

46

13

JUnit 1.6

1.6

47

JUnit
Failure: testCreate(StudentTest): expected:<Jane Doe> but was:<>

StudentTest testCreateJane Doe


Jane DoeJUnit

Student.java
StudentTest assertEquals

assertEquals("Jane Doe", studentName);

assertEquals 8JUnit
Jane Doe studentName
Jane Doe studentName getName

getName Jane Doe:


class Student {
Student(String name) {
}
String getName() {
8

Java

Agile Java

14

return "Jane Doe";


}
}

JUnit
1.7Run
Jane Doe

Jane Doe
48

1.7

StudentTest
Student

Jane Doe
getName
StudentTest Student
Student testCreate Jane
49

Doe student
public void testCreate() {
Student student = new Student("Jane Doe");
String studentName = student.getName();
assertEquals("Jane Doe", studentName);
Student secondStudent = new Student("Joe Blow");
String secondStudentName = secondStudent.getName();
assertEquals("Joe Blow", secondStudentName);
}

Agile Java

15

1.8 student Java Student

JUnitUnix9 start
Windows10 JUnit
IDE JUnit JUnit
class JUnit 11

1.8

50

junit.framework.ComparisonFailure: expected:<...oe Blow> but was:<...ane Doe>

getName Jane Doe assertEquals

Student
Student
student student Java

String myName:
Class student {
String myName;
Student(String name) {
}
String getName() {
9

&
startstart java -cp .;c:\junit3.8.1\junit.jar junit.awtui.TestRunner
StudentTest
11
JUnit Reload classes every run
10

Agile Java

16

return "Jane Doe";


}
}

name myName
Student(String name) {
myName = name;
}

getName myNameJane Doe


String getName() {
return myName;
}

JUnit Run
51

public void testCreate() {


Student student = new Student("Jane Doe");
String studentName = student.getName();
assertEquals("Jane Doe", studentName);
Student secondStudent = new Student("Joe Blow");
String secondStudentName = secondStudent.getName();
assertEquals("Joe Blow", secondStudentName);
}

Student
student
public void testCreate() {
Student student = new Student("Jane Doe");
String studentName = student.getName();
assertEquals("Jane Doe", studentName);
Student secondStudent = new Student("Joe Blow");
String secondStudentName = secondStudent.getName();
assertEquals("Joe Blow", secondStudentName);
assertEquals("Jane Doe", student.getName());
}

student.getName()
student.getName() assertEquals
student secondStudent

Agile Java

Student student = new Student


("Jane Doe");

17

Jane Doe

String studentName=
student.getName();

student

assertEquals(Jane Doe,
studentName);

student Jane Doe

Java

52

Java
new

1
2
Java

public void testCreate() {


Student student = new Student("Jane Doe");
String studentName = student.getName();
assertEquals("Jane Doe", studentName);
Student secondStudent = new Student("Joe Blow");
String secondStudentName = secondStudent.getName();
assertEquals("Joe Blow", secondStudentName);
assertEquals("Jane Doe", student.getName());
}

studentName secondStudentName
student assertEquals
JUnit
Agile Java

53

18

public void testCreate() {


Student student = new Student("Jane Doe");
assertEquals("Jane Doe", student.getName());
Student secondStudent = new Student("Joe Blow");
assertEquals("Joe Blow", secondStudent.getName());
assertEquals("Jane Doe", student.getName());
}

final String firstStudentName = "Jane Doe";

String firstStudentNameJane Doe


final
final
firstStudentName final

final String firstStudentName = "Jane Doe";


Student student = new Student(firstStudentName);
assertEquals(firstStudentName, student.getName());
...
assertEquals(firstStudentName, student.getName());
54

secondStudent
student firstStudent JUnit

public void testCreate() {


final String firstStudentName = "Jane Doe";
Student firstStudent = new Student(firstStudentName);
assertEquals(firstStudentName, firstStudent.getName());
final String secondStudentName = "Joe Blow";
Student secondStudent = new Student(secondStudentName);
assertEquals(secondStudentName, secondStudent.getName());

Agile Java

this

19

assertEquals(firstStudentName, firstStudent.getName());
}

assertEquals Java

this
Student
class Student {
String myName;
Student(String name) {
myName = name;
}
String getName() {
return myName;
}
}

55

myName
studentName
Student student
get

student.getStudentName();

name
name Student name

class Student {
String name;
Student(String name) {

Agile Java

20

name = name;
}
String getName() {
return name;
}
}

junit.framework.ComparisonFailure: expected:<Jane Doe> but was:<null>

Java
Java name
name name name
name = name;

name
null JUnit
expected:<Jane Doe> but was:<null>

56

this this
Java

name n aName
_name
a an aName
Java
this
class Student {
String name;
Student(String name) {
this.name = name;
}
String getName() {
return name;
}
}

this
name name

Agile Java

private

21

private
Java
public void testCreate() {
final String firstStudentName = "Jane Doe";
Student firstStudent = new Student(firstStudentName);
assertEquals(firstStudentName, firstStudent.getName());

57

final String secondStudentName = "Joe Blow";


Student secondStudent = new Student(secondStudentName);
assertEquals(secondStudentName, secondStudent.getName());
assertEquals(firstStudentName, firstStudent.name);
}

Student Student

final String firstStudentName = "Jane Doe";


Student firstStudent = new Student(firstStudentName);
firstStudent.name = "June Crow";
assertEquals(firstStudentName, firstStudent.getName());

Student Student name

setName setName

StudentTest
private Student name
class Student {
private String name;
...

name
StudentTest name
assertEquals(firstStudentName, firstStudent.name);

name has private access in Student

Agile Java

22

assertEquals(firstStudentName, firstStudent.name);
^
1 error

58

Student getName
name
private

Java Java

12

firstNameStringtrim sDescription
firstName trimmer description name
mediaController lastOrderPlaced

Java get getNumberOfStudents


getStudent

59

sellcancelOrder isDoorClosed

13

Student Student
StudentDirectory Students StudentDirectory
StudentDirectory Students Students

12

http://c2.com/cgi/wiki?CamelCase
13

Agile Java

23

RectangleCompactDiscLaundryList HourlyPayStrategy

14

CheckWriterPayrollSummaryReport
ChargebackCalculator

Java _

custard
numerology cust num15 IDE
id tvAdapter
Java stuDent student

C++Java Sun Java


http://java.sun.com/docs/codeconv
/ Essential Java Style16 Elements of Java
Style17

tab

class
class Student

String studentName = student

getName();

Java tabs
14
15
16
17

[Martin2003]
customer number
[Langr2000]
[Vermeulen2000]

Agile Java

60

24

Java
Java
tabs
61

3 4

http://www.chessvariants.com/d.chess/chess.html
1.

Student pawn
PawnTest PawnTest JUnit

2.

testCreate

3.

testCreate Pawn
Pawn

4.

Pawn pawn
pawn white Pawn

5.

testCreate pawnblack pawn


black

Pawn
4

62

6.

testCreate whiteblack

Agile Java