Skip to content

Commit 51a181a

Browse files
committed
jtong - init
0 parents  commit 51a181a

7 files changed

Lines changed: 99 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
.DS_Store
3+
*.iml
4+
target

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Java基础项目
2+
===========
3+
4+
这是一个基于Maven的基础项目,主要用于初学者练习Java使用。
5+
6+
## 已存在代码讲解
7+
8+
已经存在的代码主要讲解如何使用测试,我们看到HelloWorldTest下有两个测试,第一个测试演示了如何进行集成测试和使用Assert。
9+
第二个测试演示了如何进行mock。

pom.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ut-workshop1</groupId>
8+
<artifactId>ut-workshop1</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.9</version>
16+
<type>jar</type>
17+
<scope>test</scope>
18+
<optional>true</optional>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.mockito</groupId>
22+
<artifactId>mockito-all</artifactId>
23+
<version>1.9.5</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.easytesting</groupId>
28+
<artifactId>fest-assert-core</artifactId>
29+
<version>2.0M10</version>
30+
</dependency>
31+
</dependencies>
32+
</project>

src/main/java/Dependency.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Dependency {
2+
public String say(){
3+
return "Leave me alone.";
4+
}
5+
}

src/main/java/HelloWorld.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Created by twer on 3/28/14.
3+
*/
4+
public class HelloWorld {
5+
private Dependency dependency;
6+
7+
public HelloWorld(Dependency dependency) {
8+
9+
this.dependency = dependency;
10+
}
11+
public String beenCalled() {
12+
return dependency.say();
13+
}
14+
}

src/main/resources/.gitkeep

Whitespace-only changes.

src/test/java/HelloWorldTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import org.junit.Test;
2+
3+
import static org.fest.assertions.api.Assertions.assertThat;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.when;
6+
7+
8+
public class HelloWorldTest {
9+
@Test
10+
public void hello_world_test() {
11+
//given
12+
Dependency dependency = new Dependency();
13+
HelloWorld helloWorld = new HelloWorld(dependency);
14+
15+
//when
16+
String actual = helloWorld.beenCalled();
17+
18+
//then
19+
assertThat(actual).isEqualTo("Leave me alone.");
20+
}
21+
22+
@Test
23+
public void should_be_mocked() {
24+
//given
25+
Dependency dependency = mock(Dependency.class);
26+
when(dependency.say()).thenReturn("Hello World");
27+
HelloWorld helloWorld = new HelloWorld(dependency);
28+
29+
//when
30+
String actual = helloWorld.beenCalled();
31+
32+
//then
33+
assertThat(actual).isEqualTo("Hello World");
34+
}
35+
}

0 commit comments

Comments
 (0)