An Introduction to CDI (Contexts and Dependency Injection) (#4503)
* Initial Commit * Update pom.xml * Update TimeLoggerFactoryUnitTest.java * Update PngFileEditorUnitTest.java
This commit is contained in:
parent
4bfb407cfc
commit
4109abebf9
20
cdi/pom.xml
20
cdi/pom.xml
@ -14,6 +14,24 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-core</artifactId>
|
||||||
|
<version>1.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.10.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
@ -42,4 +60,4 @@
|
|||||||
<weld-se-core.version>2.4.1.Final</weld-se-core.version>
|
<weld-se-core.version>2.4.1.Final</weld-se-core.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.dependencyinjection.application;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor;
|
||||||
|
import org.jboss.weld.environment.se.Weld;
|
||||||
|
import org.jboss.weld.environment.se.WeldContainer;
|
||||||
|
|
||||||
|
public class FileApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Weld weld = new Weld();
|
||||||
|
WeldContainer container = weld.initialize();
|
||||||
|
ImageFileProcessor imageFileProcessor = container.select(ImageFileProcessor.class).get();
|
||||||
|
System.out.println(imageFileProcessor.openFile("file1.png"));
|
||||||
|
System.out.println(imageFileProcessor.writeFile("file1.png"));
|
||||||
|
System.out.println(imageFileProcessor.saveFile("file1.png"));
|
||||||
|
container.shutdown();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.dependencyinjection.factories;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import javax.enterprise.inject.Produces;
|
||||||
|
|
||||||
|
public class TimeLoggerFactory {
|
||||||
|
|
||||||
|
@Produces
|
||||||
|
public TimeLogger getTimeLogger() {
|
||||||
|
return new TimeLogger(new SimpleDateFormat("HH:mm"), Calendar.getInstance());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.dependencyinjection.imagefileeditors;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.qualifiers.GifFileEditorQualifier;
|
||||||
|
|
||||||
|
@GifFileEditorQualifier
|
||||||
|
public class GifFileEditor implements ImageFileEditor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String openFile(String fileName) {
|
||||||
|
return "Opening GIF file " + fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String editFile(String fileName) {
|
||||||
|
return "Editing GIF file " + fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String writeFile(String fileName) {
|
||||||
|
return "Writing GIF file " + fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String saveFile(String fileName) {
|
||||||
|
return "Saving GIF file " + fileName;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.dependencyinjection.imagefileeditors;
|
||||||
|
|
||||||
|
public interface ImageFileEditor {
|
||||||
|
|
||||||
|
String openFile(String fileName);
|
||||||
|
|
||||||
|
String editFile(String fileName);
|
||||||
|
|
||||||
|
String writeFile(String fileName);
|
||||||
|
|
||||||
|
String saveFile(String fileName);
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.dependencyinjection.imagefileeditors;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.qualifiers.JpgFileEditorQualifier;
|
||||||
|
|
||||||
|
@JpgFileEditorQualifier
|
||||||
|
public class JpgFileEditor implements ImageFileEditor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String openFile(String fileName) {
|
||||||
|
return "Opening JPG file " + fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String editFile(String fileName) {
|
||||||
|
return "Editing JPG file " + fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String writeFile(String fileName) {
|
||||||
|
return "Writing JPG file " + fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String saveFile(String fileName) {
|
||||||
|
return "Saving JPG file " + fileName;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.dependencyinjection.imagefileeditors;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.qualifiers.PngFileEditorQualifier;
|
||||||
|
|
||||||
|
@PngFileEditorQualifier
|
||||||
|
public class PngFileEditor implements ImageFileEditor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String openFile(String fileName) {
|
||||||
|
return "Opening PNG file " + fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String editFile(String fileName) {
|
||||||
|
return "Editing PNG file " + fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String writeFile(String fileName) {
|
||||||
|
return "Writing PNG file " + fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String saveFile(String fileName) {
|
||||||
|
return "Saving PNG file " + fileName;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.dependencyinjection.imageprocessors;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||||
|
import com.baeldung.dependencyinjection.qualifiers.PngFileEditorQualifier;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import com.baeldung.dependencyinjection.imagefileeditors.ImageFileEditor;
|
||||||
|
|
||||||
|
public class ImageFileProcessor {
|
||||||
|
|
||||||
|
private final ImageFileEditor imageFileEditor;
|
||||||
|
private final TimeLogger timeLogger;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ImageFileProcessor(@PngFileEditorQualifier ImageFileEditor imageFileEditor, TimeLogger timeLogger) {
|
||||||
|
this.imageFileEditor = imageFileEditor;
|
||||||
|
this.timeLogger = timeLogger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ImageFileEditor getImageFileditor() {
|
||||||
|
return imageFileEditor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TimeLogger getTimeLogger() {
|
||||||
|
return timeLogger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String openFile(String fileName) {
|
||||||
|
return imageFileEditor.openFile(fileName) + " at: " + timeLogger.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String editFile(String fileName) {
|
||||||
|
return imageFileEditor.editFile(fileName) + " at: " + timeLogger.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String writeFile(String fileName) {
|
||||||
|
return imageFileEditor.writeFile(fileName) + " at: " + timeLogger.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String saveFile(String fileName) {
|
||||||
|
return imageFileEditor.saveFile(fileName)+ " at: " + timeLogger.getTime();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.dependencyinjection.loggers;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
public class TimeLogger {
|
||||||
|
|
||||||
|
private final SimpleDateFormat dateFormat;
|
||||||
|
private final Calendar calendar;
|
||||||
|
|
||||||
|
public TimeLogger(SimpleDateFormat dateFormat, Calendar calendar) {
|
||||||
|
this.dateFormat = dateFormat;
|
||||||
|
this.calendar = calendar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTime() {
|
||||||
|
return dateFormat.format(calendar.getTime());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.dependencyinjection.qualifiers;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
import javax.inject.Qualifier;
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
|
||||||
|
public @interface GifFileEditorQualifier {}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.dependencyinjection.qualifiers;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
import javax.inject.Qualifier;
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
|
||||||
|
public @interface JpgFileEditorQualifier {}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.dependencyinjection.qualifiers;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
import javax.inject.Qualifier;
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
|
||||||
|
public @interface PngFileEditorQualifier {}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.test.dependencyinjection;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.imagefileeditors.GifFileEditor;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class GifFileEditorUnitTest {
|
||||||
|
|
||||||
|
private static GifFileEditor gifFileEditor;
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setGifFileEditorInstance() {
|
||||||
|
gifFileEditor = new GifFileEditor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGifFileEditorlInstance_whenCalledopenFile_thenOneAssertion() {
|
||||||
|
assertThat(gifFileEditor.openFile("file1.gif")).isEqualTo("Opening GIF file file1.gif");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGifFileEditorlInstance_whenCallededitFile_thenOneAssertion() {
|
||||||
|
assertThat(gifFileEditor.editFile("file1.gif")).isEqualTo("Editing GIF file file1.gif");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGifFileEditorInstance_whenCalledwriteFile_thenOneAssertion() {
|
||||||
|
assertThat(gifFileEditor.writeFile("file1.gif")).isEqualTo("Writing GIF file file1.gif");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGifFileEditorInstance_whenCalledsaveFile_thenOneAssertion() {
|
||||||
|
assertThat(gifFileEditor.saveFile("file1.gif")).isEqualTo("Saving GIF file file1.gif");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
package com.baeldung.test.dependencyinjection;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.imagefileeditors.GifFileEditor;
|
||||||
|
import com.baeldung.dependencyinjection.imagefileeditors.JpgFileEditor;
|
||||||
|
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor;
|
||||||
|
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor;
|
||||||
|
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.jboss.weld.environment.se.Weld;
|
||||||
|
import org.jboss.weld.environment.se.WeldContainer;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ImageProcessorUnitTest {
|
||||||
|
|
||||||
|
private static ImageFileProcessor imageFileProcessor;
|
||||||
|
private static SimpleDateFormat dateFormat;
|
||||||
|
private static Calendar calendar;
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setImageProcessorInstance() {
|
||||||
|
Weld weld = new Weld();
|
||||||
|
WeldContainer container = weld.initialize();
|
||||||
|
imageFileProcessor = container.select(ImageFileProcessor.class).get();
|
||||||
|
container.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setSimpleDateFormatInstance() {
|
||||||
|
dateFormat = new SimpleDateFormat("HH:mm");
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setCalendarInstance() {
|
||||||
|
calendar = Calendar.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImageProcessorInstance_whenInjectedPngFileEditorandTimeLoggerInstances_thenTwoAssertions() {
|
||||||
|
assertThat(imageFileProcessor.getImageFileditor()).isInstanceOf(PngFileEditor.class);
|
||||||
|
assertThat(imageFileProcessor.getTimeLogger()).isInstanceOf(TimeLogger.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() {
|
||||||
|
String currentTime = dateFormat.format(calendar.getTime());
|
||||||
|
assertThat(imageFileProcessor.openFile("file1.png")).isEqualTo("Opening PNG file file1.png at: " + currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() {
|
||||||
|
String currentTime = dateFormat.format(calendar.getTime());
|
||||||
|
assertThat(imageFileProcessor.editFile("file1.png")).isEqualTo("Editing PNG file file1.png at: " + currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() {
|
||||||
|
String currentTime = dateFormat.format(calendar.getTime());
|
||||||
|
assertThat(imageFileProcessor.writeFile("file1.png")).isEqualTo("Writing PNG file file1.png at: " + currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() {
|
||||||
|
String currentTime = dateFormat.format(calendar.getTime());
|
||||||
|
assertThat(imageFileProcessor.saveFile("file1.png")).isEqualTo("Saving PNG file file1.png at: " + currentTime);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.test.dependencyinjection;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.imagefileeditors.JpgFileEditor;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class JpgFileEditorUnitTest {
|
||||||
|
|
||||||
|
private static JpgFileEditor jpgFileUtil;
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setJpgFileEditorInstance() {
|
||||||
|
jpgFileUtil = new JpgFileEditor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJpgFileEditorInstance_whenCalledopenFile_thenOneAssertion() {
|
||||||
|
assertThat(jpgFileUtil.openFile("file1.jpg")).isEqualTo("Opening JPG file file1.jpg");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJpgFileEditorlInstance_whenCallededitFile_thenOneAssertion() {
|
||||||
|
assertThat(jpgFileUtil.editFile("file1.gif")).isEqualTo("Editing JPG file file1.gif");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJpgFileEditorInstance_whenCalledwriteFile_thenOneAssertion() {
|
||||||
|
assertThat(jpgFileUtil.writeFile("file1.jpg")).isEqualTo("Writing JPG file file1.jpg");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJpgFileEditorInstance_whenCalledsaveFile_thenOneAssertion() {
|
||||||
|
assertThat(jpgFileUtil.saveFile("file1.jpg")).isEqualTo("Saving JPG file file1.jpg");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.test.dependencyinjection;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor;
|
||||||
|
import com.baeldung.dependencyinjection.qualifiers.PngFileEditorQualifier;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@PngFileEditorQualifier
|
||||||
|
public class PngFileEditorUnitTest {
|
||||||
|
|
||||||
|
private static PngFileEditor pngFileEditor;
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setPngFileEditorInstance() {
|
||||||
|
pngFileEditor = new PngFileEditor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenPngFileEditorInstance_whenCalledopenFile_thenOneAssertion() {
|
||||||
|
assertThat(pngFileEditor.openFile("file1.png")).isEqualTo("Opening PNG file file1.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenPngFileEditorInstance_whenCallededitFile_thenOneAssertion() {
|
||||||
|
assertThat(pngFileEditor.editFile("file1.png")).isEqualTo("Editing PNG file file1.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenPngFileEditorInstance_whenCalledwriteFile_thenOneAssertion() {
|
||||||
|
assertThat(pngFileEditor.writeFile("file1.png")).isEqualTo("Writing PNG file file1.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenPngFileEditorInstance_whenCalledsaveFile_thenOneAssertion() {
|
||||||
|
assertThat(pngFileEditor.saveFile("file1.png")).isEqualTo("Saving PNG file file1.png");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.test.dependencyinjection;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.factories.TimeLoggerFactory;
|
||||||
|
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TimeLoggerFactoryUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTimeLoggerFactory_whenCalledgetTimeLogger_thenOneAssertion() {
|
||||||
|
TimeLoggerFactory timeLoggerFactory = new TimeLoggerFactory();
|
||||||
|
assertThat(timeLoggerFactory.getTimeLogger()).isInstanceOf(TimeLogger.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.test.dependencyinjection;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TimeLoggerUnitTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTimeLoggerInstance_whenCalledgetLogTime_thenOneAssertion() {
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
TimeLogger timeLogger = new TimeLogger(dateFormat, calendar);
|
||||||
|
String currentTime = dateFormat.format(calendar.getTime());
|
||||||
|
assertThat(timeLogger.getTime()).isEqualTo(currentTime);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user