Trying to find InvoiceStorage dependency for maven - java

Im following a tutorial that has a external class InvoiceStorage i think, im very noob at this, but java cannot resolve that InvoiceStorage symbol, so i think i need a dependency that is not shown in the tutorial
tutorial link:
https://semaphoreci.com/community/tutorials/stubbing-and-mocking-with-mockito-2-and-junit
package com.mokitoTutorial.app;
import com.clusterra.email.sender.EmailSender;
public class LateInvoiceNotifier {
private final EmailSender emailSender;
private final InvoiceStorage invoiceStorage;
public LateInvoiceNotifier(final EmailSender emailSender, final InvoiceStorage invoiceStorage){
this.emailSender = emailSender;
this.invoiceStorage = invoiceStorage;
}
public void notifyIfLate(Customer customer)
{
if(invoiceStorage.hasOutstandingInvoice(customer)){
emailSender.sendEmail(customer);
}
}
}

In this article, the author has just provided an example about how to use mockito. You can see the below from the above article.
In a real system, the InvoiceStorage class is actually a web service
that connects with an external legacy CRM system which is slow. A unit
test could never make use of such as web service.
The author is referring to a class called InvoiceStorage, it is for example. There is no dependency. You can follow the article and you can create your own class to test.

Related

How can I use google guice DI outside of testNG test classes in a testNG based framework?

it is very simple to implement injection of objects into a testNG test class, it is handled mostly for us, however how can I build google guice DI into my framework and use it for classes which are not necessarily tests?
I want to inject using simple dependency injection for dependencies of my Page Object classes, these are nothing really to do with testNG, so how can we get the dependencies initialized for those?
Here is a simple example piece of code I want to replace:
public class HeaderComponent extends AbstractBasePageObject {
private static final Logger LOG = LoggerFactory.getLogger(HeaderComponent.class);
private MenuComponent menu = new MenuComponent(getDriver());
public HeaderComponent(NgWebDriver ngdriver) {
super(ngdriver);
}
public MenuComponent getMenuComponent() {
return menu;
}
}
This class is absolutely nothing to do with testNG itself, so how can I initialize everything for the outcome of:
#Inject
MenuComponent menu
Everything I try the menu throws a nullPointerException because I am having trouble having guice somewhat loaded I think.
I have create some general example for you - test with injection example
It works as you're expecting, I hope. It provides some test configs, injects them to driver and at last driver is injected in test component.
Result test looks like:
import com.google.inject.Inject;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
#Guice(modules = {TestModule.class})
public class SimpleTest {
#Inject
ComponentUnderTest component;
#Test
public void sampleTest() {
System.out.println(component.getParamToTest());
System.out.println(component.param);
System.out.println(component.elseone);
}
}

Eclipse DI does not create a custom object annotated with Created

I'm developing an Eclipse RCP application based on 4.4 Luna version.
I have the following classes:
public class NewProjectDialog extends TitleAreaDialog {
#Inject
private ProjectManager projectManager;
// some code
}
and
#Creatable
#Singleton
public class ProjectManager {
// some Code
}
When I run the application and open NewProjectDialog the following exception is thrown:
org.eclipse.e4.core.di.InjectionException: org.eclipse.e4.core.di.
InjectionException: Unable to process "NewProjectDialog.projectManager":
no actual value was found for the argument "ProjectManager".
Apart #Creatable annotation must I do something more to make Eclipse DI instantiate this class when it cannot find it in the context?
I have also face the same issue and the answer already given by greg-449 is in the comment section of the question is correct. Yes, it is problem in injecting/creating the fields of such objects.

Using Resource from different package in Jersey Application

