I have this method to get my services
public <T extends ServiceInterface> T getService(Class<T> aServiceClass);
I would like to instantiate the class "MyService" that extends to ServiceInterface with this method.
How to do that? should I make a new MyService object to pass it as a parameter? but if I do that the get service method will not be useful.
If I understand your question, you want to create a class MyService, that extends ServiceInterface. And you want to get your Service by the method getService(Class<T> aServiceClass>), right? That would look like this (but please split the classes in different files):
package test;
public class Test2 {
public static void main(String[] args) {
MyService m = getService(MyService.class);
//m.someMethod(...);
}
public static <T extends ServiceInterface> T getService(Class<T> aServiceClass) {
return null; //??
}
//class MyService
public class MyService extends ServiceInterface{
//someMethods...
}
//class ServiceInterface
public class ServiceInterface {
public <T extends ServiceInterface> T getService(Class<T> aServiceClass) {
return null; //whatever the method is doing
}
}
}
Related
I use junit5+mockito to do some test, and I can mock/inject normal class/method, but I cannot mock/inject class called by application context, pls refer to the following code:
Target
public class util implements ApplicationContextAware {
private static ApplicationContext applicationContextSingleton;
public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
return applicationContextSingleton.getBeansOfType(clazz);
}
// ....
}
public interface IA {
List<String> methodInA(balabala...);
}
class A implements IA{
private somethingA;
#Override
public List<String> methodInA(params...){
// some code ...
}
}
class B {
private somethingB;
public methodInB(){
// some code ...
util.getBeansOfType(IA.class).get("A").methodInA(balabala...)
// some code ...
}
}
Test
#SpringBootTest
class BTest{
#Test
void methodInBTest(){
// how to mock/spy/stub/inject class A (or methodInA from class A, or somethingA in class A)
}
}
Attempt
I used following code, and found the class A was inject by mockito, but no real instance was created, so I cannot use when() or verify() to the member or method in class A
#SpringBootTest
#SpyBean(A.class)
class BTest{
#Test
void methodInBTest(){
// how to mock/spy/stub/inject class A (or methodInA in class A, or somethingA in class A)
}
}
Question
how to mock/spy/stub/inject class A (or methodInA, or somethingA in class A) ?
I am working with the DAO pattern on Java, for the implementation I use a DAO interface that is implemented by a "ItemDAO" abstracted class witch then leads to classes like "ItemDAOTxt" and "ItemDAOXml". This same logic is used on classes like "OrderDAO" and "EmployeesDAO; I am working on a abstract class that will use a DAO in a particular way, "ClassA". So is there a way to make some different classes extends "ClassA" in a way that I can use any DAO I want?
public abstract class ClassA
{
private DAO<T> dao;
//...
}
maybe this:
interface Foo<T> {
T getValue();
}
abstract class Bar<T,U> implements Foo<T> {
Bar(T t,U u) {
this.t=t;
this.u=u;
}
#Override public T getValue() {
return t;
}
T t;
U u;
}
class Baz<T,U>extends Bar<T,U> {
Baz(T t,U u) {
super(t,u);
}
}
public class So56811426 {
public static void main(String[] args) {
Baz<Integer,Character> baz=new Baz<Integer,Character>(42,'x');
System.out.println(baz.getValue()+" "+baz.u);
}
}
Here's My code:
public interface Baseinterface {}
abstract class Interface1 implements Baseinterface{}
abstract class Interface2 implements Baseinterface{}
public interface Classinterface {}
And i want to use this code:
public class Myclass(Baseinterface interfaceversion) implements Classinterface{}
Where the kind of interface implementation is passed as a constructor.
So when a function is defined in both of those abstract classes my actual class knows which one to use. I am fairly new at java.
Thanks.
I may be misunderstanding the nature of the question, but here goes:
Given this code which describes two abstract classes that implement the same method as defined by an interface:
interface BaseInterface {
void foo();
}
abstract class ITestA implements BaseInterface {
public void foo() {
System.out.print("A");
}
}
abstract class ITestB implements BaseInterface {
public void foo() {
System.out.print("B");
}
}
public class MyClass {
private BaseInterface enclosed;
MyClass(BaseInterface base) {
enclosed = base;
}
public void foo() {
enclosed.foo(); // call the implementation specific to the instance passed in by constructor
}
}
This could be called like:
public class Test {
void bar() {
// This may look weird cause we're defining an anonymous implementation of the abstract class, without adding any new implementation details
ITestA impl = new ITestA() {};
MyClass myClass = new MyClass(impl);
myClass.foo(); // prints "A"
}
}
So I am trying to implement the guice Provider interface,
public interface Provider<T> {
T get();
}
and I have another interface called Creator
public interface Creator {
void create();
}
and I want to create a Provider to bind different types of Creator when creating a number of CreatePhases.
private static final class CreatePhaseProvider<T> implements Provider<CreatePhase<T extends Creator>>
{
#Override
public CreatePhase<T> get(){
return null;
}
}
This gives me an error "Syntax error on token "extends" ,,". Any suggestions?
private static final class CreatePhaseProvider<T extends Creator> implements Provider<CreatePhase<T>>
{
#Override
public CreatePhase<T> get(){
return null;
}
}
Oops put the extends in the wrong place!
I'm trying to figure out why Mockit is not working for the following code:
public class TestClass {
#Test
public void test() {
Mockit.redefineMethods(ExecuterClass.class, new Object() {
#SuppressWarnings("unused")
public SomeService getService() {
return new MockSomeServiceImpl();
}
});
// Code to run test
}
}
public abstract class ExecuterClass<T,U,V,W> {
// Other methods/variables
public SomeService getService() {
return someProvider.getService();
}
}
public interface SomeService {
// Some method definitions
}
public class MockSomeServiceImpl implements SomeService {
// Some method implementations
}
The error I get back is:
java.lang.IllegalAccessError: tried to access class TestClass from class ExecuterClass
Any ideas on how to fix this?? In the end, I would like the test to use the MockSomeServiceImpl methods rather than those in SomeService implementation.
SomeService was generated by a WSDL, so I don't have the implementation that someObject.getService() returns. So I can't do Mockit.redefineMethods(SomeServiceImpl.class, MockSomeServiceImpl.class)