0

I Have no access to MyClass2 code and can't change it. How do i mock/instantiate MyClass2 myClass2?

Classes and code test:

@RunWith(JUnit4.class)//can't change thisone
public class MyTest
{
    @Autowired // or not, tried both ways
    MyClass testedInstance= new MyClass();
    @Test
    public void boot() throws Exception{
        testedInstance.boot();
        assertTrue(true);
    }
}

public class MyClass
{
    @Autowired 
    private MyClass2 myClass2;

    void boot()
    {
        myClass2.foo();//getting a null pointer here
    }
}
Alexander Petrov
  • 8,875
  • 26
  • 66
victor dabija
  • 487
  • 3
  • 10

2 Answers2

1

First you need to annotate your test class with:

@RunWith( SpringJUnit4ClassRunner.class )

Then about MyClass:

@Autowired 
MyClass testedInstance;

you need to delete the = new MyClass(); because you are autowiring it.

Then since now you are injecting MyClass if there is available for injection instance of MyClass2 it will be injected in MyClass, if not it will not.

You need to configure your application context so that such bean MyClass2 exists,

Alexander Petrov
  • 8,875
  • 26
  • 66
0

you can check the original answer

https://stackoverflow.com/a/71591567/5108695

But this is very common problem so I posted the answer here also

In my opinion, we are writing unit test cases and we should not initialize the spring context in order to test a piece of code.

So,

I used Mockito to mock the Autowired beans in my main target test class and injected those mock beans in my main test class Object

maybe sounds confusing, see the following example

Dependencies I used

    testImplementation("org.mockito:mockito-core:2.28.2")
    testImplementation("org.mockito:mockito-inline:2.13.0")
    testImplementation("org.junit.jupiter:junit-jupiter:5.8.2")
    testImplementation("org.mockito:mockito-junit-jupiter:4.0.0")

My main class is Maths and Calculator bean is autowired


class Maths{

   @Autowired Calculator cal;

   .........
   .........

   public void randomAddMethod(){
      cal.addTwoNumbers(1,2); // will return 3;
   }
}

Test class


@ExtendWith(MockitoExtension.class)

class MathsTest{

   @Mock(answer = Answers.RETURNS_DEEP_STUBS) Calculator cal;

   @InjectMocks Maths maths = new Maths();

   @Test testMethodToCheckCalObjectIsNotNull(){
      maths.randomAddMethod();
   }
}

Now cal will not be null in Maths class and will work as expected

Dupinder Singh
  • 6,183
  • 5
  • 33
  • 53