版本:SpringBootTest與MockBean沒有返回我的期望
Java: 1.8
Spring Boot: 1.5.4.RELEASE
應用程序主:
@SpringBootApplication
public class SpringbootMockitoApplication implements CommandLineRunner {
@Autowired
MyCoolService myCoolService;
public static void main(String[] args) {
SpringApplication.run(SpringbootMockitoApplication.class, args);
}
@Override
public void run(String... strings) throws Exception {
System.out.println(myCoolService.talkToMe());
}
}
我的服務接口:
public interface MyCoolService {
public String talkToMe();
}
我的服務實現:
@Service
public class MyCoolServiceImpl implements MyCoolService {
@Override
public String talkToMe() {
return "Epic Win";
}
}
我的測試類:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMockitoApplicationTests {
@MockBean
private MyCoolService myCoolService;
@Test
public void test() {
when(myCoolService.talkToMe()).thenReturn("I am greater than epic");
}
}
預期輸出:我比史詩 實際輸出更大:空
我只是想取代與模擬的背景下,將返回「我的bean實例我比史詩更偉大「。我在這裏配置錯了嗎?
我跑了與上面提到的相同的類和相同的春季啓動版本的測試,並沒有問題,它工作正常。在你的'pom.xml'中,你是否添加了'spring-boot-starter-test'和'spring-boot-test'依賴項? –