Tag Archives: spring-framework

Configure UnitTesting with Autowired in Spring context

/* Vietnamese:  Một bài viết trình bày cách viết Unit Test sử dụng Dependency-Injection(Autowiring) trong Spring Framework. */

Unit-testing is surely not a new idea, since it was proposed around the 1980s. But it’s still one of the core concepts of Test Driven Development, Agile programing, Scrum,… & some other “hot” programming methodology that arise recently. In “Clean code”, Robert C. Martin writes: “If you want to write a line of debug/log message, consider writing a test instead”.

Those ideas are really cool – if you want to know more about how writing tests stop regression bugs, improve productivity & help the designs overall, go on & read “Clean code”. I will return to the topic here: How to write Unit test in Spring context.

The second I read those above lines of Martin, I decided to try it right away. But to do Unit test in a Spring web project, I need a way to inject my services into the test case (well, there’s a HUGE pile of services). The way is easy, but only if you know what to do.

It took me quite a time to make a running system.

Here’s how I did it:

package longle.action.services;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml",
"file:src/main/webapp/WEB-INF/actions-servlet.xml"})
public class ActionServiceImplTest {
	@Autowired
	private ActionService actionService;

	@Test
	public void testHelloAction() {
		assert actionService.helloUnitTest().equals("Hello Unit Test, hope we have a good time together!");
	}
}

Hope that should cover the topic

Source code can be found here: http://www.mediafire.com/file/ww79etrez40j4gl/action.zip

Reference: (not all of these links are referred in this article. But every of them help me “go near the truth”, so I put them all here.)

  1. http://stackoverflow.com/questions/7313983/test-case-by-junit-how-to-read-web-xml
  2. http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/testing.html
  3. http://stackoverflow.com/questions/6546871/spring-junit-testing
  4. http://stackoverflow.com/questions/5733959/junit-custom-runner-with-spring-application-context
  5. http://stackoverflow.com/questions/8502327/application-context-and-spring-context-is-same
  6. http://stackoverflow.com/questions/8666720/configuration-problems-unable-to-locate-spring-namespacehandler-for-xml-schema
  7. http://stackoverflow.com/questions/412717/how-to-use-maven-surefire-plug-in-with-different-groups-for-test-and-integration
  8. http://maven.apache.org/plugins/maven-surefire-plugin/
  9. http://mekongnet.ru/index.php?mod=News&sid=37510&stprint=1

Finding key issues of the framework

Website System

1. How to learn a framework fast?

2. To learn a framework, you should grasp the “key” issues. But how to know what is a key issue?

Those two questions are pinned in my head somehow.

Not long ago, I’m investigating MVC model in Spring Framework 3.0. Most of the concept is fine, but I have problems doing the work with Spring 3 annotation config. It just seems all the book out there mention about Spring 2.x syntax, which is “xml configuration”. It’s easy to understand because that Spring 3 just appear and it will take a relative time to make a book available. So my habit about following book guides was discarded aside. I must try to find another source.

According the advice of my seniors, I go to the Spring document & find most of the thing is already here. Enough details, though a bit complex to a not-quite-familiar-with-java coder. I struggle with those concept a bit. But by comparing the document to several examples, finally I got some “working things”, which is great.

But here comes the most complicated part: integrating between frameworks. In one of my projects, I should use Spring combined with Freemarker. I investigate, and luckily enough, find this page which tell me how to use Freemarker macro to perform bindings (it’s not an easy luck).

But WTH, I can’t find a way to use the formCheckboxes! I found no working sample, and I’m not alone.

Dozens people out there can’t find a way to use that macro.

And the solution?

Well, I write my own macros.

It took me quite a time before I relize that simple solution, and curse how stupid I am. Since the end results of freemarker is HTML, who cares if it was rendered by supported macro or customed macro?

When you know what actually happens inside that big system, the answer seems to be natural.


Still remember 2 questions at the beginning of this post?

