Spring Initializrを使用して、埋め込みTomcat + Thymeleafテンプレートエンジンを使用してSpringブートWebアプリケーションを生成し、実行可能なJARファイルとしてパッケージ化しました。使用Junit/Spring Boot mockingクラス
技術:私が持っている
春ブーツ1.4.2.RELEASE、春4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcatの埋め込み8.5.6、Mavenの3、Javaの8
@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;
..
}
このクラス 私はこのJUnitテストクラスを持っています
。
@Configuration
@PropertySource("sms-gateway.properties")
public class SmsConfig {
@Value("${sms.domainId}")
private String domainId;
@Value("${sms.gateway.url}")
private String gatewayUrl;
..
}
しかし、私はアプリケーションをパッケージ化するのでオブジェクトのプロパティを嘲笑していないようです。 、私はTestSmsConfig
という名前のモックBeanを作成し
***************************
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.
@Bean
を追加しようとしましたが、私はコンパイルエラーました:私はこのエラーを得たあなたはsmsconfigのをオートワイヤリングしている
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;
...
}