public Document createDocument(JournalArticle article) {
try {
Document document = documentService.getDocument(article);
return document;
}
catch (DocumentException e) {
LOGGER.error("Error at parsing document: " + article.getContent(), e);
return null;
}
catch (IllegalArgumentException e) {
LOGGER.error("Error at create document", e);
return null;
}
}
public class NumberServiceTest {
@Test
public void sumTest() {
//code
}
}
public class NumberService {
public double multiply(int x, Double y) {
check(y);
return x*y;
}
private void check(Object o) {
if (o == null) {
throw new IllegalArgumentException();
}
}
}
@Test
@Parameters({"1, 2, 2", "1, 2, 1"})
public void multiplyTest(int x, double y, double result) {
Assert.assertEquals(result, numberService.multiply(x, y), 0);
}
public double divide(int x, Double y) {
check(y);
remoteService.sendSomeEmailMessage(arg...);
return x/y;
}
public class ServiceImpl implements Service {
public int doSomething() {
return 2;
}
public int doSomething2() {
...
}
...
}
public class MainService {
private NumberService numberService;
public MainService(NumberService numberService) {
this.numberService = numberService;
}
public double divide(double x, double y) {
if (y == 0) throw new IllegalArgumentException();
return numberService.divide((int) x, y);
}
}
public class NumberService {
public double divide(int x, Double y) {
return x/y;
}
}
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
@Before
public void init() {
numberServiceMock = mock(NumberService.class);
mainService = new MainService(numberServiceMock);
}
@Before
public void init() {
numberServiceMock = mock(NumberService.class);
mainService = new MainService(numberServiceMock);
}
@Test
public void divide_shouldReturnSameValueAsMock() {
double result = 0.5;
when(numberServiceMock.divide(1, 2D)).thenReturn(result);
double divideResult = mainService.divide(1, 2D);
Assert.assertEquals(result, divideResult, 0);
verify(numberServiceMock, times(1)).divide(anyInt(), anyDouble());
}