Question
So my two tests, one_second and five_seconds are both failing. I also get Waited 6 seconds 5 times from my main method. I know it's
So my two tests, one_second and five_seconds are both failing. I also get "Waited 6 seconds" 5 times from my main method. I know it's something obvious but if anyone could fix it.
-----------------------------
package thread;
public class ConsoleThread {
SystemWrapper systemWrapper;
ThreadWrapper threadWrapper;
public static void main(String args[]) throws InterruptedException {
ConsoleThread consoleThread = new ConsoleThread(new SystemWrapper(), new ThreadWrapper());
consoleThread.start(5);
}
public ConsoleThread(SystemWrapper systemWrapper, ThreadWrapper threadWrapper) {
// TODO Auto-generated constructor stub
this.systemWrapper = systemWrapper;
this.threadWrapper = threadWrapper;
}
public void start(int times) throws InterruptedException {
// TODO Auto-generated method stub
systemWrapper.println("Starting. . .");
for (int i = 0; i < times; i++) {
systemWrapper.println("Waited " + (times + 1) + " second");
}
threadWrapper.sleep(1000);
}
}
---------------------------
package thread;
public class ThreadWrapper {
public void sleep(int i) throws InterruptedException {
// TODO Auto-generated method stub
Thread.sleep(i);
}
}
----------------------------
package thread;
public class SystemWrapper {
public void println(String string) {
System.out.println(string);
}
}
---------------------------
package thread;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
public class ConsoleThreadTest {
ConsoleThread consoleThread;
SystemWrapper systemWrapper;
ThreadWrapper threadWrapper;
@Before
public void setUp() throws Exception {
systemWrapper = Mockito.mock(SystemWrapper.class);
threadWrapper = Mockito.mock(ThreadWrapper.class);
consoleThread = new ConsoleThread(systemWrapper, threadWrapper);
}
@Test
public void print_starting() throws InterruptedException {
consoleThread.start(0);
Mockito.verify(systemWrapper).println("Starting. . .");
}
@Test
public void one_second() throws InterruptedException {
consoleThread.start(1);
Mockito.verify(systemWrapper).println("Starting. . .");
Mockito.verify(systemWrapper).println("Waited 1 second");
}
@Test
public void wait_one_second() throws InterruptedException {
consoleThread.start(1);
Mockito.verify(threadWrapper).sleep(1000);
}
@Test
public void five_seconds() throws InterruptedException {
consoleThread.start(5);
Mockito.verify(systemWrapper).println("Starting. . .");
Mockito.verify(systemWrapper).println("Waited 1 second");
Mockito.verify(systemWrapper).println("Waited 2 second");
Mockito.verify(systemWrapper).println("Waited 3 second");
Mockito.verify(systemWrapper).println("Waited 4 second");
Mockito.verify(systemWrapper).println("Waited 5 second");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started