I must says the truth, that I don’t know the best answer. But I have something that seems to work:

The solution depends on the needs. If you just use the surface of the libraries API as the common “players”, some popular tutorials might be enough. Parsing XML maybe a good example for this issue. With the same reason, I think with beginner, Grails (instead of Spring) is a good start, cause it requires little work to make a full-fledge simple application.

If you want to create a customized & not-so-popular web application, it requires more tons of work. And a solid background is preferable.

Annotation Validation in Spring Framework 3

/* Vietnamese: bài viết hướng dẫn một cách ngắn gọn cách thực hiện kiểm tra điều kiện của đối tượng (validation) bằng cách dùng ký hiệu (annotation) với Spring Framework */

Validation is not a problem in Spring Framework, at least, by the normal way.

But the “Validation by Annotation” is not that popular, since this utility was only available from Spring 3. Up to now I found no resource which give a short & comprehensive tutorial yet (at least to this time – 05/2011)

How to make annotation validation works in Spring Framework 3

1. Configure Maven to include Hibernate Validator

2. Setup default validator bean in config file:
[sourcecoce language="xml"]
<bean id=”validator” class=”org.springframework.validation.beanvalidation.LocalValidatorFactoryBean” />
[/sourcecode]

3. Add @annotation

Let’s see what is annotation validation in its action:

public class User implements Serializable {
        private long id;
        @NotNull(message=&quot;{user.name.notnull}
        @Length(max = 255, message=&quot;{user.name.length}&quot;)
	private String username;
	public void setUsername(String name) {
		this.username = name;
	}
	public int getUsername() {
		return username;
	}
}

Code for the controller:

@Controller
@RequestMapping(&quot;/administration/*&quot;)
public class AdministrationController {
    /**
     * For every request for this controller, this will
     * create a user instance for the form.
     */
    @ModelAttribute
    public User newRequest(@RequestParam(required=false) String id) {
        return (id != null ? userService.getUserById(id) : new User());
    }

    @RequestMapping(value=&quot;/editUser&quot;, method=RequestMethod.GET)
	public final String edit(@Valid User user, BindingResult result) throws Exception {

		if (user == null) {
			return &quot;redirect:/administration/manageUser&quot;;
		}

		return null;
	}

	@RequestMapping(value=&quot;/editUser&quot;, method=RequestMethod.POST)
	public final String updateUser(@Valid User user, BindingResult result) throws Exception {

		if (result.hasErrors()) {
			return null;
		}

		userService.updateUser(user);

		return &quot;redirect:/administration/manageUser&quot;;
	}
}

And in the end, for the jsp:

&lt;/pre&gt;
&lt;h1&gt;Edit Site User&lt;/h1&gt;
&lt;div&gt;
&lt;fieldset&gt;&lt;legend&gt;Edit Site User&lt;/legend&gt;
 &lt;input type=&quot;hidden&quot; name=&quot;userId&quot; value=&quot;${user.id}&quot; /&gt;
				&lt;p&gt;
					&lt;form:label for=&quot;id&quot; path=&quot;id&quot;&gt;Id&lt;/form:label&gt;
					&lt;br /&gt;
					&lt;form:input path=&quot;id&quot; readonly=&quot;true&quot; /&gt;
					&lt;font color=&quot;red&quot;&gt;&lt;form:errors path=&quot;id&quot; /&gt;
					&lt;/font&gt;
				&lt;/p&gt;
				&lt;p&gt;
					&lt;form:label for=&quot;username&quot; path=&quot;username&quot;&gt;User Name&lt;/form:label&gt;
					&lt;br /&gt;
					&lt;form:input path=&quot;username&quot; readonly=&quot;true&quot; /&gt;
					&lt;font color=&quot;red&quot;&gt;&lt;form:errors path=&quot;username&quot; /&gt;
					&lt;/font&gt;
				&lt;/p&gt;
&lt;/fieldset&gt;
&lt;/div&gt;
&lt;pre&gt;