Mam klasy domeny, która rozszerza klasy abstrakcyjna, który wstrzykuje usługi spring security core plugin.Jak wyśmiać usługę wstrzykniętą w klasie domeny z klasy kontrolnej kontrolera?
class Extra extends WithOwner {
String name
}
abstract class WithOwner {
transient springSecurityService
User user
def getCurrentUser() {
return springSecurityService.currentUser
}
def beforeValidate() {
if(!user) {
user = getCurrentUser()
}
}
Boolean isLoggedUserTheOwner(){
return (user?.id == getCurrentUser()?.id)
}
}
Chcę zaimplementować test kontrolera.
@TestFor(ExtraController)
@Mock([Extra, User, UserRole, Role])
class ExtraControllerTests {
void testEdit() {
def utils = new TestUtils()
def user1 = utils.saveUser1()
populateValidParams(params)
def extra = new Extra(params)
extra.user = user1
assert extra.save() != null
params.id = extra.id
def model = controller.edit() // Line 69
assert model.extraInstance == extra
}
}
Jeśli uruchomić powyższą próbę uzyskać:
test-app ExtraController.testEdit --unit --echoOut | Uruchamianie 1 testu jednostkowego ... 1 z 1 - Wyjście z testEdit - | Błąd: testEdit (com.softamo.movilrural.ExtraControllerTests) | wyjątek java.lang.NullPointerException: nie można uzyskać właściwości 'currentUser' na obiekcie zerowym na com.softamo.movilrural.WithOwner.getCurrentUser (WithOwner.groovy: 8) na com.softamo.movilrural.WithOwner.isLoggedUserTheOwner (WithOwner.groovy: 18) na com.softamo.movilrural.ExtraController.edit (ExtraController.groovy: 39) na com.softamo.movilrural.ExtraControllerTests.testEdit (ExtraControllerTests.groovy: 69) | Zakończono test 1 jednostka, 1 nie powiodło się w 853ms
Próbowałem bezskutecznie drwić służby bezpieczeństwa takiego:
Extra.metaClass.springSecurityService = new MockSpringSecurityService(user1)
lub nawet szyderczy sposób
Extra.metaClass.getCurrentUser = { return user1 }
Jakiś pomysł jak mogę obejść ten problem.
Witam! Utknąłem z tym samym problemem. Czy opracowałeś rozwiązanie dla swojego pytania? A może masz boczną stronę do wstrzyknięcia usługi wewnątrz obiektu domeny bez automatycznego testowania jednostki? – snowindy
Powodzenia w tym problemie?, Z tym samym problemem. –