This is a dummy code with the test (junit5+mockito). How to enforce to return expected list of values in the unit test?
I try with spy(), mock(), but I don't get expected value or sometimes I get null pointer exception.
class A {
}
public interface B {
public List<A> f1();
}
class X {
B o1;
public X(B y) {
o1 = y;
}
protected void x() {
List<A> results = m1();
// ...
}
protected List<A> m1() {
return o1.f1();
}
}
class XTest {
#Mock
private static B b;
#BeforeAll
public static void setUp() {
b = org.mockito.Mockito.mock(B.class);
}
#Test
public void t1() {
X s = spy(new X(b));
A p = new A();
A r = new A();
List<A> c = Arrays.asList(p, r);
when(s.m1()).thenReturn(c); // how to enforce m1() to return c ?
}
}
Try to use doReturn, like this:
public class XTest {
#Mock
private B b;
#Test
public void t1() {
X s = spy(new X(b));
List<A> list = Arrays.asList(new A(), new A());
doReturn(list).when(s).m1();
//
doSomething
}
}
You test class X and for this you mock class B. Never mock the class that you want to test:
class XTest {
private static B b;
#BeforeAll
public static void setUp() {
b = org.mockito.Mockito.mock(B.class);
}
#Test
public void t1() {
X s = new X(b);
A p = new A();
A r = new A();
List<A> c = Arrays.asList(p, r);
when(b.f1()).thenReturn(c); // m1() calls f1 and returns c
}
}
This is my first day learning java (on my own), so I need some help.
This is my code:
public class java_main {
public static void main(String[] args) {
MyClass my = new MyClass(3,4);
MyClass your = new MyClass();
}
public class MyClass {
public int a,b;
public Myclass(int i, int j) {
a = i;
b = j;
}
public Myclass() {
a = 1;
b = 2;
}
}
}
I'm getting this error:
No enclosing instance of type java_main is accessible. Must qualify the allocation with an enclosing instance of type java_main (e.g. x.new A() where x is an instance of java_main).
Any suggestions? Thanks in advance!!!
The problem you have is that you have enclosed in java_main class MyClass
public class java_main {
public class MyClass {
}
}
Remove the java_main, to get valid result.
public class MyClass {
public static void main(String[] args) {
MyClass my = new MyClass(3,4);
MyClass your = new MyClass();
}
private final int a,b;
public Myclass() {
this(1,2);
}
public Myclass(int a, int b) {
this.a = a;
this.b = b;
}
}
The ussue you have is casued that you have to create first instance of outer class in way to be create instance of inner.
public static void main(String[] args)
{
java_main outer = new java_main();
Myclass my = outer.new Myclass(3,4);
Myclass your = outer.new Myclass();
}
The key word static apply to parts of code that is not part of object it is only enclosed by its path (a method must be in class).
Try to find a tutorial that will guide you.
You could make MyClass public:
public static class MyClass{
...
}
It works ...
public class java_main{
public static void main(String[] args) {
// TODO Auto-generated method stub
Myclass my = new Myclass(3,4);
Myclass your = new Myclass();
System.out.println("keval");
}
}
class Myclass
{
public int a,b;
public Myclass(int i, int j)
{
a = i;
b = j;
}
public Myclass()
{
a = 1;
b = 2;
}
}
change your code to this:
public class java_main {
public static void main(String[] args)
{
Myclass my = new Myclass(3,4);
Myclass your = new Myclass();
}
}
class Myclass
{
public int a,b;
public Myclass(int i, int j)
{
a = i;
b = j;
}
public Myclass()
{
a = 1;
b = 2;
}
}
Just try this,
public class java_main {
public static void main(String[] args) {
MyClass my = new java_main().new MyClass(3, 4);
MyClass your = new java_main().new MyClass();
}
public class MyClass {
public int a, b;
public MyClass(int i, int j) {
a = i;
b = j;
}
public MyClass() {
a = 1;
b = 2;
}
}
}
Better approach is move the public class into separate file
Class name should start with Capital letter as per Java naming standard.
Here, I just created an instance of java_main and by using that instance created an instance for MyClass. This is an approach if you don't want to make MyClass as static inner class.
else make MyClass as static inner class it will work but it depends on use case,
public class java_main {
public static void main(String[] args) {
MyClass my = new java_main().new MyClass(3, 4);
MyClass your = new java_main().new MyClass();
}
public class MyClass {
public int a, b;
public MyClass(int i, int j) {
a = i;
b = j;
}
public MyClass() {
a = 1;
b = 2;
}
}
}
The problem you are having is you cannot reference non-static variables (instances of MyClass) from the static context (main menu in java_main class).
Either you change your MyClass like this,
public static class Myclass
Or take out MyClass out of the java_main class. And remove the public access modifier from the MyClass. Because you cannot have two public classes in the same file.
public class java_main {
public static void main(String[] args)
{
Myclass my = new Myclass(3,4);
Myclass your = new Myclass();
}
}
class Myclass
{
public int a,b;
public Myclass(int i, int j)
{
a = i;
b = j;
}
public Myclass()
{
a = 1;
b = 2;
}
}
Hope this helps for you or someone else.
Cheers!
Code:
public class A{
B b = new B();
public class B{
public void fun(){ send(A); }
}
I want to do something with all A object in B.
I can create method in A class:
private A getThis(){return this;}
But is it other solution (some keyword)?
Try this code inside your inner class.
A.this
It should give you a reference to the enclosing instance from the outer class.
Here is a small example.
public class A {
private B b = new B();
public class B {
public void fun() {
}
public A getEnclosing(){
return A.this;
}
}
public static void main(String[] args){
A a = new A();
System.out.println(a == a.b.getEnclosing());
}
}
Try
B b = new B(this);
Then B contructor
public B(A a) {
this.a = a;
}
Consider following situation. I want to achieve the different behavior for methoddA() of class A depending upon from where it is getting call like here from class D or class C. How this can be achieved, method overriding is not working here.
class A
{
public methodA(){ //some code }
}
class B
{
A a = new A()
public methodB()
{
a.methodA();
}
}
class C
{
B b = new B();
public methodC()
{
b.methodB();
}
}
class D
{
B b = new B();
public methodD()
{
b.methodB();
}
}
What you need here is Polymorphism. First create an interface -
public interface MyInterface
{
void methodA();
}
then create two different implementations for two different behaviors -
public class First implements MyInterface
{
public void methodA() {
// first behavior
}
}
public class Second implements MyInterface
{
public void methodA() {
// second behavior
}
}
Now create your other classes as follows -
class B
{
public void methodB(MyInterface m)
{
m.methodA();
}
}
class C
{
B b = new B();
public void methodC()
{
// Pass the corresponding behavior implementation
// as argument here.
b.methodB(new First());
}
}
class D
{
B b = new B();
public void methodD()
{
// Pass the second behavior implementation.
b.methodB(new Second());
}
}
This will result in a more maintainable code.
You can pass the class name to your method as a String and in your method check
if(className.equals("A") // or use isInstanceOf() if you are passing objects of A/B
//do something
if(className.equals("B")
// do something else.
Why do you need two different implementations?
This easy trick can work for you... Please correct me if i am wrong..
I following code I have modified the method signature of Class A1 and Class B1 to accept Object and similarly while calling the methods from Class C and Class D whereever we are calling this method of class B1 pass this as reference. In Class A1 we can then check instanceof object and identify the calling class.
class A1
{
public void methodA(Object c){ //some code }
if (D.class.isInstance(c)){
System.out.println("Called from Class D");
}else if (C.class.isInstance(c)){
System.out.println("Called from Class c");
}else{
System.out.println("Called from Some diff class");
}
}
}
class B1
{
A1 a = new A1();
public void methodB(Object c)
{
a.methodA(c);
}
}
class C
{
B1 b = new B1();
public void methodC()
{
b.methodB(this);
}
}
class D
{
B1 b = new B1();
public void methodD()
{
b.methodB(this);
}
}
public class Testnew{
public static void main(String args[]){
D d = new D();
d.methodD();
C c = new C();
c.methodC();
B1 b = new B1();
b.methodB(b);
}
}
In my java application I want to solve the following scenario
class C {
}
Class A extends C {
public A() {
B objB=new B(this);
objB.methodA();
}
public void methodX() {
}
}
Class B {
private A objA;
public B(A a) {
objA=a;
}
public void methodA() {
objA.methodX()
}
}
Is it possible to call methodX() of A from Class B. When I compile the code the objA(in class B) gives access only to Class C methods? Is alternate way exists for solving the above scenario?? Plz help
Apart from the fact that it makes no sense what you are trying to do and code is weired there is nothing wrong with it Semantically . I just tried it like below.
package com.test;
public class So {
/**
* #param args
*/
public static void main(String[] args) {
A a = new A();
B b = new B(a);
b.methodA();
}
}
class A extends C {
public A() {
B objB = new B(this);
objB.methodA();
}
public void methodX() {
}
}
class B {
private A objA;
public B(A a) {
objA = a;
}
public void methodA() {
objA.methodX();
}
}
class C {
}
here is an other variant for main(String[] args) method:
public static void main(String[] args) {
C ca = new A();
B b2 = new B((A)ca); // This is typcasting
b2.methodA();
}