This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //create the injector builder | |
| Injector.Builder builder = new Injector.Builder(); | |
| //add the bindings | |
| builder.addClassBinding(ConsoleWriter.class, SystemOutConsoleWriter.class); | |
| builder.addSingletonMapping(MessageFormatter.class, MessageFormatter.class); | |
| builder.addObjectMapping(String.class, "Inject Says"); | |
| //create the injector | |
| Injector inject = builder.build(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ExampleClass | |
| { | |
| private final DependencyInterface dependency; | |
| //no dependency injection | |
| public ExampleClass() | |
| { | |
| //this makes the class hard to test | |
| this.dependency = new Dependency(); | |
| //... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.inject.Injector; | |
| public class MainClass | |
| { | |
| private Injector injector; | |
| /** MANUAL OBJECT TREE SETUP **/ | |
| public void manualObjectTreeSetup() | |
| { | |
| //create the dependency tree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CurrentTimeFormatter | |
| { | |
| private static String FORMAT = "Current time in millis is : "; | |
| //Not testable since can not predict value of | |
| //System.currentTimeMillis() | |
| public String formatCurrentTime_notTestable() | |
| { | |
| return FORMAT + System.currentTimeMillis(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface RequestHandler | |
| { | |
| public HttpResponse handle(HttpRequest request, Context context); | |
| } | |
| public interface HttpRequest | |
| { | |
| public String getPath(); | |
| public Map<String, String> getHeaders(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestRequestHandler | |
| { | |
| private static final String STORE_BODY = "stored body"; | |
| private static final String CASE_HEADER = "case"; | |
| private static final String UPPERCASE = "uppercase"; | |
| private static final String REQUEST_PATH = "path"; | |
| private ExampleRequestHandler requestHandler; | |
| private HttpRequest request; | |
| private Context context; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Test | |
| public void shouldReturnedStoredText() | |
| { | |
| //record mode | |
| //record behavior and expected calls | |
| reset(request, context, monitoring, store, headers); | |
| expect(context.getMonitoring()).andReturn(monitoring); | |
| monitoring.incrementRequestCounter(); | |
| expect(request.getPath()).andReturn(REQUEST_PATH); | |
| expect(request.getHeaders()).andReturn(headers); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestExampleRequestHandler_Mockito | |
| { | |
| private static final String STORE_BODY = "stored body"; | |
| private static final String CASE_HEADER = "case"; | |
| private static final String UPPERCASE = "uppercase"; | |
| private static final String REQUEST_PATH = "path"; | |
| private ExampleRequestHandler requestHandler; | |
| private HttpRequest request; | |
| private Context context; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class Logger | |
| { | |
| private final String name; | |
| private int logCount = 0; | |
| public Logger(String name) | |
| { | |
| this.name = name; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestLogger | |
| { | |
| private static final String LOGGER_NAME = "logger_name"; | |
| private static final Level LEVEL = Level.INFO; | |
| private static final String MESSAGE = "message"; | |
| private static final String FORMATTED_LOG = LOGGER_NAME + " - " + LEVEL + " : " + MESSAGE; | |
| private Logger logger; | |
| @Before |
OlderNewer