Skip to content

Commit

Permalink
#13: Improve unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Jan 9, 2021
1 parent f214000 commit 9f8e00c
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.itsallcode.whiterabbit.logic;

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Locale;
import java.util.Optional;

import org.junit.jupiter.api.Test;

class ConfigTest
{
Config config = new TestingConfig();

@Test
void getProjectFile()
{
assertThat(config.getProjectFile()).isEqualTo(Paths.get("data/projects.json"));
}

@Test
void getLogPath()
{
assertThat(config.getLogPath()).isEqualTo(Paths.get("data/logs"));
}

@Test
void getUiStatePath()
{
assertThat(config.getUiStatePath()).isEqualTo(Paths.get("data/ui-state.json"));
}

private static class TestingConfig implements Config
{

@Override
public Path getDataDir()
{
return Paths.get("data");
}

@Override
public Locale getLocale()
{
return null;
}

@Override
public Optional<Duration> getCurrentHoursPerDay()
{
return null;
}

@Override
public boolean allowMultipleInstances()
{
return false;
}

@Override
public boolean writeLogFile()
{
return false;
}

@Override
public Path getConfigFile()
{
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.itsallcode.whiterabbit.logic.service.scheduling;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.itsallcode.whiterabbit.logic.service.ClockService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class SchedulingServiceTest
{
private static final Instant NOW = Instant.parse("2007-12-03T10:15:30.00Z");

@Mock
private ClockService clockServiceMock;
@Mock
private ScheduledExecutorService executorServiceMock;
@Mock
private Runnable runnableMock;
@Mock
private Trigger triggerMock;
private SchedulingService service;

@BeforeEach
void setUp()
{
service = new SchedulingService(clockServiceMock, executorServiceMock);
}

@Test
void scheduleWithDelay()
{
service.schedule(Duration.ofMillis(150), runnableMock);
verify(executorServiceMock).schedule(any(DelegatingErrorHandlingRunnable.class), eq(150L),
eq(TimeUnit.MILLISECONDS));
}

@Test
void scheduleWithTrigger()
{
when(clockServiceMock.instant()).thenReturn(NOW);
final Instant nextExecution = NOW.plusMillis(100);
when(triggerMock.nextExecutionTime(any(), any())).thenReturn(nextExecution);
service.schedule(triggerMock, runnableMock);
verify(executorServiceMock).schedule(any(ReschedulingRunnable.class), eq(100L), eq(TimeUnit.MILLISECONDS));
}
}

0 comments on commit 9f8e00c

Please sign in to comment.