How to stub methods sooner than @Before block?












0















I initialize my ViewModel at top level, and as it initializes, it calls its init {} block, which then calls methods that I stub in @Before block.



Although all the tests are passed, NullPointerExceptions is thrown in methods that I call in init {}. I tried to lateinit my ViewModel in @Before block. It didn't work.



// AViewModelTest.kt
private val repoMock: ARepository = mock()
private val viewModel: AViewModel = AViewModel(repoMock)

@Before
fun setup() {
// method stubbing
`when`(repoMock.getSmth()).thenReturn(response)
}


// AViewModel.kt
constructor(repo: ARepository) {}

init {
onStartLoading(repo)
}

fun onStartLoading(repo: ARepository) {
val response = repo.getSmth()
handleResponse(response) // response is null here -> NullPointerException
}









share|improve this question




















  • 1





    Could you add some code as well?

    – tynn
    Nov 23 '18 at 12:03











  • You could try the @BeforeClass annotation: stackoverflow.com/questions/20295578/…

    – MasterQueue
    Nov 23 '18 at 12:13






  • 1





    Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.

    – GhostCat
    Nov 23 '18 at 13:27











  • @tynn updated the question

    – madim
    Nov 26 '18 at 3:36






  • 1





    init blocks are equivalent to constructor bodies, and so they will always be called before any methods. This is expected behavior. repo is null when onStartLoading(repo) is called from init.

    – Brucelet
    Nov 26 '18 at 3:37
















0















I initialize my ViewModel at top level, and as it initializes, it calls its init {} block, which then calls methods that I stub in @Before block.



Although all the tests are passed, NullPointerExceptions is thrown in methods that I call in init {}. I tried to lateinit my ViewModel in @Before block. It didn't work.



// AViewModelTest.kt
private val repoMock: ARepository = mock()
private val viewModel: AViewModel = AViewModel(repoMock)

@Before
fun setup() {
// method stubbing
`when`(repoMock.getSmth()).thenReturn(response)
}


// AViewModel.kt
constructor(repo: ARepository) {}

init {
onStartLoading(repo)
}

fun onStartLoading(repo: ARepository) {
val response = repo.getSmth()
handleResponse(response) // response is null here -> NullPointerException
}









share|improve this question




















  • 1





    Could you add some code as well?

    – tynn
    Nov 23 '18 at 12:03











  • You could try the @BeforeClass annotation: stackoverflow.com/questions/20295578/…

    – MasterQueue
    Nov 23 '18 at 12:13






  • 1





    Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.

    – GhostCat
    Nov 23 '18 at 13:27











  • @tynn updated the question

    – madim
    Nov 26 '18 at 3:36






  • 1





    init blocks are equivalent to constructor bodies, and so they will always be called before any methods. This is expected behavior. repo is null when onStartLoading(repo) is called from init.

    – Brucelet
    Nov 26 '18 at 3:37














0












0








0








I initialize my ViewModel at top level, and as it initializes, it calls its init {} block, which then calls methods that I stub in @Before block.



Although all the tests are passed, NullPointerExceptions is thrown in methods that I call in init {}. I tried to lateinit my ViewModel in @Before block. It didn't work.



// AViewModelTest.kt
private val repoMock: ARepository = mock()
private val viewModel: AViewModel = AViewModel(repoMock)

@Before
fun setup() {
// method stubbing
`when`(repoMock.getSmth()).thenReturn(response)
}


// AViewModel.kt
constructor(repo: ARepository) {}

init {
onStartLoading(repo)
}

fun onStartLoading(repo: ARepository) {
val response = repo.getSmth()
handleResponse(response) // response is null here -> NullPointerException
}









share|improve this question
















I initialize my ViewModel at top level, and as it initializes, it calls its init {} block, which then calls methods that I stub in @Before block.



Although all the tests are passed, NullPointerExceptions is thrown in methods that I call in init {}. I tried to lateinit my ViewModel in @Before block. It didn't work.



