我有一个带有3个集成测试类的项目: A、B和C。我对代码进行了修改,作为这些更改的一部分,我在测试类A中添加了@MockBean。
下面是一个由每个Integration类扩展的类:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class, webEnvironment = RANDOM_PORT)
@TestPropertySource(locations = "classpath:application-test.yml")
@ActiveProfiles(profiles = {"default", "test"})
public abstract class IntegrationTest {
@Value("${local.server.port}")
private int serverPort;
@Autowired
private ObjectMapper objectMapper;
@Before
public void setUpIntegrationTest() {
RestAssured.port = serverPort;
RestAssured.config = RestAssuredConfig.config()
.logConfig(LogConfig.logConfig()
.enableLoggingOfRequestAndResponseIfValidationFails()
.enablePrettyPrinting(true))
.objectMapperConfig(objectMapperConfig()
.jackson2ObjectMapperFactory((cls, charset) -> objectMapper)
.defaultObjectMapperType(ObjectMapperType.JACKSON_2))
.jsonConfig(jsonConfig().numberReturnType(BIG_DECIMAL))
.redirect(new RedirectConfig().followRedirects(false));
}
}下面是一个具体的测试类:
import org.springframework.boot.test.mock.mockito.MockBean;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doNothing;
public class TestClassA extends IntegrationTest {
@MockBean
private SomeBean foo;
@Before
@Override
public void setUpIntegrationTest() {
super.setUpIntegrationTest();
doNothing().when(foo).fooMethod(any(SomeClass.class), any(SomeOtherClass.class));
}
@Test
public void testCaseX() {
given()
.body("{\"foo\": \"bar\"}")
.when()
.post("/some/path/")
.then()
.statusCode(OK.value());
}
}我试着用三种不同的方式进行测试:
导致: org.springframework.beans.BeanInstantiationException:未能实例化io.github.azagniotov.stubby4j.server.StubbyManager:工厂方法‘存根’抛出的异常;嵌套异常为java.net.BindException:已在使用的地址: java.net.BindException:已在使用的地址
Jetty是作为Stubby4j的一部分设置的,它以以下方式实例化为配置bean:
@Configuration
public class StubbyConfig {
@Bean
public StubbyManager stubby(final ResourceLoader resourceLoader) throws Exception {
Resource stubbyFile = resourceLoader.getResource("classpath:stubs/stubby.yml");
if (stubbyFile.exists()) {
Map<String, String> stubbyConfig = Maps.newHashMap();
stubbyConfig.put("disable_admin_portal", null);
stubbyConfig.put("disable_ssl", null);
File configFile = stubbyFile.getFile();
Future<List<StubHttpLifecycle>> stubLoadComputation =
ConcurrentUtils.constantFuture(new YAMLParser().parse(configFile.getParent(), configFile));
StubbyManager stubbyManager = new StubbyManagerFactory()
.construct(configFile, stubbyConfig, stubLoadComputation);
stubbyManager.startJetty();
return stubbyManager;
} else {
throw new FileNotFoundException("Could not load stubby.yml");
}
}
}我用两种不同的方式进行了一些调试,在行stubbyManager.startJetty();中放置了一个断点。
显然,我的问题是:为什么MockedBean注释会导致这种行为,我如何避免它?
提前谢谢。
当前项目设置:
发布于 2017-07-31 14:15:45
当我还没有做完的时候,我就开始使用Jetty:
if(!stubbyManager.statuses().contains(""Stubs portal configured at")
stubbyManager.startJetty();
else
stubbyManager.joinJetty();如果你能找到更好的解决办法,请告诉我
发布于 2021-12-13 11:10:13
您可以使用Spring的SocketUtils类查找可用的TCP端口,然后将其传递到stubbyConfig映射中:
...
static final int PORT_RANGE_MIN = 2048;
static final int PORT_RANGE_MAX = 65535;
...
...
public StubbyConfig() {
this.stubPort = SocketUtils.findAvailableTcpPort(PORT_RANGE_MIN, PORT_RANGE_MAX);
}
...
@Bean
public StubbyManager stubby(final ResourceLoader resourceLoader) throws Exception {
...
if (stubbyFile.exists()) {
...
stubbyConfig.put("stubs", String.valueOf(stubsPort));
...
}
}
...
public int getStubsPort() {
return this.stubPort;
}然后,因为StubbyConfig类上有端口getter,所以在运行集成测试时可以将其传递给RestAssured.port。
https://stackoverflow.com/questions/41491708
复制相似问题