0

Spring and Constructor Injection

asked 2017-02-13 12:42:46 +0800

Coderdood gravatar image Coderdood
1

updated 2017-02-13 12:48:03 +0800

Hello,

So far, I've been using @WireVariable to inject Spring beans on my controllers and it's known to work perfectly.

@WireVariable("someBean") 
private SomeBean someBean;

But here is my question... Is there a way to achieve the same result but with constructor injection? It's a known good practice to inject beans via constructor so it's easy to mock them on unit tests and such. Like this

private final SomeBean somebean;

public MyController(SomeBean someBean) {
    this.somebean = someBean;
}

Can this be achieved somehow or I simply cannot unit test a ZK Controller?

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-02-24 03:27:24 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2017-02-24 03:28:57 +0800

@WireVariable doesn't support contructor injection. This doesn't mean you "can't" test your controllers at all. As always you can provide a setter to set the bean from your test cases, or use reflection to access the private field. There are several utils to achieve this easily.

Besides an MVC controller (such as a SelectorComposer) is usually accessing components and their properties to produce a certain output. So your controller won't function anyway without a component tree to manipulate or it doesn't serve it's purpose. (if you are using an MVVM View Model this is different story, however you were talking about Controller as in MVC)

We recommend using Zats to test your controller in combination with your zul file in order to trigger events and verify the changes to the UI.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2017-02-13 12:42:46 +0800

Seen: 34 times

Last updated: Feb 24 '17

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More