简体   繁体   中英

JUnit for Spring beans using autowire?

Here is the code:

public class Customer 
{
    @Autowired
    private Person person;
    //some business logic using person object
}

Now I need to write the jUnit test case for the Customer class, how to go about it? Shall i use Mockito to mock the person object and then execute the business logic and if yes the how to set the mocked person object as the property of the Customer without any setter/getter?

Thanks!

Sounds to me like you're overthinking it.

I would not mock a business object model; that's for interface-based classes when you don't want an integration test.

I would not inject a Person into a JUnit test; I'd simply call " new ", instantiate what was appropriate for my test, and get on with it.

If your Customer has to have an instance of Person, I'd recommend constructor injection. You can pass a mock Person that way if you must.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM