Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. public class Simple_Mock{
  2. class Mission{
  3. public Mission(){
  4. }
  5. public void complete(int arg){
  6. // code
  7. }
  8. public boolean isComplete(){
  9. //code
  10. return false;
  11. }
  12. }
  13.  
  14.  
  15. //void answer
  16. @Test
  17. public void mock_Mission_test{
  18. Mission mission_mock = mock(Mission.class);
  19. doAnswer(new Answer<Void>() {
  20. public Void answer(InvocationOnMock invocation) {
  21. when(mission_mock.isCompleted()).thenReturn(true);
  22.  
  23. // get arguments from the innvocation if the method is called with args.
  24. // The doAnswer is how to mock a method LOL
  25. Integer arg = invocation.getArguments()[0];
  26. System.out.println(arg);
  27. return null;
  28. }
  29. }).when(mission_mock).complete(0);
  30.  
  31. task_mock.complete(0);
  32. assertTrue(mission_mock.isCompleted());
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement