Yuri Molodyko
Yuri Molodyko

Reputation: 600

How to replace @SpringBootTest on @ContextConfiguration (Java Spring)?

I have the test class, with @SpringBootTest:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnviroment = SpringBootTest.WebEnviroment.RANDOM_PORT, classes = App.class)
public class MyTest {
...
}

I need to replace @SpringBootTest, on @ContextConfiguration, and i do it:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "my.xml", initializers = MyTest.MockJee.class)
public class MyTest {
...
}

But @SpringBootTest has parameter, which @ContextConfigruration hasn't:

webEnviroment = SpringBootTest.WebEnviroment.RANDOM_PORT

So if i can join @SpringBootTest and @ContextConfiguration, it will be nice. But SpringBootTest + ContextConfiguration create conflict (they create context both), so this is reason why i want to replace SpringBootTest. Question: How to make like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "my.xml", initializers = MyTest.MockJee.class, webEnviroment = SpringBootTest.WebEnviroment.RANDOM_PORT)
public class MyTest {
...
}

Upvotes: 0

Views: 806

Answers (1)

tsachev
tsachev

Reputation: 1161

It really depends on what you are trying to achieve. But one of the effects of using SpringBootTest.WebEnviroment.RANDOM_PORT is to actually set the property server.port to 0.

Upvotes: 1

Related Questions