Browse Source

Mock test

master
Tim Lachmann 1 month ago
parent
commit
def506778a

+ 1
- 0
Softwareengeneering.iml View File

@@ -9,5 +9,6 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="junit.jupiter" level="project" />
<orderEntry type="library" name="mockito.core" level="project" />
</component>
</module>

+ 8
- 0
src/Praktikum02/ErrorWriter.java View File

@@ -0,0 +1,8 @@
package Praktikum02;

public class ErrorWriter implements IHelloWorldWriter{
@Override
public void writeHelloWorld() {
System.err.println("Hello World");
}
}

+ 11
- 0
src/Praktikum02/Executor.java View File

@@ -0,0 +1,11 @@
package Praktikum02;

public class Executor {
public static void printHellowWorld(IHelloWorldWriter writer1 , IHelloWorldWriter writer2 , boolean second) {
if (!second) {
writer1.writeHelloWorld();
}else{
writer2.writeHelloWorld();
}
}
}

+ 5
- 0
src/Praktikum02/IHelloWorldWriter.java View File

@@ -0,0 +1,5 @@
package Praktikum02;

public interface IHelloWorldWriter {
void writeHelloWorld();
}

+ 9
- 0
src/Praktikum02/OutWriter.java View File

@@ -0,0 +1,9 @@
package Praktikum02;

public class OutWriter implements IHelloWorldWriter{

@Override
public void writeHelloWorld() {
System.out.println("Hello World");
}
}

+ 32
- 0
test/Praktikum02/ExecutorTest.java View File

@@ -0,0 +1,32 @@
package Praktikum02;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class ExecutorTest {

@Test
void printHelloWorldOnStdOut() {
IHelloWorldWriter outMock = mock(OutWriter.class);
IHelloWorldWriter errMock = mock(ErrorWriter.class);

Executor.printHellowWorld(outMock, errMock ,false);

verify(outMock,times(1)).writeHelloWorld();
verify(errMock,times(0)).writeHelloWorld();

}
@Test
void printHelloWorldOnErrorOut() {
IHelloWorldWriter outMock = mock(OutWriter.class);
IHelloWorldWriter errMock = mock(ErrorWriter.class);

Executor.printHellowWorld(outMock, errMock ,true);

verify(outMock,times(0)).writeHelloWorld();
verify(errMock,times(1)).writeHelloWorld();

}
}

Loading…
Cancel
Save