MissingMethodInvocationException while using when() method on a service which InjectMocks - unit-testing

I am writing unit tests for my service PlanGenerator. I have mocked necessary services and I have InjectMocked plangenerator service as follows:
#Mock
private LoggerFactory lf;
#Mock
private Logger logger;
#Mock
private RequestGenerator requestGenerator;
#InjectMocks
private PlanGenerator planGenerator;
In methods, I am able to call when method on Mocked services like requestGenerator as this:
Mockito.when(requestGenerator.getLatestRequest()).thenReturn(latestRequestDummy);
However, I am not able to call the same method on planGenerator service like this:
Mockito.when(planGenerator.someOtherMethod()).thenReturn(someValue);
It gives me MissingMethodInvocationException error while saying when() requires an argument which has to be 'a method call on a mock'
How can I mock planGenerator service, also it works as same as injectmocks?

Related

JavaFX2: Consume Restful services

I am trying to work on an architecture to consume RESTFul services from a JavaFX application. The requirement is to use spring's rest template(Not too pressed for this. If spring does not work then probably I can look at jersey or resteasy.
My setup is as below:
My main class has a dropdown listing values from a RESTFul service.
The architecture to hit the RESTFul services is as below:
I have a client class that gives me the instance of the service class that hits the rest services.
In the service class, I have various methods that I hit on the RESTFul service.
public MyService1()
{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
MyConfig.class);
context.registerShutdownHook();
}
#Autowired
private RestOperations restTemplate;
private final Logger LOG = LoggerFactory.getLogger(MyService1.class);
public List<MyObject> getMyObjects() throws IOException
{
HttpEntity<MultiValueMap<String, HttpEntity<?>>> request = new HttpEntity<MultiValueMap<String, HttpEntity<?>>>(
createParts(), createMultiPartHeaders());
Map<String, String> vars = new HashMap<String, String>();
List<MyObject> myObjects = restTemplate.getForObject(
"http://localhost:8080/my-webservices/objects", List.class, vars);
return myObjects;
}
With the above setup, I get the exception:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.springframework.beans.factory.BeanCreationException
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:581)
It seems like you have not added the dependency library containing "BeanCreationException" class. Please add the jar file of spring as dependency for that JavaFX Project.

JBoss WebServices and EJB

My application should show a list of stuff over RESTful WebServices.
This list of stuff is generated via an EJB which is deployed in JBoss AS 6 in a .jar.
My question is, how do I get this list of stuff in order to show it via Web Services?
If I try to inject #EJB in the stateless bean annotated with #WebService, and try to invoke the method that generates the list, I get an error saying that #EJB cannot be resolved (or imported). (fixed it, needed to modify maven dependencies).
Now I get a NullPointerException when I call the supposedly injected EJB...
I am using the webapp archetype from Maven, which contains an EAR, a JAR and a WAR.
I am at a total loss here, been trying to figure this for almost a week.
Thanks in advance!
--EDIT-- code and typo fixed
This is my stateless bean which SHOULD be exposed as a WebService (according to:
http://www.adam-bien.com/roller/abien/entry/restful_calculator_with_javascript_and )
#Stateless
#Path("/MyRESTApplication")
public class HelloWorldResource {
#EJB
private TestBean testBean;
#GET()
#Produces("text/plain")
public String sayHello(String name) {
return testBean.doMoreStuff();
}
}
the doMoreStuff() function simply returns "HELLO!".

CDI Constructor Injection in a JAX-WS web service

I have a JAX-WS webservice deployed to a Java EE 6 container.
I would like to use CDI to inject session beans to this webservice. Up till now I have successfully been doing this using field injection but the company policy is to use constructor based injection. The webservice implementation is NOT a session bean. So is
#WebService
public class MyWebservice{
private ServiceA serviceA;
private ServiceB serviceB;
#Inject
public MyWebservice( ServiceA serviceA, ServiceB serviceB)
{
this.serviceA = serviceA;
this.serviceB = serviceB;
}
public void webMethod1()
{
serviceA.doSomething();
serviceB.doSomething();
}
}
acceptable to CDI?

Singleton class with #Startup annotation WL 11g

i have a problem deploying my webservice to Weblogic 11g.
JAVA: JRockit 1.6.x
I need to run a method on webservice deployment and i made this code:
#Singleton
#Startup
public class StartupBean {
Logger logger = Logger.getLogger(StartupBean.class);
#PostConstruct
private void postConstruct() {
logger.error("WS started.");
}
#PreDestroy
private void preDestroy() {
logger.error("WS stoped.");
}
}
without any additional xml config.
It works normally on 12c but i need it on 11g.
What is the workaround?
Thanks
No, you can't do this in pre-3.1-EJB without XML config.
The common practice in EJB 3.0 for implementing a #Startup bean was to instantiate in in a servlet, which is configured to load on startup in web.xml. If you need it just for log4j you can initialize it directly from such servlet.

Spring & Mockito - ignore transitive dependencies

I have a layered application that looks like this:
#PreAuthorize('isAuthenticated()')
#Controller
public class MyController {
#Autowired
MyService service;
}
#Service
public class MyService {
#Autowired
MyDao dao;
}
public interface MyDao {
}
#Repository
public class MyDaoImpl implements MyDao {
}
I want to test the AOP-dependent #PreAuthorize annotation, so I use the SpringJUnit4ClassRunner which creates a test AuthenticationManager and MyController.
If the #ContextConfiguration does not include any bean matching MyService, test initialization fails because it can't resolve the bean.
If I didn't need AOP, I would use Mockito test runner and inject Mockito.mock(MyService.class). But if I try and do it with spring runner, again my test fails because it can't resolve MyDao for the service (even though the service a mock).
I definitely don't want to mock the whole object graph. I'd rather it stopped on the mocked service. How can I do it?
Your MyService should implement an interface, and you should mock the interface instead of the class. Then you won't need a DAO implementation. You might also, perhaps, be running into a similar issue that I faced while trying to test a JAX-RS resource class in Jersey. The problem is how to deploy a single bean into a Spring container but mock its dependencies. I wrote a blog post on it that might help you out if this is the problem you're facing. In particular, the final solution may be of help.