简体   繁体   English

用于单元测试的spring-boot junit负载测试属性资源

[英]spring-boot junit load test property resource for unit test

I am using spring-boot-1.5 . 我正在使用spring-boot-1.5 Is there any way to load application.properties in src/test/resources during the unit test ? 单元测试期间,有什么方法可以在src / test / resources中加载application.properties吗? I know how to load it using integration test we can use @SpringBootTest or @ContextConfiguration but I would like to use application.properties during unit test. 我知道如何使用集成测试加载它,我们可以使用@SpringBootTest@ContextConfiguration,但是我想在单元测试期间使用application.properties。

Let me explain my scenario a bit, 让我解释一下我的情况,

@SpringBootApplication(exclude = { MongoAutoConfiguration.class, MongoDataAutoConfiguration.class })
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class BookBackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(BookBackendApplication.class, args);
    }
}

@Service
public class BookService {
    @Value("${book-list:ACTIVE}")
    private List<String> bookList = new ArrayList<>();
    @Value("${book-status:PURCHASING}")
    private String bookStatus;

    public BookResponse purchaseBook(BookRequest bookRequest) {
       if(bookRequest.getStatus().equals(bookStatus)) { //Here getting NPE while executing unit test
            ....
       }
    }
}

UNIT TEST 单元测试

@RunWith(SpringRunner.class)
public class BookServiceTest {

    @InjectMocks
    private BookService bookService;

  @Test
  public void testBookActivate() throws IOException {
      BookResponse bookResponse = bookService.purchaseBook(bookRequest);
      ...
  }
}

Properties are not loading while running this unit test so getting NPE in BookService.java. 在运行此单元测试时,属性未加载,因此在BookService.java中获得了NPE。 Is there any way to load src/test/resource/application.properties while running the unit test? 运行单元测试时,有什么方法可以加载src / test / resource / application.properties吗?

Any pointers or help would be really appreciable. 任何指针或帮助将是非常可观的。

You can use @TestPropertySource 您可以使用@TestPropertySource

See this page: override-default-spring-boot-application-properties-settings-in-junit-test 参见本页: 覆盖默认测试中的默认弹簧启动应用程序属性设置

Finally, I got an answer. 最后,我得到了答案。 I have used below code to set static fields while running mocks. 我使用下面的代码在运行模拟时设置静态字段。 ReflectionTestUtils resolved the issue. ReflectionTestUtils解决了该问题。

@RunWith(SpringRunner.class)
public class BookServiceTest {

    @InjectMocks
    private BookService bookService;

    @Before
    public void setup() {
        ReflectionTestUtils.setField(bookService, "bookStatus", "PURCHASING");
        ReflectionTestUtils.setField(bookService, "bookList", Arrays.asList("ACTIVE","ACTIVATING"));
    }

  @Test
  public void testBookActivate() throws IOException {
      BookResponse bookResponse = bookService.purchaseBook(bookRequest);
      ...
  }
}

Thanks a lot for the support guys. 非常感谢您的支持。 Let's keep helping others. 让我们继续帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM