2222import java .util .Arrays ;
2323import java .util .HashMap ;
2424import java .util .List ;
25+ import java .util .Locale ;
2526import java .util .Map ;
2627import java .util .concurrent .ExecutorService ;
2728import java .util .concurrent .Executors ;
3132
3233import org .hamcrest .Matchers ;
3334import org .junit .Before ;
35+ import org .junit .Rule ;
3436import org .junit .Test ;
37+ import org .junit .rules .ExpectedException ;
3538
3639import org .springframework .beans .DirectFieldAccessor ;
3740import org .springframework .context .ApplicationContextException ;
@@ -64,6 +67,8 @@ public class ScriptTemplateViewTests {
6467
6568 private StaticWebApplicationContext wac ;
6669
70+ @ Rule
71+ public ExpectedException expectedException = ExpectedException .none ();
6772
6873 @ Before
6974 public void setup () {
@@ -75,15 +80,20 @@ public void setup() {
7580 }
7681
7782
83+ @ Test
84+ public void missingTemplate () throws Exception {
85+ this .view .setUrl (RESOURCE_LOADER_PATH + "missing.txt" );
86+ this .view .setEngine (mock (InvocableScriptEngine .class ));
87+ this .configurer .setRenderFunction ("render" );
88+ this .view .setApplicationContext (this .wac );
89+ assertFalse (this .view .checkResource (Locale .ENGLISH ));
90+ }
91+
7892 @ Test
7993 public void missingScriptTemplateConfig () throws Exception {
80- try {
81- this .view .setApplicationContext (new StaticApplicationContext ());
82- fail ("Should have thrown ApplicationContextException" );
83- }
84- catch (ApplicationContextException ex ) {
85- assertTrue (ex .getMessage ().contains ("ScriptTemplateConfig" ));
86- }
94+ this .expectedException .expect (ApplicationContextException .class );
95+ this .view .setApplicationContext (new StaticApplicationContext ());
96+ this .expectedException .expectMessage (contains ("ScriptTemplateConfig" ));
8797 }
8898
8999 @ Test
@@ -159,53 +169,37 @@ public void nonSharedEngine() throws Exception {
159169
160170 @ Test
161171 public void nonInvocableScriptEngine () throws Exception {
162- try {
163- this .view .setEngine (mock (ScriptEngine .class ));
164- fail ("Should have thrown IllegalArgumentException" );
165- }
166- catch (IllegalArgumentException ex ) {
167- assertThat (ex .getMessage (), containsString ("instance" ));
168- }
172+ this .expectedException .expect (IllegalArgumentException .class );
173+ this .view .setEngine (mock (ScriptEngine .class ));
174+ this .expectedException .expectMessage (contains ("instance" ));
169175 }
170176
171177 @ Test
172178 public void noRenderFunctionDefined () {
173179 this .view .setEngine (mock (InvocableScriptEngine .class ));
174- try {
175- this .view .setApplicationContext (this .wac );
176- fail ("Should have thrown IllegalArgumentException" );
177- }
178- catch (IllegalArgumentException ex ) {
179- assertThat (ex .getMessage (), containsString ("renderFunction" ));
180- }
180+ this .expectedException .expect (IllegalArgumentException .class );
181+ this .view .setApplicationContext (this .wac );
182+ this .expectedException .expectMessage (contains ("renderFunction" ));
181183 }
182184
183185 @ Test
184186 public void engineAndEngineNameBothDefined () {
185187 this .view .setEngine (mock (InvocableScriptEngine .class ));
186188 this .view .setEngineName ("test" );
187189 this .view .setRenderFunction ("render" );
188- try {
189- this .view .setApplicationContext (this .wac );
190- fail ("Should have thrown IllegalArgumentException" );
191- }
192- catch (IllegalArgumentException ex ) {
193- assertThat (ex .getMessage (), containsString ("'engine' or 'engineName'" ));
194- }
190+ this .expectedException .expect (IllegalArgumentException .class );
191+ this .view .setApplicationContext (this .wac );
192+ this .expectedException .expectMessage (contains ("'engine' or 'engineName'" ));
195193 }
196194
197195 @ Test
198196 public void engineSetterAndNonSharedEngine () {
199197 this .view .setEngine (mock (InvocableScriptEngine .class ));
200198 this .view .setRenderFunction ("render" );
201199 this .view .setSharedEngine (false );
202- try {
203- this .view .setApplicationContext (this .wac );
204- fail ("Should have thrown IllegalArgumentException" );
205- }
206- catch (IllegalArgumentException ex ) {
207- assertThat (ex .getMessage (), containsString ("sharedEngine" ));
208- }
200+ this .expectedException .expect (IllegalArgumentException .class );
201+ this .view .setApplicationContext (this .wac );
202+ this .expectedException .expectMessage (contains ("sharedEngine" ));
209203 }
210204
211205 @ Test
0 commit comments