Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Promblem with JUnit testing

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Promblem with JUnit testing

    Hello! I been trying to learn JUnit testning but I have problems with how to do it. Does anyone have time to do it and explain? This is a void method so it does not return anything so how do I test it?

    public void randomStarter(){ // Set this.currentPlayer to 1 or 2 based on random.
    Random randomizer = new Random();
    this.currentPlayer = randomizer.nextInt(2)+1;
    }

  2. #2
    Junior Member
    Join Date
    Dec 2021
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Promblem with JUnit testing

    You can use the assert keyword to do this:

    @Test
    public void randomStarter(){ // Set this.currentPlayer to 1 or 2 based on random.
    Random randomizer = new Random();
    this.currentPlayer = randomizer.nextInt(2)+1;

    assert (this.currentPlayer == 1 || this.currentPlayer == 2);
    }

    If you get anything other than 1 or 2 assert will throw an AssertionError Exception

  3. The Following User Says Thank You to armela For This Useful Post:

    19xx62xx14 (April 7th, 2022)

Similar Threads

  1. Help with JUnit testing
    By vysero in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 13th, 2018, 07:41 PM
  2. Promblem with CGPA calculator
    By Surdyehy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 23rd, 2018, 07:25 AM
  3. Using Junit testing to test
    By egreenhorn in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 15th, 2014, 11:29 AM
  4. NullPointerException with JUnit Testing
    By 3sbwya in forum Exceptions
    Replies: 3
    Last Post: February 28th, 2014, 07:47 PM
  5. JUnit Testing
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2012, 11:19 AM