package com.mumux.androidtesting.actions;
import com.mumux.androidtesting.actions.impl.ActionUtils;
import com.mumux.androidtesting.actions.impl.StartAction;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.InputStream;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ActionUtils.class, ProcessBuilder.class})
public class StartActionTest {
@Mock
ProcessBuilder pb;
@Mock
Process proc;
@Test
//Solve Issue with mocking Process Builder
public void testSuccess() throws Exception {
Action action = new StartAction();
action.setValues(new Object[]{"XXX"});
//ProcessBuilder pb = PowerMockito.mock(ProcessBuilder.class);
//PowerMockito.whenNew(ProcessBuilder.class).withParameterTypes(String[].class).
// withArguments(anyVararg()).thenReturn(pb);
//when(pb.start()).thenReturn(null);
//PowerMockito.whenNew(ProcessBuilder.class).withParameterTypes(List.class).withArguments(isA(ArrayList.class)).thenReturn(pb);
//Mockito.when(pb.start()).thenReturn(null);
mockStatic(ActionUtils.class);
// Used by message stream
when(ActionUtils.getString(any(InputStream.class))).thenReturn("");
Runtime runtime = Runtime.getRuntime();
Assert.assertNull(action.run(null, runtime));
}
@Test
//TODO Solve
public void testFailure() throws Exception {
Action action = new StartAction();
action.setValues(new Object[]{"XXX"});
// ProcessBuilder pb = PowerMockito.mock(ProcessBuilder.class);
//PowerMockito.whenNew(ProcessBuilder.class).withAnyArguments().thenReturn(pb);
//when(pb.start()).thenReturn(null);
mockStatic(ActionUtils.class);
// Used by message stream
String errorMessage = "Error: Activity not started, unable to resolve Intent...";
when(ActionUtils.getString(any(InputStream.class))).thenReturn(errorMessage);
Assert.assertEquals(errorMessage, action.run(null, Runtime.getRuntime()));
}
}