Tristan_Bagnulo
Tristan_Bagnulo

Reputation: 21

How to import the '@Test' method in Java

I am trying to use the @Test function on a small program as I am directed to using Java 8 tutorial on youtube. Find it here. I am up to lesson 2 which starts at 21:59. Here is the youtube link.

https://www.youtube.com/watch?v=grEKMHGYyns

However, I have the following errors:

  1. When trying to import the Test function the advice given by my Apache Netbeans IDE is ...

"package org.junit does not exist"

... the same happens for "org.Assert"

  1. Over the "@Test" function on line 9 of my PersonTest.java file, the IDE gives the error...

"Cannot find symbol".

  1. The same error exists over the "assertEquals" method on line 13.

Attempted Solution 1: Search dependency at Maven Repositories for org.junit.Test.

Result 1: "No matching items."


Attempted Solution 2: write the dependencies into the pom.xml file.

Result: n/a. Nothing happened.


My code on the PersonTest.java file:


package com.marcusbiel.javacourse.lesson2;

import org.junit.Test;

import org.Assert.assertEquals;

public class PersonTest {


@Test
public void shouldReturnHelloWorld() {

    Person tristan = new Person();
    assertEquals("Hello World",tristan.helloWorld() );
}
}

My code on the Person.java file:


package com.marcusbiel.javacourse.lesson2;

public class Person {
    public String helloWorld(){
        return "Hello World";
    }       
}

My code on the pom.xml file:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>mavenproject1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>12</maven.compiler.source>
        <maven.compiler.target>12</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Note: I tried to fix the problem by adding in the... "

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

... part. I don't think it's working. I have no feedback from the program on that note.

When I run it. I want to see a messaging saying "all tests passed". As in the video here... (I have a timestamp on this link where the outcome occurs).

youtu.be/grEKMHGYyns?t=2125

Upvotes: 0

Views: 5573

Answers (2)

Mikko Maunu
Mikko Maunu

Reputation: 42074

Problem is very old JUnit version 3.8.1 (from 2007) combined with code that requires JUnit 4.

Issue can be solved by using Junit 4:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

Also Assert.assertEquals import is wrong. Should be:

import static org.junit.Assert.assertEquals;

Upvotes: 1

Hassam Abdelillah
Hassam Abdelillah

Reputation: 2294

I think that you should clean up and update your project dependencies.

Perform a mvn clean install from netbeans IDE.

This will update your project dependencies. I think this will solve the issue of unresolvable dependencies.

Here is the doc to using maven with netbeans

Upvotes: 0

Related Questions