2017-09-27 22 views
2

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; 

     ... 
} 

答えて

1

をしますが、表示されません。 @Beanをテストアプリケーションのコンテキストに提供します。

また、@ InjectMocksを間違って使用しています。これは、テスト対象のクラス(SmsConfigではなく、NextelSMSSenderクラス)に模擬オブジェクトを挿入するために使用する必要があります。

解決するには、構成クラスに@Beanメソッドを追加してSmsConfig Beanインスタンスを提供します。

テストクラスのSmsConfig変数の@InjectMocksを@MockBeanに置き換えます。 SmsConfigはautowired mockオブジェクトであり、テスト対象のクラスではありません。

節を参照してください41.3.4 - 詳細はSpring Boot Testing Featuresの:

Spring Boot includes a @MockBean annotation that can be used to define a Mockito mock for a bean inside your ApplicationContext. You can use the annotation to add new beans, or replace a single existing bean definition. The annotation can be used directly on test classes, on fields within your test, or on @Configuration classes and fields. When used on a field, the instance of the created mock will also be injected. Mock beans are automatically reset after each test method.

0

OKなので、これはあなたの設定です:

@Configuration 
    @PropertySource("sms-gateway.properties") 
    public class SmsConfig { 

    @Value("${sms.domainId}") 
    private String domainId; 

    @Value("${sms.gateway.url}") 
    private String gatewayUrl; 
.. 
} 

あなたのテストでその構成を使用したい場合は、設定クラスに追加する必要があります。

@ContextConfiguration(classes={TestPersistenceConfig.class, SmsConfig.class}) 

そして、@Autowired oとして追加しないでくださいどんなものでも。