daily pastebin goal
40%
SHARE
TWEET

Mockito

a guest Apr 21st, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.mockdemo.app;
  2.  
  3. import static org.junit.Assert.*;
  4. import static org.assertj.core.api.Assertions.assertThat;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.mockito.Mockito;
  9. import org.mockito.runners.*;
  10.  
  11. import com.example.mockdemo.messenger.ConnectionStatus;
  12. import com.example.mockdemo.messenger.MalformedRecipientException;
  13. import com.example.mockdemo.messenger.MessageService;
  14. import com.example.mockdemo.messenger.SendingStatus;
  15.  
  16. import static org.mockito.Mockito.*;
  17.  
  18. @RunWith(MockitoJUnitRunner.class)
  19. public class MockitoTest {
  20.  
  21.     Messenger messenger;
  22.     MessageService ms;
  23.    
  24.     private final String VALID_SERVER = "inf.ug.edu.pl";
  25.     private final String INVALID_SERVER = "inf.ug.edu.eu";
  26.  
  27.     private final String VALID_MESSAGE = "some message";
  28.     private final String INVALID_MESSAGE = "ab";
  29.  
  30.     @Before
  31.     public void setUp(){
  32.         ms = Mockito.mock(MessageService.class);
  33.         messenger = new Messenger(ms);
  34.     }
  35.    
  36.     //Same as EasyMock :
  37.    
  38.     @Test
  39.     public void twovalidTest() throws MalformedRecipientException {
  40.         doReturn(ConnectionStatus.SUCCESS).when(ms).checkConnection(VALID_SERVER);
  41.         doReturn(SendingStatus.SENT).when(ms).send(VALID_SERVER, VALID_MESSAGE);
  42.        
  43.         assertEquals(messenger.sendMessage(VALID_SERVER, VALID_MESSAGE), 0);
  44.     }
  45.    
  46.     @Test
  47.     public void validPlusInvalidTest() throws MalformedRecipientException {
  48.         doReturn(ConnectionStatus.SUCCESS).when(ms).checkConnection(VALID_SERVER);
  49.         doReturn(SendingStatus.SENDING_ERROR).when(ms).send(VALID_SERVER, INVALID_MESSAGE);
  50.        
  51.         assertEquals(messenger.sendMessage(VALID_SERVER, INVALID_MESSAGE), 1);
  52.     }
  53.    
  54.     @Test
  55.     public void exceptionTest() throws MalformedRecipientException {
  56.         doReturn(ConnectionStatus.SUCCESS).when(ms).checkConnection(VALID_SERVER);
  57.         when(ms.send(VALID_SERVER, INVALID_MESSAGE)).thenThrow(new MalformedRecipientException());
  58.        
  59.         assertEquals(messenger.sendMessage(VALID_SERVER, INVALID_MESSAGE), 1);
  60.     }
  61.    
  62.     //Other :
  63.    
  64.     @Test
  65.     public void validConnectionTest() throws MalformedRecipientException {
  66.         doReturn(ConnectionStatus.SUCCESS).when(ms).checkConnection(VALID_SERVER);
  67.         assertEquals(messenger.testConnection(VALID_SERVER), 0);
  68.     }
  69.    
  70.     @Test
  71.     public void invalidConnectionTest() throws MalformedRecipientException {
  72.         doReturn(ConnectionStatus.FAILURE).when(ms).checkConnection(INVALID_SERVER);
  73.         assertThat(messenger.testConnection(INVALID_SERVER)).isEqualTo(1);
  74.     }
  75.    
  76.     @Test
  77.     public void nullTextTest() throws MalformedRecipientException {
  78.         assertSame(messenger.sendMessage("", INVALID_MESSAGE), 2);
  79.     }
  80.    
  81.     //Enums
  82.    
  83.     @Test
  84.     public void ConnectionStatusValueTest(){
  85.         assertNotNull(ConnectionStatus.valueOf("FAILURE"));
  86.     }
  87.    
  88.     @Test
  89.     public void SendingStatusValueTest(){
  90.         assertNotNull(SendingStatus.valueOf("SENT"));
  91.     }
  92. }
RAW Paste Data
Top