Skip to content

Commit

Permalink
Finished normal Mockito testing codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
MewX committed Feb 4, 2018
1 parent e08b44a commit 43a31ff
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.util.List;

public class ServiceCaller {
private Service service = new Service();

public void callService(List<String> list, Message messageHost) {
new Service().runService(list, messageHost);
service.runService(list, messageHost);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.mewx.practice.javamockito;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class ServiceCallerTest {
@InjectMocks
private ServiceCaller serviceCaller = new ServiceCaller();
@Spy
private Service serviceInside = new Service(); // NOTE: cannot apply to new Service() objects inside methods // TODO: try PowerMockito
@Captor
private ArgumentCaptor<List<String>> argCaptor;

private Message message;
private ArrayList<String> list;

@Before
public void setUp() {
// Note: for JUnit 4.5+, no need to initMocks() any more
message = new Message();
list = new ArrayList<>();
list.add("Initial item");
}

@Test
public void callServiceWithOneInList() {
serviceCaller.callService(list, message);
verify(serviceInside, times(1)).runService(argCaptor.capture(), eq(message));
assertEquals(1, argCaptor.getValue().size());
assertEquals(1, message.getSize());
assertEquals(Service.ERROR_MSG, message.getMessageAt(0));
}

@Test
public void callServiceWithTwoInList() {
list.add("Another item");

serviceCaller.callService(list, message);
verify(serviceInside, times(1)).runService(argCaptor.capture(), eq(message));
assertEquals(2, argCaptor.getValue().size());
assertEquals(0, message.getSize());
}
}

0 comments on commit 43a31ff

Please sign in to comment.