Skip to content

Commit

Permalink
added missing UnitTest for WorkoutException
Browse files Browse the repository at this point in the history
  • Loading branch information
avalax committed Sep 22, 2016
1 parent 5b2d352 commit f7151f3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ public WorkoutSession(Context context) {

public void switchWorkout(Workout workout) throws WorkoutException {
this.workout = workout;
writeCurrentWorkoutToFile();
try {
writeCurrentWorkoutToFile();
} catch (IOException e) {
throw new WorkoutException(e);
}
}

public void saveCurrentWorkout() throws WorkoutException {
writeCurrentWorkoutToFile();
try {
writeCurrentWorkoutToFile();
} catch (IOException e) {
throw new WorkoutException(e);
}
}

public Workout getWorkout() throws WorkoutException {
Expand All @@ -52,17 +60,12 @@ private Workout readCurrentWorkoutFromFile() {
return workout;
}

private void writeCurrentWorkoutToFile() throws WorkoutException {
protected void writeCurrentWorkoutToFile() throws IOException {
File file = new File(context.getDir("data", Context.MODE_PRIVATE), "currentWorkout");
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
outputStream.writeObject(workout);
outputStream.flush();
outputStream.close();
} catch (IOException ioe) {
throw new WorkoutException(ioe);
}

ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
outputStream.writeObject(workout);
outputStream.flush();
outputStream.close();
}

public boolean hasWorkout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public WorkoutException() {
super();
}

public WorkoutException(IOException ioe) {
super(ioe);
public WorkoutException(Throwable e) {
super(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

Expand All @@ -20,13 +20,13 @@
import de.avalax.fitbuddy.BuildConfig;
import de.avalax.fitbuddy.domain.model.workout.BasicWorkout;
import de.avalax.fitbuddy.domain.model.workout.Workout;
import de.avalax.fitbuddy.domain.model.workout.WorkoutId;
import de.avalax.fitbuddy.domain.model.workout.WorkoutException;
import de.avalax.fitbuddy.domain.model.workout.WorkoutId;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(RobolectricGradleTestRunner.class)
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, manifest = "src/main/AndroidManifest.xml", sdk=21)
public class WorkoutSessionTest {
private WorkoutSession workoutSession;
Expand All @@ -41,7 +41,7 @@ private void writeWorkout(Workout workout) throws IOException {
outputStream.close();
}

private Workout readWorkout() throws Exception {
private Workout readWorkout() throws IOException, ClassNotFoundException {
Workout workout;
File file = new File(context.getDir("data", Context.MODE_PRIVATE), "currentWorkout");
FileInputStream fis = new FileInputStream(file);
Expand Down Expand Up @@ -108,6 +108,35 @@ public void switchWorkout_shouldHaveWorkout() throws Exception {
assertThat(workoutSession.hasWorkout(), equalTo(true));
}

@Test(expected = WorkoutException.class)
public void switchWorkout_shouldThrowWorkoutExceptionWhenIOExceptionOccurs() throws Exception {
WorkoutId workoutId = new WorkoutId("42");
Workout workout = new BasicWorkout();
workout.setWorkoutId(workoutId);
context = RuntimeEnvironment.application.getApplicationContext();
workoutSession = new WorkoutSession(context) {
@Override
protected void writeCurrentWorkoutToFile() throws IOException {
throw new IOException();
}
};

workoutSession.switchWorkout(workout);
}

@Test(expected = WorkoutException.class)
public void saveWorkout_shouldThrowWorkoutExceptionWhenIOExceptionOccurs() throws Exception {
context = RuntimeEnvironment.application.getApplicationContext();
workoutSession = new WorkoutSession(context) {
@Override
protected void writeCurrentWorkoutToFile() throws IOException {
throw new IOException();
}
};

workoutSession.saveCurrentWorkout();
}

@Test
public void persistedWorkout_shouldReturnWorkoutWithId() throws Exception {
WorkoutId workoutId = new WorkoutId("42");
Expand Down

0 comments on commit f7151f3

Please sign in to comment.