Spring - DI Test

^_^·2022년 5월 17일
0

Spring Web

목록 보기
1/4
post-thumbnail
  • Project 생성

  • build.gradle 설정 추가 - dependencies
//spring
implementation 'org.springframework:spring-context:5.3.19'
implementation 'org.springframework:spring-core:5.3.19'
implementation 'org.springframework:spring-test:5.3.19'

//lombok
compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
testCompileOnly 'org.projectlombok:lombok:1.18.24'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'

//log4j2
implementation 'org.apache.logging.log4j:log4j-api:2.17.2'
implementation 'org.apache.logging.log4j:log4j-core:2.17.2'
implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.17.2'
  • sample 패키지 생성 - SampleDTO, SampleService 생성
  • SampleDTO
package org.board.sample;
import org.springframework.stereotype.Repository;
@Repository
public class SampleDTO {}
  • SampleService
package org.board.sample;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class SampleService {
    private final SampleDTO sampleDTO;
}
  • resources - log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>

<configuration status="INFO">

    <Appenders>

        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout charset="UTF-8" pattern="%d{hh:mm:ss} %5p [%c] %m%n"/>
        </Console>
    </Appenders>

    <loggers>
        <logger name="org.springframework" level="INFO" additivity="false">
            <appender-ref ref="console" />
        </logger>

        <logger name="org.board" level="INFO" additivity="false">
            <appender-ref ref="console" />
        </logger>

        <root level="INFO" additivity="false">
            <AppenderRef ref="console"/>
        </root>

    </loggers>

</configuration>
  • root-content.xml 생성
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.board.sample"/>

</beans>
  • test - sampleTests패키지 생성 후 sampleTests
package sampleTests;

import lombok.extern.log4j.Log4j2;
import org.board.sample.SampleService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@Log4j2
@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations="file:src/main/webapp/WEB-INF/root-context.xml")
public class SampleTests {

    @Autowired
    private SampleService sampleService;

    @Test
    public void test1(){
		log.info(sampleService);
    }
}
  • SampleTests 결과

0개의 댓글