简体   繁体   中英

Better to use @Autowired or new in Spring test class?

I was wondering about this question by having some trouble writting unit tests for my spring application.

Let's take the following example:

@SpringBootTest
@RunWith(SpringRunner.class)
public class AlbumDTOConverterTest {

    @Autowired
    private AlbumDTOConverter albumDTOConverter;

    @Test
    public void ToEntity_ReturnValue_NotNull() {
        AlbumDTO albumDTO = new AlbumDTO("Summer album", new Date(), "This summer, we have made some wonderfull photos. Have a look!", null);
        assertNotNull(albumDTOConverter.toEntity(albumDTO));
    }
}

In order to make @Autowired work properly, I am launching a container with annotating the test class with @SpringBootTest .

The thing is that I think I am doing this wrong. In my opinion, I'll rather just create a new instance of AlbumDTOConverter by just using the new operator instead of using Spring's IoD.

What do you guys think about this ?

For unit tests you don't need a whole container to start. By definition, such units should be tested in isolation. Creating an instance of a class with the new keyword is perfectly fine. Even if the class has dependencies on other classes, you can also create them manually and pass to an appropriate constructor of the class.

Note that the unit is not the same as the class . The term of the unit is commonly confused among developers, especially beginners. You don't have to rely on the dependency injection in your unit tests. The container will only increase the time needed to execute the tests and the long execution time is the main reason why developers avoid running them very often. There is nothing wrong in manually building your dependency tree for a unit under tests.

In the long run, creating similar inputs for different tests might lead to duplication in the test code, but fortunately there are best practices for this problem, eg shared fixture .

If you are doing unit test then you should not use @Autowire every time.

Unit test basic says "Unit tests are responsible for testing a specific piece of code, just a small functionality (unit) of the code"

Now question is when to use spring capabilities ?

Sometimes, you'll need to do some unit tests relying on Spring framework like web service call, repository call etc. For example, if you have a repository that has a custom query using the @Query annotation, you might need to test your query. Also, if you are serialising/deserialising objects, you'd want to make sure that your object mapping is working. You might want to test your controllers as well, when you have some parameter validation or error handling. How can you be sure that you are using Spring correctly? In these situations you can take advantage of the new Spring Boot's test annotations.

I thinks this will give better idea.

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