質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
JUnit

JUnitは、Javaで開発されたプログラムのユニットテストを行うためのアプリケーションフレームワークです。簡単にプログラムのユニットテストを自動化することができ、結果もわかりやすく表示されるため効率的に開発時間を短縮できます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

ソフトウェアテスト

ソフトウェアテストは、プログラムを実行し、要求通りに正しく動作が行えているかどうか確認する作業です。プログラム中のバグをできる限り多く発見することを目標として行われます。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

Q&A

0回答

2621閲覧

MockMVCでServletExceptionが発生し、テストができない。

snowshink

総合スコア138

JUnit

JUnitは、Javaで開発されたプログラムのユニットテストを行うためのアプリケーションフレームワークです。簡単にプログラムのユニットテストを自動化することができ、結果もわかりやすく表示されるため効率的に開発時間を短縮できます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

ソフトウェアテスト

ソフトウェアテストは、プログラムを実行し、要求通りに正しく動作が行えているかどうか確認する作業です。プログラム中のバグをできる限り多く発見することを目標として行われます。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

0グッド

0クリップ

投稿2020/12/30 13:27

Eclipse 2020
Jetty 7
Spring 4.1.2
JUnit4 5.
(thymeleaf-spring4) *当初は入れない想定

#やりたいこと
Controllerクラスの結合(システム?)テスト

##やったこと
テストクラスの生成
テスト対象

Java

1package controller; 2 3import org.springframework.stereotype.Controller; 4import org.springframework.web.bind.annotation.RequestMapping; 5import org.springframework.web.servlet.ModelAndView; 6 7@Controller 8@RequestMapping("/hello") 9public class HelloController { 10 @RequestMapping 11 public ModelAndView hello() { 12 return new ModelAndView("hello"); 13 } 14}

テストクラス

Java

1package controller; 2import 省略 3 4 5@RunWith(SpringJUnit4ClassRunner.class) 6@ContextHierarchy({ 7 @ContextConfiguration(value = "classpath:testContext.xml") 8}) 9@WebAppConfiguration 10public class HelloControllerTest { 11 12 13 MockMvc mockMVC; 14 15 @Before 16 public void setUp() throws Exception { 17 System.out.println("setup"); 18 MockitoAnnotations.initMocks(this); 19 mockMVC = MockMvcBuilders.standaloneSetup(new HelloController()).build(); 20 } 21 22 @Test 23 public void test() throws Exception { 24 mockMVC.perform(get("/hello")) 25 .andExpect(status().isOk()); 26 } 27 28} 29

testContext.xml

XML

1<beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:mvc="http://www.springframework.org/schema/mvc" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 https://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/mvc 9 https://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 10 http://www.springframework.org/schema/context 11 https://www.springframework.org/schema/context/spring-context-3.1.xsd"> 12 13 <!-- Enables the Spring MVC @Controller programming model --> 14 <mvc:annotation-driven /> 15 16 <!-- Default Servlet --> 17 <mvc:default-servlet-handler /> 18 19 <!-- Static Resources --> 20 <mvc:resources mapping="/resources/**" location="/public/" cache-period="#{60 * 60}" /> 21 22 <context:property-placeholder location="classpath:*.properties" /> 23 24 <context:component-scan base-package="controller" /> 25 26 <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 27 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 28 <property name="prefix" value="/WEB-INF/views/"/> 29 <property name="suffix" value=".jsp"/> 30 </bean> 31 32 <bean 33 id="viewResolver_" 34 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 35 36 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 37 <property name="prefix" value="/WEB-INF/views/" /> 38 <property name="suffix" value=".jsp" /> 39 </bean> 40 41 <bean class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver" id="webTemplateResolver"> 42 <property name="prefix" value="WEB-INF/views/" /> 43 <property name="suffix" value=".jsp" /> 44 <property name="templateMode" value="HTML" /> 45 <property name="characterEncoding" value="UTF-8" /> 46 <property name="order" value="2" /> 47 <property name="cacheable" value="false" /> 48 </bean> 49 50</beans>

Resolverが3つあるのは試行した結果です。

###起きたエラー

text

1javax.servlet.ServletException: Circular view path [hello]: would dispatch back to the current handler URL [/hello] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) 2 at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205)

###ためしたこと

  1. 上のResolverの追加

(初期)viewResolver Only --> ☓
Add viewResolver_ --> ☓
Add WebTemplateResolver --> ☓

  1. テストクラスのアノテーションをはずす

参考にしている翔泳社の本では、なくていいと書かれていたので。

###プロジェクト情報
pom.xml

XML

