Better to use @Autowired or new in Spring class?

I was wondering about this question if you are having trouble running unit tests for my spring application.

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));
    }
}

To do it @Autowiredright, I run a container with a test class annotation @SpringBootTest.

The thing is, I think I'm doing it wrong. In my opinion, I would rather just create a new instanceone AlbumDTOConverterby simply using the operator newinstead of using spring IoD.

What do you guys think about this?

+4
source share
2 answers

. , . . , .

, . , . . , , , . .

, , , , . .

+4

unit test, @Autowire .

Unit test basic : " , () "

, spring?

, spring, -, .. , @Query, . , / , , . , , . , spring ? spring Boot test.

, .

+1

Source: https://habr.com/ru/post/1661173/


All Articles