I want to create simple java project with JUnit, so for example I'm want to write an algorithm like merge sort or some Java class and create test class so I can make unit test for that class.
I've create the project with:
File -> New -> Project -> java -> next and setup the project name and location
and I want to make the unit test for the class the I've created, and I've tried the following solotions :
[ctrl + shift + t]
But I always endup with import static org.junit.Assert.*;
cannot resolve symbol 'junit', I tried different unit test library end up the same way.
How to resolve this problem so I can make unit test class in this simple Java project?
We can run all unit tests inside the IntelliJ IDEA. IntelliJ IDEA has various unit testing frameworks like JUnit, TestNG and many more. In this section, we will understand how unit test work.
Open the corresponding JUnit test class in the editor. Place the cursor where you want a new test method to be generated. Press Alt+Insert and select Test Method from the Generate menu. By default, IntelliJ IDEA attaches the Test suffix to source class names when it automatically generates test classes.
Follow the steps given below to create Unit Test: 1. Create Projects 2. In src folder, create a Java class file and enter the following code. 3. Create a New Directory with name Test to perform Unit Testing. 4. Now our Project Structure will look like this. 5. Go to File->Project Structure->Module. A new window screen will open.
After IntelliJ IDEA finishes running your tests, it shows the results in the Run tool window on the Test Runner tab. For more information on how to analyze test results, refer to Explore test results.
You can use Gradle or Maven (my personal preference these days).
But the easiest way is to add the JUnit JAR to your project, write some tests, and execute them in IntelliJ.
Here's the model class:
package model;
public class Foo {
private String value;
public Foo(String v) { this.value = v; }
public String toString() { return this.value; }
}
Here's the test class:
package model;
public class FooTest {
@Test
public void testToString() {
String expected = "Test";
Foo foo = new Foo(expected);
Assert.assertEquals(expected, foo.toString());
}
}
I'm not sure this is the best solutions but I manage to build the unit test use gradle and maven. like this :
create Java project :
File -> New -> Project -> Gradle -> choose only java-> fill the groupId and ArtifactId-> choose
use default gradle wrapper
-> enter project name and location ->finish
and from the root of the project
right click -> Add Framework Support -> choose maven.
from there I can create the class that I want and make the unit test using the solutions from the question [ctrl + shift +t] .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With