-1

I am unit testing a class AuthController, which has this constructor

@Autowired
public AuthController(DynamicBeanFactory beanService) {
   Sysout(beanService); //here null is coming - Point-1
}

In Test Class, I have done:

@Mock
DynamicBeanFactory beanService;

@InjectMocks
AuthController authController(beanService);

- Below @Test are there -

Now when I am running this :

the value of beanService inside constructor above at point-1 is coming null.


Also, when I am doing @Autowired at place of @InjectMocks it works.

Below I have shared some questions people asked.

Q) What is AuthController?

A) It's just a class having that constructor I have shared above.

Q) What is Dynamic Bean Factory ?

A) It's a Service class something like this below :

@Service
public class DynamicBeanFactory

   @Autowired
   public DynamicBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory)
   }
}
  • You need to use `@Mock` instead of `@Autowired`, also don't confuse spring with mocking. You shouldn't really use spring in your unit tests – Lino Feb 25 '21 at 09:54
  • @Lino still null is coming inside contructor. – Ashutosh Tiwari Feb 25 '21 at 10:01
  • Please provide a complete [mre], currently your code doesn't compile. What is `AuthController authController(beanService)`? – Lino Feb 25 '21 at 10:02
  • I have updated the question, please check. – Ashutosh Tiwari Feb 25 '21 at 10:10
  • 1
    It still isn't a [mre]. How are you running your tests? How are you initializing your mocks (are you using `MockitoAnnotations.init(this)` in a `@BeforeEach`, or are you using `@RunWith(MockitoJUnitRunner.class)`)? There's still a lot of open questions – Lino Feb 25 '21 at 10:32
  • I don't have @BeforeEach, MockitoAnnotations.init(this. What I am using is this -->@RunWith(MockitoJUnitRunner.class) – Ashutosh Tiwari Feb 25 '21 at 10:34
  • Does this answer your question: [How to use Mockito with JUnit5](https://stackoverflow.com/questions/40961057/how-to-use-mockito-with-junit5) – Lino Feb 25 '21 at 10:35
  • nope it doesn't answer my question. – Ashutosh Tiwari Feb 25 '21 at 10:39

2 Answers2

0

If you are using @Mock remember to add @RunWith(MockitoJUnitRunner.class) to your test class otherwise it won't initialize the mocks.

Tavo Sanchez
  • 141
  • 6
-1

You can use @MockBean for @Autowired dependencies. Like this:

@MockBean
DynamicBeanFactory beanService;

Try this, it's working for me. I assumed that AuthController has a constructor which takes DynamicBeanFactory as a parameter and there is no other dependency.

@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
class TestApplicationTests {
            
    @InjectMocks
    DynamicBeanFactory d;
    
    @MockBean 
    AuthController a;

    @Test
    public void test() {
        Assert.notNull(a);
    }
}
  • Still "null" is coming in the constructor. – Ashutosh Tiwari Feb 25 '21 at 10:00
  • Can you share the code snippet of init() and setUp(). – Manjeet Singh Feb 25 '21 at 10:02
  • I have updated the question. I don;t have init() and setup() in my code. Please mention if I need to put them. – Ashutosh Tiwari Feb 25 '21 at 10:13
  • @AshutoshTiwari, Try the code I have provided above. – Manjeet Singh Feb 25 '21 at 10:44
  • Ok trying this, Manjeet while I am trying testing, the application.properties are not picked by the class I am doing testing for. Do I need to use some annotation or something so that @ Value starts working back. FYI: It works if I do @ Autowired in place of @ InjectMocks – Ashutosh Tiwari Feb 25 '21 at 10:51
  • Not sure about the first part of your question. Yes, it will work if you use @Autowired because it will create an object which is equivalent to running your actual code. Ideally testing should be done in the following way: controller -> service -> dao While testing controller layer, you should mock service layer to mimick it's behaviour and to avoid actual service call. Same for service and Dao. – Manjeet Singh Feb 25 '21 at 11:41
  • Manjeet, the reason your code is not working because I have paramterized constructor whereas by writing @Autowired AuthController, it is giving the params as null that's wht NPE is coming. – Ashutosh Tiwari Feb 26 '21 at 05:14