add simple mocking demo
parent
129954d556
commit
4fe9ff36e9
|
@ -27,6 +27,13 @@
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>5.17.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-core</artifactId>
|
<artifactId>log4j-core</artifactId>
|
||||||
|
@ -66,7 +73,8 @@
|
||||||
<transformers>
|
<transformers>
|
||||||
<transformer
|
<transformer
|
||||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
<mainClass>de.hs_mannheim.informatik.mvn.Main</mainClass>
|
<mainClass>
|
||||||
|
de.hs_mannheim.informatik.mvn.Main</mainClass>
|
||||||
</transformer>
|
</transformer>
|
||||||
</transformers>
|
</transformers>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
@ -120,7 +128,8 @@
|
||||||
|
|
||||||
<reporting>
|
<reporting>
|
||||||
<plugins>
|
<plugins>
|
||||||
<!-- generate Javadocs via "mvn site" and find them in the site folder-->
|
<!-- generate Javadocs via "mvn site" and find them in the site
|
||||||
|
folder-->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package de.hs_mannheim.informatik.mvn.mocking;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CountWords {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
var cnt = new CountWords().countWords("Der schnelle braune Fuchs springt über den mueden Hund. Der Hund liegt nur muede herum.");
|
||||||
|
cnt.entrySet().stream().forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Integer> countWords(String text) {
|
||||||
|
var words = text.toLowerCase().split("\\W+");
|
||||||
|
var cnt = new HashMap<String, Integer>();
|
||||||
|
|
||||||
|
for (String w : words) {
|
||||||
|
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Integer> countWordsFromFile(TextExtractor te, String path) {
|
||||||
|
return this.countWords(te.extractText(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package de.hs_mannheim.informatik.mvn.mocking;
|
||||||
|
|
||||||
|
public abstract class TextExtractor {
|
||||||
|
public abstract String extractText(String path);
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package de.hs_mannheim.informatik.mvn.mocking;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
class TextExtractorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCountWords() {
|
||||||
|
CountWords cw = new CountWords();
|
||||||
|
var cnt = cw.countWords("hallo welt");
|
||||||
|
|
||||||
|
assertEquals(1, cnt.get("hallo"));
|
||||||
|
assertEquals(1, cnt.get("welt"));
|
||||||
|
|
||||||
|
assertEquals(null, cnt.get("unbekannt"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCountWordsFromFile() {
|
||||||
|
CountWords cw = new CountWords();
|
||||||
|
|
||||||
|
TextExtractor teMock = Mockito.mock(TextExtractor.class);
|
||||||
|
when(teMock.extractText("/tmp/test.pdf")).thenReturn("hallo welt");
|
||||||
|
|
||||||
|
var cnt = cw.countWordsFromFile(teMock, "/tmp/test.pdf");
|
||||||
|
|
||||||
|
assertEquals(1, cnt.get("hallo"));
|
||||||
|
assertEquals(1, cnt.get("welt"));
|
||||||
|
|
||||||
|
assertEquals(null, cnt.get("unbekannt"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue