forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
testing-modules/easymock/src/test/java/com/baeldung/testing/easymock/MatchersUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.baeldung.testing.easymock; | ||
|
||
import static org.easymock.EasyMock.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class MatchersUnitTest { | ||
|
||
@Test | ||
void whenUsingIsA_thenMatchesTypeAndRejectsNull() { | ||
Service mock = mock(Service.class); | ||
mock.process(isA(String.class)); | ||
expectLastCall().times(1); | ||
replay(mock); | ||
|
||
mock.process("test"); | ||
verify(mock); | ||
} | ||
|
||
@Test | ||
void whenUsingIsAWithInheritance_thenMatchesSubclass() { | ||
Service mock = mock(Service.class); | ||
mock.handleRequest(isA(Request.class)); | ||
expectLastCall().times(2); | ||
replay(mock); | ||
|
||
mock.handleRequest(new Request("normal")); | ||
mock.handleRequest(new SpecialRequest()); | ||
verify(mock); | ||
} | ||
|
||
@Test | ||
void whenUsingIsAWithNull_thenFails() { | ||
Service mock = mock(Service.class); | ||
mock.process(isA(String.class)); | ||
expectLastCall().times(1); | ||
replay(mock); | ||
|
||
assertThrows(AssertionError.class, () -> { | ||
mock.process(null); | ||
verify(mock); | ||
}); | ||
} | ||
|
||
@Test | ||
void whenUsingAnyObject_thenMatchesNullAndAnyType() { | ||
Service mock = mock(Service.class); | ||
mock.process(anyObject()); | ||
expectLastCall().times(2); | ||
replay(mock); | ||
|
||
mock.process("test"); | ||
mock.process(null); | ||
verify(mock); | ||
} | ||
|
||
interface Service { | ||
void process(String input); | ||
void handleRequest(Request request); | ||
} | ||
|
||
class Request { | ||
private String type; | ||
Request(String type) { | ||
this.type = type; | ||
} | ||
} | ||
|
||
class SpecialRequest extends Request { | ||
SpecialRequest() { | ||
super("special"); | ||
} | ||
} | ||
} |