I have developed an SDK that uses a Resource in a rest package:
package com.example.some.package.rest;
...
#Path("/health")
public class HealthResource {
public HealthResource() {
// some code
}
#GET
#Path("/")
#Produces(MediaType.TEXT_HTML)
public Response getHealth() {
// some code to return health
}
}
Then I have another package with an Application implementation:
package com.example.different.package.rest;
...
public class HealthApplication extends Application {
public Set<Object> getSingletons() {
return Sets.<Object> newHashSet(new HealthResource());
}
}
However, this will not work for me. I have to use another wrapper class:
package com.example.different.package.rest;
...
#Path("")
public class WrapperHealthResource extends HealthResource {
public WrapperHealthResource() {
super();
}
}
If I use WrapperHealthResource in the HealthApplication instead of HealthResource, then it works fine.
I think it's pretty useless to have this extra class. How can I get rid of this complexity?
Firstly, I haven't been able to test this on Jersey 1.8 - I only have a 2.7 test harness around. But the API hasn't really changed in this area so you should be OK.
If you only need the Application class to enforce the Singleton of your HealthResource resource and nothing else, then I would just annotate the class with #Singleton and remove your Application class. That's the easiest.
I'm not able to reproduce your issue with 2.7, so this may be an issue with 1.8 (unlikely in this instance) or how you're registering your Application. If you want to post the rest of your code, I might be able to help.
Will
PS - if you're able, upgrade to 2.8.

Play Framework Dependency Injection

I've been looking all over Google to find some useful information on how to use Guice/Spring DI in Play Framework 2.1
What I want to do is to Inject several Services in some DAO's and vice versa.
Just need some clarification on this - With play 2.1, do you have to use an # annotation within the routes file for DI?
I've looked at this guide here - https://github.com/playframework/Play20/blob/master/documentation/manual/javaGuide/main/inject/JavaInjection.md
and applied the following steps creating a Global class in app and adding the GUICE dependencies in Build.scala but keep on getting a null pointer exception when invoking on the injected object.
Has anyone been able to get DI working in Play 2.1 using Guice? I've seen examples across the internet but they all seem to be using DI within the controller.
I noticed you are using Java. Here is how I got it to work for injecting into a controller.
First, I created the following 4 classes :
MyController:
package controllers;
import play.mvc.*;
import javax.inject.Inject;
public class MyController extends Controller {
#Inject
private MyInterface myInterface;
public Result someActionMethodThatUsesMyInterface(){
return ok(myInterface.foo());
}
}
MyInterface:
package models;
public interface MyInterface {
String foo();
}
MyImplementation2Inject:
package models;
public class MyImplementation2Inject implements MyInterface {
public String foo() {
return "Hi mom!";
}
}
MyComponentModule:
package modules;
import com.google.inject.AbstractModule;
import models.MyInterface;
import models.MyImplementation2Inject;
public class ComponentModule extends AbstractModule {
#Override
protected void configure() {
bind(MyInterface.class).
to(MyImplementation2Inject.class);
}
}
Now the final part, that took me a silly long time to figure out, was to register the module. You do this by adding the following line to the end of the application.conf file, which is located in the conf directory:
play.modules.enabled += "modules.MyComponentModule"
I hope this was helpful to you. :)
I use cake pattern and my own version of Global overriding getControllerInstance
https://github.com/benjaminparker/play-inject
Cheers
Ben
Sorry, this is a late response, but here's our example
https://github.com/typesafehub/play-guice
Have you tried using some different approach to DI than Guice?
We also tried implementing a project with Guice or Spring but ended in registering our dependencies in objects that implement trait such as:
trait Registry {
def userDao: UserDao
...
}
object Registry {
var current: Registry = _
}
object Environnment {
object Dev extends Registry {
val userDao = ...
//implement your environment for develpment here
}
object Test extends Registry {
val userDao = ...
//implement your ennviroment for tests here e.g. with mock objects
}
}
Another good approach wich might fit for you is the cake pattern (just google for it).

akka with play framework

still learning to master akka java with play framework. I have a code snippet below. It was working fine but has decided to give some headaches.
public class Application extends Controller {
static ActorRef masterActor;
RubineActor rubineactor;
public static Result index() {
return ok(index.render(null));
........ somecode
}
it was working fine but now my eclipse juno complains that it cannot resolve the index object in the return line . I am new to both akka and play framework . Can someone please explain what is happening to me. cos have to submit the project as my final year project. thanks
Your problem is not related to Akka, it's a template concern.
The variable index is provided by a template import, certainly import views.html.*;
Eclipse sometimes cannot resolve this object because it is generated automatically by Play after the first request.
Templates are compiled as standard Scala functions, following a simple naming convention. If you create a views/Application/index.scala.html template file, it will generate a views.html.Application.index class that has a render() method.
See the hello word sample for a concrete exemple.

Categories

Resources