版本: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'依赖项? –