私は明確なMaven Webアプリプロジェクトを持っています。jUnit を pom に追加します。
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<!-- jsoup HTML parser library @ http://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
</dependency>
</dependencies>
アプリには非常に単純なサーブレットが 1 つあります。このサーブレットの jUnit 4 テストを生成したいと考えています。そこで、サーブレット パッケージ > [ツール] > [テストの作成] をクリックします。この後、メソッドでテストクラスを取得します。
public void testProcessRequest() throws Exception {
System.out.println("processRequest");
HttpServletRequest request = null;
HttpServletResponse response = null;
MyServlet instance = new MyServlet(); // This is the problem
instance.processRequest(request, response);
// TODO review the generated test code and remove the default call to fail.
//fail("The test case is a prototype.");
}
MyServlet には下線が引かれ、エラーは「Java EE API がプロジェクトのクラスパスにありません」です。
pom で依存関係のスコープを変更しようとしましたが、問題はまだ存在します。