How to mock Eureka when doing Integration Tests in Spring?

后端 未结 2 1694
感情败类
感情败类 2021-02-20 03:21

I am running a simple Junit Testing a Controller in Spring Boot. The test code looks like this:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfigu         


        
相关标签:
2条回答
  • 2021-02-20 04:02

    You can set a system property for eureka.client.enabled=false for tests.

    If you're running the tests using gradle you can do this:

    tasks.withType(Test) {
        systemProperty 'eureka.client.enabled', 'false'
    }
    

    If you're running tests in an IDE then you'll have to set the system property there as well.

    0 讨论(0)
  • 2021-02-20 04:06

    Another solution is to disable the Eureka Client in your application.properties or application.yml file under test/resources

    applications.properties:

    eureka.client.enabled=false

    application.yml:

    eureka: client: enabled: false

    This has the benefit of not needing to remeber to include the system property for every JUnit test that requires disabling the Eureka Client.

    0 讨论(0)
提交回复
热议问题