Junit Test Application-25-unsuccessfulAuthentication JUnit 테스트

jaegeunsong97·2023년 8월 5일
0

Junit Bank Application 깃허브

Junit Bank Application 기록 노션

  • 코드 추가
@Test
     public void unsuccessfulAuthentication_test() throws Exception {
          // given
          LoginRequestDto loginRequestDto = new LoginRequestDto();
          loginRequestDto.setUsername("ssar");
          loginRequestDto.setPassword("12345");
          String reqeustBody = om.writeValueAsString(loginRequestDto);
          System.out.println("테스트 : " + reqeustBody);

          // when
          ResultActions resultActions = mvc
                    .perform(post("/api/login").content(reqeustBody).contentType(MediaType.APPLICATION_JSON));
          String reasponseBody = resultActions.andReturn().getResponse().getContentAsString();
          String jwtToken = resultActions.andReturn().getResponse().getHeader(JwtValueObject.HEADER);
          System.out.println("테스트 : " + reasponseBody);
          System.out.println("테스트 : " + jwtToken);

          // then
          resultActions.andExpect(status().isUnauthorized());
     }

하지만 전체를 돌리면 터진다.

왜냐하면 각각의 테스트를 돌릴 때마다. @BeforeEach가 발돌하기 때문이다. 이때 테스트는 독립적으로 수행되야하기 때문에 순서가 중요하지 않다. 따라서 테스트마다 롤백이 되야한다.

@Transactional // 추가
@ActiveProfiles("test")
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
public class JwtAuthenticationFilterTest extends DummyObject {

각 테스트가 진행되고 끝날때마다 롤백이 된다.

profile
블로그 이전 : https://medium.com/@jaegeunsong97

0개의 댓글