File tree 9 files changed +53
-10
lines changed
9 files changed +53
-10
lines changed Original file line number Diff line number Diff line change 4
4
<classpathentry kind =" src" path =" test" />
5
5
<classpathentry kind =" con" path =" org.eclipse.jdt.launching.JRE_CONTAINER" />
6
6
<classpathentry kind =" con" path =" org.eclipse.jdt.junit.JUNIT_CONTAINER/4" />
7
+ <classpathentry kind =" lib" path =" C:/svn/trunk/build/target/ivy/repo-cache/mockito/mockito-all/jars/mockito-all-1.9.5.jar" />
7
8
<classpathentry kind =" output" path =" bin" />
8
9
</classpath >
Original file line number Diff line number Diff line change 3
3
//Client
4
4
public class Client {
5
5
public static void main (String [] args ) {
6
- RemoteControl control = new RemoteControl ();
6
+
7
+ //Invoker
8
+ Switch lightSwitch = new Switch ();
7
9
10
+ //Receiver
8
11
Light light = new Light ();
9
12
13
+ //Concrete Commands from Command Interface
10
14
Command lightsOn = new LightOnCommand (light );
11
15
Command lightsOff = new LightOffCommand (light );
12
16
13
17
// switch on
14
- control .setCommand (lightsOn );
15
- control .pressButton ();
18
+ lightSwitch .setCommand (lightsOn );
19
+ lightSwitch .pressButton ();
16
20
17
21
// switch off
18
- control .setCommand (lightsOff );
19
- control .pressButton ();
22
+ lightSwitch .setCommand (lightsOff );
23
+ lightSwitch .pressButton ();
20
24
21
25
}
22
26
Original file line number Diff line number Diff line change @@ -6,10 +6,12 @@ public class Light {
6
6
7
7
public void switchOn () {
8
8
on = true ;
9
+ System .out .println ("Turned Light On." );
9
10
}
10
11
11
12
public void switchOff () {
12
13
on = false ;
14
+ System .out .println ("Turned Light Off." );
13
15
}
14
16
15
17
}
Original file line number Diff line number Diff line change
1
+ package halterman .command ;
2
+
3
+ //Invoker
4
+ public class Switch {
5
+ private Command command ;
6
+
7
+ public void setCommand (Command command ) {
8
+ this .command = command ;
9
+ }
10
+
11
+ public void pressButton () {
12
+ command .execute ();
13
+ }
14
+
15
+ }
Original file line number Diff line number Diff line change 2
2
3
3
import static org .junit .Assert .*;
4
4
5
+ import org .mockito .Mock ;
6
+ import org .mockito .Mockito ;
7
+ import org .mockito .MockitoAnnotations ;
8
+
9
+ import org .junit .Before ;
5
10
import org .junit .Test ;
6
11
7
12
public class CommandPatternTest {
8
13
14
+ @ Mock
15
+ Light light ;
16
+
17
+ @ Mock
18
+ Command lightsOn ;
19
+
20
+ private Switch control ;
21
+
22
+ @ Before
23
+ public void setUp () {
24
+ MockitoAnnotations .initMocks (this );
25
+ control = new Switch ();
26
+ }
27
+
9
28
@ Test
10
29
public void shouldTurnOnLight () {
11
- Light l = new Light ();
12
- Command loc = new LightOnCommand (l );
13
-
14
- RemoteControl rc = new RemoteControl ();
15
- rc .setCommand (loc );
30
+ Command lightsOn = new LightOnCommand (light );
31
+
32
+ // switch on
33
+ control .setCommand (lightsOn );
34
+ control .pressButton ();
35
+
36
+ //verify
16
37
17
38
// Need Mockito here, will work on this from home.
18
39
}
You can’t perform that action at this time.
0 commit comments