Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use assertTrue?

I have:

package com.darlik.test;

import org.junit.Assert;

public class Test {

    public static void main(String[] args) {
        assertTrue(1, 2);
    }

}

package with org.junit is set and working but in line with assertTrue i have error:

The method assertTrue(int, int) is undefined for the type Test

Why? I use Eclipse.

like image 684
darlik Avatar asked Aug 17 '14 18:08

darlik


People also ask

How do you write assertTrue in JUnit?

Assert class in case of JUnit 4 or JUnit 3 to assert using assertTrue method. Assertions. assertTrue() checks if supplied boolean condition is true. In case, condition is false, it will through AssertError.

How do you assert assertTrue in selenium?

AssertTrue() Assertion verifies the boolean value returned by a condition. If the boolean value is true, then assertion passes the test case, and if the boolean value is false, then assertion aborts the test case by an exception. Syntax of AssertTrue() method is given below: Assert.

What does assertTrue return?

assertTrue() in Python is a unittest library function that is used in unit testing to compare test value with true. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is true then assertTrue() will return true else return false.


3 Answers

assertTrue is based on a single boolean condition. For example

assertTrue(1 == 2);

You need to import the statement statically to use

import static org.junit.Assert.assertTrue;

Typically, however assertEquals is used when comparing 2 parameters, e.g.

public class MyTest {

   @Test
   public void testAssert() throws Exception {
        assertEquals(1, 2);
   }
}
like image 88
Reimeus Avatar answered Oct 16 '22 10:10

Reimeus


You have to specify the class that defines that method:

Assert.assertTrue(condition);

Furthermore you're calling the method with 2 parameters which makes no sense. assertTrue expects a single boolean expression.

Although you can also do this by using a static import:

import static org.junit.Assert.*;

which will allow you to call it as assertTrue(condition); instead.

like image 32
Jeroen Vannevel Avatar answered Oct 16 '22 10:10

Jeroen Vannevel


From the doc : assertTrue(boolean) or assertTrue(String, boolean) if you want to add a message.

AssertTrue assert that a condition is true, you still have to code such condition for it to be evaluated at runtime.

like image 43
m4rtin Avatar answered Oct 16 '22 12:10

m4rtin