I have a problem, unit test should throw an exception
public class MainActivityTest {
NumberPicker warmUpSeconds;
NumberPicker warmUpMinutes;
NumberPicker restSeconds;
NumberPicker restMinutes;
NumberPicker coolDownSeconds;
NumberPicker coolDownMinutes;
NumberPicker workMinutes;
NumberPicker workSeconds;
NumberPicker rounds;
@Test(expected = WrongTimeSetEx.class)
public void testButtonOnClick() throws WrongTimeSetEx {
MainActivity tested=mock(MainActivity.class);
warmUpSeconds=mock(NumberPicker.class);
warmUpMinutes=mock(NumberPicker.class);
restSeconds=mock(NumberPicker.class);
restMinutes=mock(NumberPicker.class);
coolDownMinutes=mock(NumberPicker.class);
coolDownSeconds=mock(NumberPicker.class);
workMinutes=mock(NumberPicker.class);
workSeconds=mock(NumberPicker.class);
rounds=mock(NumberPicker.class);
when(warmUpSeconds.getValue()).thenReturn(0);
when(warmUpMinutes.getValue()).thenReturn(0);
when(restMinutes.getValue()).thenReturn(0);
when(restSeconds.getValue()).thenReturn(0);
when(coolDownSeconds.getValue()).thenReturn(10);
when(coolDownMinutes.getValue()).thenReturn(15);
when(workMinutes.getValue()).thenReturn(10);
when(workSeconds.getValue()).thenReturn(10);
when(rounds.getValue()).thenReturn(0);
tested.setTimes();
}
}
and there is tested method
public void setTimes() throws WrongTimeSetEx
{
warmUpTime = warmUpSeconds.getValue();
warmUpTime += 60 * warmUpMinutes.getValue();
restTime = restSeconds.getValue();
restTime += 60 * restMinutes.getValue();
coolDownTime = coolDownSeconds.getValue();
coolDownTime += 60 * coolDownMinutes.getValue();
workTime = workSeconds.getValue();
workTime += 60 * workMinutes.getValue();
roundsNumber = rounds.getValue();
String communicate="";
if(restTime==0) //check if times are correct and make sense
communicate+="No time set for Rest Time!<br>\n";
if(workTime==0)
communicate+="No time set for Work Time!<br>\n";
if(roundsNumber==0)
communicate+="No rounds number is set!<br>\n";
if(!communicate.isEmpty())
throw new WrongTimeSetEx(communicate);
}
warmUpTimes and the rest of "Times" are package-private, the error I get during test is:
java.lang.AssertionError: Expected exception: jablonskijakub.intervaltraining.exceptions.WrongTimeSetEx
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Process finished with exit code -1
I'm not familiar with unit tests, I just read something about them and I can't find many examples. Sorry in advance for bad code.
@edit it's not even entering setTimes function @edit2 Solved, I supposed my mocks don't make any sense, because I don't even pass them to function (function itself doesn't get any parameters), so I changed definition of setTimes and made it to get minutes and seconds in int and in test I passed only 0 as parameters. It worked, exception was thrown.
As you are throwing Exception when restTime is 0 according to your code & your test mocks restMinutes & restSeconds to return zero which in turn makes your restTime zero & throws WrongTimeSetEx(communicate) where communicate is "communicate+="No time set for Rest Time". So make the following change in your test & it should work fine:-
when(restMinutes.getValue()).thenReturn(10);
when(restSeconds.getValue()).thenReturn(15);
when(rounds.getValue()).thenReturn(10);
Solved, I supposed my mocks don't make any sense, because I don't even pass them to function (function itself doesn't get any parameters), so I changed definition of setTimes and made it to get minutes and seconds in int and in test I passed only 0 as parameters. It worked, exception was thrown.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.