1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>sprng-mvc-test</groupId> 4 <artifactId>sprng-mvc-test</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 <name>springMVCTest</name> 8 9 <properties> 10 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 11 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 12 13 <!-- Java version --> 14 <java.version>8</java.version> 15 <maven.compiler.source>${java.version}</maven.compiler.source> 16 <maven.compiler.target>${java.version}</maven.compiler.target> 17 18 <!-- Spring --> 19 <spring.version>4.1.2.RELEASE</spring.version> 20 21 <!-- Test --> 22 <junit.version>5.6.0</junit.version> 23 24 <log4j.version>1.2.17</log4j.version> 25 26 27 </properties> 28 29 <dependencies> 30 <dependency> 31 <groupId>javax.servlet</groupId> 32 <artifactId>jstl</artifactId> 33 <version>1.2</version> 34 </dependency> 35 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-core</artifactId> 39 <version>${spring.version}</version> 40 </dependency> 41 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-web</artifactId> 45 <version>${spring.version}</version> 46 </dependency> 47 48 <dependency> 49 <groupId>org.springframework</groupId> 50 <artifactId>spring-webmvc</artifactId> 51 <version>${spring.version}</version> 52 </dependency> 53 54 <dependency> 55 <groupId>org.springframework</groupId> 56 <artifactId>spring-test</artifactId> 57 <version>${spring.version}</version> 58 <scope>test</scope> 59 </dependency> 60 61 62 63 <dependency> 64 <groupId>io.spring.platform</groupId> 65 <artifactId>platform-bom</artifactId> 66 <version>2.0.8.RELEASE</version> 67 <type>pom</type> 68 <scope>import</scope> 69 </dependency> 70 71 <dependency> 72 <groupId>org.thymeleaf</groupId> 73 <artifactId>thymeleaf-spring4</artifactId> 74 <version>3.0.0.RELEASE</version> 75 </dependency> 76 77 <dependency> 78 <groupId>log4j</groupId> 79 <artifactId>log4j</artifactId> 80 <version>${log4j.version}</version> 81 </dependency> 82 83 84 <dependency> 85 <groupId>org.hibernate.javax.persistence</groupId> 86 <artifactId>hibernate-jpa-2.0-api</artifactId> 87 <version>1.0.1.Final</version> 88 </dependency> 89 90 <dependency> 91 <groupId>org.projectlombok</groupId> 92 <artifactId>lombok</artifactId> 93 <version>1.18.16</version> 94 <scope>provided</scope> 95 </dependency> 96 97 <dependency> 98 <groupId>javax.validation</groupId> 99 <artifactId>validation-api</artifactId> 100 <version>2.0.1.FINAL</version> 101 <scope>provided</scope> 102 </dependency> 103 104 <dependency> 105 <groupId>javax.xml.bind</groupId> 106 <artifactId>jaxb-api</artifactId> 107 <version>2.3.0</version> 108 </dependency> 109 110 <dependency> 111 <groupId>org.apache.taglibs</groupId> 112 <artifactId>taglibs-standard-impl</artifactId> 113 <version>1.2.5</version> 114 </dependency> 115 116 <dependency> 117 <groupId>org.hibernate</groupId> 118 <artifactId>hibernate-validator</artifactId> 119 <version>5.2.4.Final</version> 120 </dependency> 121 122<!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> 123 <dependency> 124 <groupId>org.springframework</groupId> 125 <artifactId>spring-test</artifactId> 126 <version>${spring.version}</version> 127 <scope>test</scope> 128 </dependency> 129 130<!-- https://mvnrepository.com/artifact/org.springframework/spring-test-mvc --> 131 <dependency> 132 <groupId>org.springframework</groupId> 133 <artifactId>spring-test-mvc</artifactId> 134 <version>1.0.0.M2</version> 135 <scope>test</scope> 136 </dependency> 137 138 <dependency> 139 <groupId>org.mockito</groupId> 140 <artifactId>mockito-core</artifactId> 141 <version>1.10.19</version> 142 <scope>test</scope> 143 </dependency> 144 145 <dependency> 146 <groupId>junit</groupId> 147 <artifactId>junit</artifactId> 148 <version>4.12</version> 149 <scope>test</scope> 150 </dependency> 151 152 153 </dependencies> 154 155 <repositories> 156 <repository> 157 <id>spring-maven-milestone</id> 158 <name>Spring Maven Milestone Repository</name> 159 <url>https://maven.springframework.org/milestone</url> 160 </repository> 161 </repositories> 162 163 <build> 164 <sourceDirectory>main/java</sourceDirectory> 165 <testSourceDirectory>test/java</testSourceDirectory> 166 <plugins> 167 <plugin> 168 <artifactId>maven-compiler-plugin</artifactId> 169 <version>3.8.0</version> 170 <configuration> 171 <release>13</release> 172 </configuration> 173 </plugin> 174 <plugin> 175 <artifactId>maven-war-plugin</artifactId> 176 <version>3.2.3</version> 177 <configuration> 178 <warSourceDirectory>WebContent</warSourceDirectory> 179 </configuration> 180 </plugin> 181 </plugins> 182 </build> 183</project>

フォルダ構成
フォルダ構成

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問