@AutoWired @Component注解注入,测试过程不能有两个构造方法,不能识别。如果接口有多个实现类 注解的时候需要@Qualifier区分

@AutoWired @Component注解注入,测试过程不能有两个构造方法,不能识别

package testone.auto;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.*;

/**
 * @author helloLi
 * @version 1.0
 * @date 2020/4/8 12:45
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:META-INF/spring/soundsystem.xml")
public class TestStudentTest {
    @Autowired
    private TestStudent testStudent;
@Test
    public  void test() {
        testStudent.setName("王思");
        testStudent.setId(12);
    System.out.println(testStudent.getName());
    }

}

如果接口有多个实现类 注解的时候需要@Qualifier区分

package soundsystem;

import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:META-INF/spring/soundsystem.xml")
public class CDPlayerTest {

  @Rule
  public final StandardOutputStreamLog log = new StandardOutputStreamLog();

  @Autowired
  @Qualifier("pc")
  private MediaPlayer player;
  
  @Autowired
  private CompactDisc cd;


  @Test
  public void play() {
    player.play();
//    assertEquals(
//        "Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles\n",
//        log.getLog());
  }

}

你可能感兴趣的:(spring学习)