Start by creating a Maven project and importing Mockito. Mockito will be used in
ID: 3731374 • Letter: S
Question
Start by creating a Maven project and importing Mockito. Mockito will be used in roder to test double objects, etc.
Your goal for this assignment is to create a method that sysouts "Starting...", waits 1 second, then sysouts "Waited 1 second." then waits 1 second, then sysouts "Waited 2 second.", then waits 1 second, then sysouts "Waited 3 second", then waits 1 second, then sysouts "Waited 4 second.", then waits 1 second, then sysouts "Waited 5 second."
Without TDD, the code is:
System.println("Starting...");
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.println("Waited " + (i + 1) + " second.");
}
You will only need 1 @Test (you're welcome to use more, but that's the bare minimum).
You will need a class called ConsoleThread and test class ConsoleThreadTest.
ConsoleThread will have a single void method: start()
You will need 2 wrappers.
May want a main method somewhere to test the method manually. The main method should just create a new ConsoleThread and call the start() method. Nothing more!
Remember to TDD - one line at a time, forced by the test.
To verify the thread sleep was called 5 times: Mockito.verify(threadWrapper, Mockito.times(5)).sleep(1000);
Explanation / Answer
I have modified the code and Junit is passing as per requirement. Also I have removed PowerMockito which is not required. I hope this will help you.
Use below dependencies in your pom file
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
#1 Console Thread
public class ConsoleThread{
public void start(int x) throws InterruptedException{
System.out.println("Starting...");
for(int i=0;i<5;i++){
ThreadWrapper.sleep(x);
System.out.println("Waited "+(i+1)+" second.");
}
}
}
#2 ThreadWrapper
public class ThreadWrapper {
public static void sleep(int x) throws InterruptedException{
Thread.sleep(x);
}
}
#3 ConsoleMainThread
public class ConsoleMainThread {
public static void main(String[] args) throws InterruptedException {
ConsoleThread consoleThread=new ConsoleThread();
consoleThread.start(1000);
}
}
#4 ConsoleThread Junit Test
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
@RunWith(MockitoJUnitRunner.class)
@PrepareForTest({ConsoleThread.class,ThreadWrapper.class})
public class ConsoleThreadTest {
ConsoleThread consoleThread;
@Mock
ThreadWrapper threadWrapper;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
consoleThread=Mockito.mock(ConsoleThread.class);
Mockito.mock(ThreadWrapper.class);
}
@After
public void tearDown(){
consoleThread=null;
}
@Test
public void startTest() throws Exception{
Mockito.doNothing().when(consoleThread).start(Mockito.anyInt());
consoleThread.start(1000);
consoleThread.start(1000);
consoleThread.start(1000);
consoleThread.start(1000);
consoleThread.start(1000);
Mockito.verify(consoleThread,Mockito.times(5)).start(1000);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.