【发布时间】:2018-03-09 04:31:23
【问题描述】:
我使用 Spring Initializr 生成了一个 Spring Boot Web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎,并打包为可执行 JAR 文件。
使用的技术:
Spring Boot 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat 嵌入 8.5.6、Maven 3、Java 8
我有这门课 我有这个junit测试类:
@ContextConfiguration(classes={TestPersistenceConfig.class})
@RunWith(SpringRunner.class)
@SpringBootTest
public class JdbcRemoteUnitRepositoryTests {
@Autowired
private JdbcRemoteUnitRepository repository;
@Autowired
@InjectMocks
private SmsConfig smsConfig;
@Autowired
@InjectMocks
private NextelSMSSender smsService;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testGetAllUnits() throws DataAccessException, SQLException {
Assert.assertTrue(true);
}
}
NextelSMSSender 类:
@Service("smsService")
public class NextelSMSSender {
public final static String OK_STATUS_CODE = "200";
@Autowired
SmsConfig smsConfig;
..
}
.
@Configuration
@PropertySource("sms-gateway.properties")
public class SmsConfig {
@Value("${sms.domainId}")
private String domainId;
@Value("${sms.gateway.url}")
private String gatewayUrl;
..
}
但似乎不是在模拟 objects 属性,因为当我打包应用程序时。我收到了这个错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field smsConfig in com.plats.bruts.NextelSMSSender required a bean of type 'com.plats.bruts.config.SmsConfig' that could not be found.
Action:
Consider defining a bean of type 'com.plats.bruts.config.SmsConfig' in your configuration.
我创建了一个名为TestSmsConfig的mock bean,尝试添加@Bean,但出现编译错误:
The annotation @Bean is disallowed for this
location
@Configuration
@Bean
public class TestSmsConfig {
@Value("fake.domainId")
private String domainId;
@Value("fake.sms.gateway.url")
private String gatewayUrl;
...
}
【问题讨论】:
标签: java spring maven spring-boot junit