// AViewModelTest.kt
private val repoMock: ARepository = mock()
private val viewModel: AViewModel = AViewModel(repoMock)

@Before
fun setup() {
// method stubbing
`when`(repoMock.getSmth()).thenReturn(response)
}


// AViewModel.kt
constructor(repo: ARepository) {}

init {
onStartLoading(repo)
}

fun onStartLoading(repo: ARepository) {
val response = repo.getSmth()
handleResponse(response) // response is null here -> NullPointerException
}






android junit kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 3:43







madim

















asked Nov 23 '18 at 11:59









madimmadim

314418




314418








  • 1





    Could you add some code as well?

    – tynn
    Nov 23 '18 at 12:03











  • You could try the @BeforeClass annotation: stackoverflow.com/questions/20295578/…

    – MasterQueue
    Nov 23 '18 at 12:13






  • 1





    Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.

    – GhostCat
    Nov 23 '18 at 13:27











  • @tynn updated the question

    – madim
    Nov 26 '18 at 3:36






  • 1





    init blocks are equivalent to constructor bodies, and so they will always be called before any methods. This is expected behavior. repo is null when onStartLoading(repo) is called from init.

    – Brucelet
    Nov 26 '18 at 3:37














  • 1





    Could you add some code as well?

    – tynn
    Nov 23 '18 at 12:03











  • You could try the @BeforeClass annotation: stackoverflow.com/questions/20295578/…

    – MasterQueue
    Nov 23 '18 at 12:13






  • 1





    Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.

    – GhostCat
    Nov 23 '18 at 13:27











  • @tynn updated the question

    – madim
    Nov 26 '18 at 3:36






  • 1





    init blocks are equivalent to constructor bodies, and so they will always be called before any methods. This is expected behavior. repo is null when onStartLoading(repo) is called from init.

    – Brucelet
    Nov 26 '18 at 3:37








1




1





Could you add some code as well?

– tynn
Nov 23 '18 at 12:03





Could you add some code as well?

– tynn
Nov 23 '18 at 12:03













You could try the @BeforeClass annotation: stackoverflow.com/questions/20295578/…

– MasterQueue
Nov 23 '18 at 12:13





You could try the @BeforeClass annotation: stackoverflow.com/questions/20295578/…

– MasterQueue
Nov 23 '18 at 12:13




1




1





Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.

– GhostCat
Nov 23 '18 at 13:27





Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you.

– GhostCat
Nov 23 '18 at 13:27













@tynn updated the question

– madim
Nov 26 '18 at 3:36





@tynn updated the question

– madim
Nov 26 '18 at 3:36




1




1





init blocks are equivalent to constructor bodies, and so they will always be called before any methods. This is expected behavior. repo is null when onStartLoading(repo) is called from init.

– Brucelet
Nov 26 '18 at 3:37





init blocks are equivalent to constructor bodies, and so they will always be called before any methods. This is expected behavior. repo is null when onStartLoading(repo) is called from init.

– Brucelet
Nov 26 '18 at 3:37












1 Answer
1






active

oldest

votes


















0














In the end, lateinit in @Before did the thing. The problem occurred because I had an implicit reference to Android Context in my ViewModel.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446315%2fhow-to-stub-methods-sooner-than-before-block%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    In the end, lateinit in @Before did the thing. The problem occurred because I had an implicit reference to Android Context in my ViewModel.






    share|improve this answer






























      0














      In the end, lateinit in @Before did the thing. The problem occurred because I had an implicit reference to Android Context in my ViewModel.






      share|improve this answer




























        0












        0








        0







        In the end, lateinit in @Before did the thing. The problem occurred because I had an implicit reference to Android Context in my ViewModel.






        share|improve this answer















        In the end, lateinit in @Before did the thing. The problem occurred because I had an implicit reference to Android Context in my ViewModel.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 28 '18 at 3:42

























        answered Nov 26 '18 at 9:38









        madimmadim

        314418




        314418






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446315%2fhow-to-stub-methods-sooner-than-before-block%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...