0

我在春天很新,我现在面临一个测试问题:

我有以下服务:

@Service
public class MyService {

public Integer getKey() {
    List<Integer> keys = getKeys(1);
    if (keys.size() == 1) {
        return keys.get(0);
    }
    throw new IllegalArgumentException("Error!");
}

... and a getKeys() method, which provides a list based ona  rest call...
}

我在另一个类中使用这个服务类:

@NoArgsConstructor
public class MyOtherClass extends MyClass {

@Autowired
private MyService myService;

....
@Override public KeyValue<Object, Object> doSomething(Object key, Object value) {
    if (conditionIsTrue(key, value)) {
            MyObject obj = new MyObject();
            myObject.setKey(keyService.getKey());  ----- here is always null the keyService
            .....
    } else {
        return KeyValue.pair(null, null);
    }
}

我尝试编写一个测试,但 MyService 始终为空..

@ActiveProfiles("my-test")
@SpringBootTest(classes = Application.class)
@Testcontainers
@Slf4j
public class MyTest extends TestContext {

@BeforeEach
void init(final TestInfo testInfo) {
   ....
}

@AfterEach
void deinit() {
    ....
}

@Test
public void myTest() {
....
}

如何将模拟 MyService 注入测试容器?

谢谢!

4

0 回答 0