File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ .idea
2+ .DS_Store
3+ * .iml
4+ target
Original file line number Diff line number Diff line change 1+ # Java基础项目
2+ ===========
3+
4+ 这是一个基于Maven的基础项目,主要用于初学者练习Java使用。
5+
6+ ## 已存在代码讲解
7+
8+ 已经存在的代码主要讲解如何使用测试,我们看到HelloWorldTest下有两个测试,第一个测试演示了如何进行集成测试和使用Assert。
9+ 第二个测试演示了如何进行mock。
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 1+ public class Dependency {
2+ public String say (){
3+ return "Leave me alone." ;
4+ }
5+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments