0%

spring junit에서 autowired 동작하지 않는 현상 해결

spring junit에서 autowired 동작하지 않는 현상 해결

이미지

spring framework 에서 junit4 사용하기

  1. junit test class 최상단에 아래 내용을 입력해줍니다.
    ContextConfiguration의 location 에 대한 xml 은 각자의 프로젝트 상황에 맞게 수정이 필요합니다.
    1
    2
    3
    4
    5
    @RunWith(SpringJUnit4ClassRunner.class) 
    @ContextConfiguration(locations = {
    "file:src/main/resources/spring/context-*.xml",
    "file:src/main/resources/spring/*-servlet.xml"
    })
  2. test하려는 class에 대해서, spring bean이라면 annotation(어노테이션)을 꼭 쓰도록 한다.
    ex) 아래와 같이 service로 bean을 등록하였다면,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Service 
    TestService

    아래와 같이 객체를 @autowired 해주도록 합니다.
    @Autowired
    TestService service;

    아래와 같이 new를 사용하여 객체를 생성하지 않도록 합니다.
    TestService service = new TestService();

테스트 코드

1
2
3
4
5
6
7
8
9
10
11
Service class

@Service
public class TestService{
@Autowired
TestDao dao;

public void insert(int a){
dao.insert(a);
}
}

Junit test class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {
"file:src/main/resources/spring/context-*.xml",
"file:src/main/resources/spring/*-servlet.xml"
})
public class TestServiceTest {
@Autowired
TestService service;

@Test
public void TestServiceTest() {
dao.insert(1);
assertTrue(true);
}
}

테스트 코드에 대한 설명

spring framework, junit4를 사용하는데 autowired가 동작하지 않았던 문제에 대해 공유드립니다.
먼저, test하려는 class는 아래와 같습니다.

1
2
3
4
5
6
7
8
9
@Service 
public class TestService{
@Autowired
TestDao dao;

public void insert(int a){
dao.insert(a);
}
}

junit을 위한 테스트는 아래와 같이 작성하였습니다.

1
2
3
4
5
6
7
8
9
10
public class TestServiceTest { 

TestService service = new TestService();

@Test
public void TestServiceTest() {
dao.insert(1);
assertTrue(true);
}
}

실행을 해보니 TestService 안의 TestDao가 Null이라는
NullPointerException 이발생하였습니다.

spring에서 junit을 사용하는 방식이 따로 있는가 싶어
인터넷을 검색해보니, 아래와 같은 선언을 해야한다는 점을 확인하였습니다.

1
2
3
4
5
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {
"file:src/main/resources/spring/context-*.xml",
"file:src/main/resources/spring/*-servlet.xml"
})

물론, contextConfiguration에 대한부분은 단번에 프로젝트 설정에 따라 변경이 필요해야한다는 것을 알 수 있었습니다.
또한, xml 을 여러개 지정할 수도 있습니다.

해당 내용을 추가하여 아래와 같이 unit test class를 수정하였으나,
여전히 동일한 문제가 발생하고 있었습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {
"file:src/main/resources/spring/context-*.xml",
"file:src/main/resources/spring/*-servlet.xml"
})
public class TestServiceTest {

TestService service = new TestService();

@Test
public void TestServiceTest() {
dao.insert(1);
assertTrue(true);
}
}

리서치를 해보다가 결론적으론 TestService service에 @Autowired를 해주었어야 했습니다.
Bean을 등록하면서, 의존관계에 있는 다른 Bean들을 등록하게 되는 구조로 추측됩니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {
"file:src/main/resources/spring/context-*.xml",
"file:src/main/resources/spring/*-servlet.xml"
})
public class TestServiceTest {

@Autowired
TestService service;

@Test
public void TestServiceTest() {
dao.insert(1);
assertTrue(true);
}
}

#spring,#springframework,#junit,#junit4,#test,#NullPointerException, #NPE,#테스트,#스프링,#프레임워크