2016-10-24 9 views
3

私はスプリングブート1.4で簡単なテストを実行できません。私は公式サイトtesting-the-spring-mvc-sliceからチュートリアルに従ったが、私はそれを働かせなかった。Springブートテスト1.4でSpringBootConfigurationを見つけることができません

私は次のエラーを取得するたび:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test 

任意のアイデア、ヒントを?事前に

おかげ

編集:

これはこれはテスト

@RunWith(SpringRunner.class) 
@WebMvcTest(UserManagementController.class) 
public class UserManagementControllerTest { 

    @Autowired 
    private MockMvc mvc; 

    @Test 
    public void showUserView() throws Exception { 
     this.mvc.perform(get("/gs/users/getUsers")) 
      .andExpect(status().isOk()) 
      .andDo(print()); 
    } 
} 

は、私の視点からです

@Controller 
public class UserManagementController { 

@GetMapping(value = "/gs/users/getUsers") 
    public @ResponseBody String getAllUsers() { 
     return "test"; 
    } 
} 

コントローラが、それはまったく同じだですサイトからこの投稿のように。

@WebMvcTestを行います:

  • 自動設定Spring MVCの、ジャクソン、Gson、メッセージコンバータなど
  • ロード関連するコンポーネント(@Controller@RestController@JsonComponentなど)
  • 設定MockMVC

なぜ「スーパー」クラスを設定する必要があるのですか

+0

はいそうです他の注釈 – Jaiwo99

+0

のいずれかを使用する、述べています。しかし、公式文書では、あなたはそれを使うことができ、それで十分だと言います。 –

+0

あなたがテストしたクラスとテスト自体を提供すれば、それはより明確になる可能性があります –

答えて

11

The search algorithm works up from the package that contains the test until it finds a @SpringBootApplication or @SpringBootConfiguration annotated class. As long as you’ve structure your code in a sensible way your main configuration is usually found.

したがって、@ *テストでテストに注釈を付けました。実行され、サブクラスの設定がチェックされ、見つからなかった場合は例外がスローされます。

テストクラスのパッケージまたはサブパッケージにconfigを持つか、またはコンフィグクラスを直接@ContextConfigurationまたは@SpringBootTestに渡すか、または@SpringBootApplicationと注釈を付けたクラスが必要です。

@SpringBootApplicationによると、私はあなたが@WebMvcTestで言及した方法でコントローラをテストしました:アプリケーションが@SpringBootApplicationとして注釈付きクラスを持っていて、あなたが言及していない例外で失敗した場合、それは動作します。同じポイントについて

In this example, we’ve omitted classes which means that the test will first attempt to load @Configuration from any inner-classes, and if that fails, it will search for your primary @SpringBootApplication class.

Githubのdiscussion:それをあなたが言及した記事を再マーキングがあります。 MSGのような

Spring Boot Documentation

+0

説明のためにありがとう。 –

+0

素晴らしい、ありがとう –

+0

私は同じ問題があります。私はすべてのデータベースの内容を読み込み、簡単なテストが長時間続くので、私の主な生産構成を指差すのを避けたいと思います。@WebMvcTestは軽量の設定(db、最小ネットワーキング)をロードすると思っていたので、サービス層を模擬してコントローラロジックを素早くテストします。しかし、私は常に完全なスタックをテストし、DBオブジェクトなどを作成する必要があるコントローラをテストするように見えます – tequilacat

関連する問題